Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following java code: import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; // Main class public class HoughTransform extends Frame implements ActionListener

Given the following java code:

import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; // Main class public class HoughTransform extends Frame implements ActionListener { BufferedImage input; int width, height, diagonal; ImageCanvas source, target; TextField texRad, texThres; // Constructor public HoughTransform(String name) { super("Hough Transform"); // load image try { input = ImageIO.read(new File(name)); } catch ( Exception ex ) { ex.printStackTrace(); } width = input.getWidth(); height = input.getHeight(); diagonal = (int)Math.sqrt(width * width + height * height); // prepare the panel for two images. Panel main = new Panel(); source = new ImageCanvas(input); target = new ImageCanvas(input); main.setLayout(new GridLayout(1, 2, 10, 10)); main.add(source); main.add(target); // prepare the panel for buttons. Panel controls = new Panel(); Button button = new Button("Line Transform"); button.addActionListener(this); controls.add(button); controls.add(new Label("Radius:")); texRad = new TextField("10", 3); controls.add(texRad); button = new Button("Circle Transform"); button.addActionListener(this); controls.add(button); controls.add(new Label("Threshold:")); texThres = new TextField("25", 3); controls.add(texThres); button = new Button("Search"); button.addActionListener(this); controls.add(button); // add two panels add("Center", main); add("South", controls); addWindowListener(new ExitListener()); setSize(diagonal*2+100, Math.max(height,360)+100); setVisible(true); } class ExitListener extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } } // Action listener public void actionPerformed(ActionEvent e) { // perform one of the Hough transforms if the button is clicked. if ( ((Button)e.getSource()).getLabel().equals("Line Transform") ) { int[][] g = new int[360][diagonal]; int r = 0; // insert your implementation for straight-line here. DisplayTransform(diagonal, 360, g); } else if ( ((Button)e.getSource()).getLabel().equals("Circle Transform") ) { int[][] g = new int[height][width]; int radius = Integer.parseInt(texRad.getText()); // insert your implementation for circle here. DisplayTransform(width, height, g); } } // display the spectrum of the transform. public void DisplayTransform(int wid, int hgt, int[][] g) { target.resetBuffer(wid, hgt); for ( int y=0, i=0 ; y 255 ? 255 : g[y][x]; target.image.setRGB(x, y, new Color(value, value, value).getRGB()); } target.repaint(); } public static void main(String[] args) { new HoughTransform(args.length==1 ? args[0] : "rectangle.png"); } }

Also given:

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(); } } } }

Complete the code so when the Circle Transform button is pressed it applies Circle Hough Transform to the image and displays on the output image, and displays the circles with given radius on the input image.

Image given:

image text in transcribed

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

How do you add two harmonic motions having different frequencies?

Answered: 1 week ago

Question

Evaluate the importance of the employee handbook.

Answered: 1 week ago

Question

Discuss the steps in the progressive discipline approach.

Answered: 1 week ago