Class Canny


  • public class Canny
    extends Object

    This software has been released into the public domain. Please read the notes in this source file for additional information.

    This class provides a configurable implementation of the Canny edge detection algorithm. This classic algorithm has a number of shortcomings, but remains an effective tool in many scenarios. This class is designed for single threaded use only.

    Sample usage:

    
     //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 a more complete understanding of this edge detector's parameters consult an explanation of the algorithm.

    Author:
    Tom Gibara
    • Constructor Detail

      • Canny

        public Canny()
        Constructs a new detector with default parameters.
      • Canny

        public Canny​(Float lowThreshold,
                     Float highThreshold,
                     Float gaussianKernelRadius,
                     Integer gaussianKernelWidth,
                     Boolean contrastNormalized,
                     RenderedImage sourceImage)
        Constructor that avoids setters.
        Parameters:
        lowThreshold -
        highThreshold -
        gaussianKernelRadius -
        gaussianKernelWidth -
        contrastNormalized -
        sourceImage -
    • Method Detail

      • getSourceImage

        public RenderedImage getSourceImage()
        The image that provides the luminance data used by this detector to generate edges.
        Returns:
        the source image, or null
      • setSourceImage

        public void setSourceImage​(RenderedImage image)
        Specifies the image that will provide the luminance data in which edges will be detected. A source image must be set before the process method is called.
        Parameters:
        image - a source of luminance data
      • getLowThreshold

        public float getLowThreshold()
        The low threshold for hysteresis. The default value is 2.5.
        Returns:
        the low hysteresis threshold
      • setLowThreshold

        public void setLowThreshold​(float threshold)
        Sets the low threshold for hysteresis. Suitable values for this parameter must be determined experimentally for each application. It is nonsensical (though not prohibited) for this value to exceed the high threshold value.
        Parameters:
        threshold - a low hysteresis threshold
      • getHighThreshold

        public float getHighThreshold()
        The high threshold for hysteresis. The default value is 7.5.
        Returns:
        the high hysteresis threshold
      • setHighThreshold

        public void setHighThreshold​(float threshold)
        Sets the high threshold for hysteresis. Suitable values for this parameter must be determined experimentally for each application. It is nonsensical (though not prohibited) for this value to be less than the low threshold value.
        Parameters:
        threshold - a high hysteresis threshold
      • getGaussianKernelWidth

        public int getGaussianKernelWidth()
        The number of pixels across which the Gaussian kernel is applied. The default value is 16.
        Returns:
        the radius of the convolution operation in pixels
      • setGaussianKernelWidth

        public void setGaussianKernelWidth​(int gaussianKernelWidth)
        The number of pixels across which the Gaussian kernel is applied. This implementation will reduce the radius if the contribution of pixel values is deemed negligable, so this is actually a maximum radius.
        Parameters:
        gaussianKernelWidth - a radius for the convolution operation in pixels, at least 2.
      • getGaussianKernelRadius

        public float getGaussianKernelRadius()
        The radius of the Gaussian convolution kernel used to smooth the source image prior to gradient calculation. The default value is 16.
        Returns:
        the Gaussian kernel radius in pixels
      • setGaussianKernelRadius

        public void setGaussianKernelRadius​(float gaussianKernelRadius)
        Sets the radius of the Gaussian convolution kernel used to smooth the source image prior to gradient calculation.
      • isContrastNormalized

        public boolean isContrastNormalized()
        Whether the luminance data extracted from the source image is normalized by linearizing its histogram prior to edge extraction. The default value is false.
        Returns:
        whether the contrast is normalized
      • setContrastNormalized

        public void setContrastNormalized​(boolean contrastNormalized)
        Sets whether the contrast is normalized
        Parameters:
        contrastNormalized - true if the contrast should be normalized, false otherwise
      • process

        public void process()