Example Canny Edge

From BoofCV
Revision as of 10:01, 19 June 2014 by Peter (talk | contribs)
Jump to navigationJump to search

Edge or contour detection is a basic computer vision problem. The Canny edge detector is a popular algorithm for detecting edges in an image which uses hystersis thresholding. In BoofCV the Canny edge detector can produce different kinds of output. A binary image containing every pixel which is identified as an edge or a tree graph containing all the selected edge pixels.

Example Code:


  • Object contours/edges

Relevant Applets:

Example Code

 * Demonstration of the Canny edge detection algorithm.  In this implementation the output can be a binary image and/or
 * a graph describing each contour.
 * @author Peter Abeles
public class ExampleCannyEdge {

	public static void main( String args[] ) {
		BufferedImage image = UtilImageIO.loadImage("../data/applet/simple_objects.jpg");

		ImageUInt8 gray = ConvertBufferedImage.convertFrom(image,(ImageUInt8)null);
		ImageUInt8 edgeImage = new ImageUInt8(gray.width,gray.height);

		// Create a canny edge detector which will dynamically compute the threshold based on maximum edge intensity
		// It has also been configured to save the trace as a graph.  This is the graph created while performing
		// hysteresis thresholding.
		CannyEdge<ImageUInt8,ImageSInt16> canny = FactoryEdgeDetectors.canny(2,true, true, ImageUInt8.class, ImageSInt16.class);

		// The edge image is actually an optional parameter.  If you don't need it just pass in null

		// First get the contour created by canny
		List<EdgeContour> edgeContours = canny.getContours();
		// The 'edgeContours' is a tree graph that can be difficult to process.  An alternative is to extract
		// the contours from the binary image, which will produce a single loop for each connected cluster of pixels.
		// Note that you are only interested in external contours.
		List<Contour> contours = BinaryImageOps.contour(edgeImage, ConnectRule.EIGHT, null);

		// display the results
		BufferedImage visualBinary = VisualizeBinaryData.renderBinary(edgeImage, null);
		BufferedImage visualCannyContour = VisualizeBinaryData.renderContours(edgeContours,null,
		BufferedImage visualEdgeContour = VisualizeBinaryData.renderExternal(contours, null,
				gray.width, gray.height, null);

		ShowImages.showWindow(visualBinary,"Binary Edges from Canny");
		ShowImages.showWindow(visualCannyContour,"Canny Trace Graph");
		ShowImages.showWindow(visualEdgeContour,"Contour from Canny Binary");