Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Im really having a hard time figuring this out! Given an input image, your first need to display its histogram 1. Then, your task is

Im really having a hard time figuring this out!

Given an input image, your first need to display its histogram

1. Then, your task is to implement the following four thresholding approaches: manual thresholding using user-specified threshold value, automatic thresholding based on image histogram, Otsus thresholding method, and adaptive thresholding with 77 window size and user-specified offset value. For each of the first three global thresholding approaches, you are also required to display the threshold value used in the histogram as a vertical bar. When the input is a color image, you need to perform thresholding to different color channels independently. This means that, for each of the last three automatic thresholding approaches, the threshold values used for the three color channels could be different.

Code worked on so far:

ImageCanvas.java

import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*;

// Canvas for image display class ImageCanvas extends Canvas { BufferedImage image;

// initialize the image and mouse control public ImageCanvas(BufferedImage input) { image = input; addMouseListener(new ClickListener()); } public ImageCanvas(int width, int height) { image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); addMouseListener(new ClickListener()); }

// redraw the canvas public void paint(Graphics g) { // draw boundary g.setColor(Color.gray); g.drawRect(0, 0, getWidth()-1, getHeight()-1); // compute the offset of the image. int xoffset = (getWidth() - image.getWidth()) / 2; int yoffset = (getHeight() - image.getHeight()) / 2; g.drawImage(image, xoffset, yoffset, this); }

// reset an empty image public void resetBuffer(int width, int height) { image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); repaint(); } // reset image based on the input public void copyImage(BufferedImage input) { Graphics2D g2D = image.createGraphics(); g2D.drawImage(input, 0, 0, null); repaint(); }

// listen to mouse click class ClickListener extends MouseAdapter { public void mouseClicked(MouseEvent e) { if ( e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON3 ) try { ImageIO.write(image, "png", new File("saved.png")); } catch ( Exception ex ) { ex.printStackTrace(); } }

ImageThreshold.java

import java.util.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.awt.geom.*; import java.io.*; import javax.imageio.*;

// Main class public class ImageThreshold extends Frame implements ActionListener { BufferedImage input; int width, height; TextField texThres, texOffset; ImageCanvas source, target; PlotCanvas plot; // Constructor public ImageThreshold(String name) { super("Image Histogram"); // load image try { input = ImageIO.read(new File(name)); } catch ( Exception ex ) { ex.printStackTrace(); } width = input.getWidth(); height = input.getHeight(); // prepare the panel for image canvas. Panel main = new Panel(); source = new ImageCanvas(input); plot = new PlotCanvas(256, 200); target = new ImageCanvas(width, height); target.copyImage(input); main.setLayout(new GridLayout(1, 3, 10, 10)); main.add(source); main.add(plot); main.add(target); // prepare the panel for buttons. Panel controls = new Panel(); controls.add(new Label("Threshold:")); texThres = new TextField("128", 2); controls.add(texThres); Button button = new Button("Manual Selection"); button.addActionListener(this); controls.add(button); button = new Button("Automatic Selection"); button.addActionListener(this); controls.add(button); button = new Button("Otsus Method"); button.addActionListener(this); controls.add(button); controls.add(new Label("Offset:")); texOffset = new TextField("10", 2); controls.add(texOffset); button = new Button("Adaptive Mean-C"); button.addActionListener(this); controls.add(button); // add two panels add("Center", main); add("South", controls); addWindowListener(new ExitListener()); setSize(width*2+400, height+100); setVisible(true); } class ExitListener extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // Action listener for button click events public void actionPerformed(ActionEvent e) { // example -- compute the average color for the image if ( ((Button)e.getSource()).getLabel().equals("Manual Selection") ) { int threshold; try { threshold = Integer.parseInt(texThres.getText()); } catch (Exception ex) { texThres.setText("128"); threshold = 128; } plot.clearObjects(); plot.addObject(new VerticalBar(Color.BLACK, threshold, 100)); } } public static void main(String[] args) { new ImageThreshold(args.length==1 ? args[0] : "fingerprint.png"); } }

PlotCanvas.java

import java.awt.*; import java.util.*;

// an interface for all objects to be plotted interface Plotable { public void plot(Graphics g, int xoffset, int yoffset); }

// Canvas for plotting graph class PlotCanvas extends Canvas { // size of plot area int width, height; // Axes and objects to be plotted Axis x_axis, y_axis; Vector objects;

public PlotCanvas(int wid, int hgt) { width = wid; height = hgt; x_axis = new Axis(true, width); y_axis = new Axis(false, height); objects = new Vector(); } // add objects to plot public void addObject(Plotable obj) { objects.add(obj); repaint(); } public void clearObjects() { objects.clear(); repaint(); } // redraw the canvas public void paint(Graphics g) { // draw axis int xoffset = (getWidth() - width) / 2; int yoffset = (getHeight() + height) / 2; x_axis.plot(g, xoffset, yoffset); y_axis.plot(g, xoffset, yoffset); // plot each object Iterator itr = objects.iterator(); while(itr.hasNext()) itr.next().plot(g, xoffset, yoffset); } }

// Axis class for plotting X or Y axis class Axis implements Plotable { // type and length of the axis boolean xAxis; int length, size=15; // Constructor public Axis(boolean isX, int len) { xAxis = isX; length = len; } // plot axis with arrow public void plot(Graphics g, int xoffset, int yoffset) { g.setColor(Color.BLACK); if ( xAxis ) { g.drawLine(xoffset-size, yoffset, xoffset+length+size, yoffset); g.fillArc(xoffset+length, yoffset-size, size*2, size*2, 160, 40); } else { g.drawLine(xoffset, yoffset+size, xoffset, yoffset-length-size); g.fillArc(xoffset-size, yoffset-length-size*2, size*2, size*2, 250, 40); } } }

// Bar class defines for ploting a vertical line class VerticalBar implements Plotable { // color, location, and length of the line Color color; int pos, length; // Constructor public VerticalBar(Color clr, int p, int len) { color = clr; pos = p; length = len; } public void plot(Graphics g, int xoffset, int yoffset) { g.setColor(color); g.drawLine(xoffset+pos, yoffset, xoffset+pos, yoffset-length); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

Students also viewed these Databases questions

Question

10. What is meant by a feed rate?

Answered: 1 week ago