This is an efficent implementation of the Canny edge detection algorithm in Java (see http://en.wikipedia.org/wiki/Canny_edge_detector) which I have released into the public domain.
It is implemented using a single Java class CannyEdgeDetector.java.
//create the detector
CannyEdgeDetector detector = new CannyEdgeDetector();
//adjust its parameters as desired
detector.setLowThreshold(0.5f);
detector.setHighThreshold(1f);
//apply it to an image
detector.setSourceImage(frame);
detector.process();
BufferedImage edges = detector.getEdgesImage();
For more information see the comments in the supplied code.
The version of this code published here prior to the 1st of Feburary 2011
contained an (incredibly dumb) error in the implementation of the performHysterisis()
function. Thanks to Noam Brown for analyzing and identifying the problem.
The version of this code published here prior to July 12th 2009
contained an error in the implementation of the hypot()
function.
Thanks to Joachim Pimiskern for taking the
time to observe, investigate and isolate the problem.
The following image was processed using the detector's default parameters.
The short video below was created during the testing and optimization of this implementation. I chose a 'comic styled' video clip in the expectation that its stylization would be favourable to the algorithm. It is combined with a colour reduction algorithm derived from a linear-time clustering algorithm I have developed.
It demonstrates some of the weaknesses of the algorithm and my implementation of it. Note the absence of edges around the edges of the frame. This is the width of the convolution operation. Also observe the poor localization of the edges in some cases (eg. the edge of the shield). This is a consequence of using a relatively large radius for the Gaussian kernel.
Download the video file here: flv 1.7MB, wmv 470KB, mov 475KB.