public class CannyEdgeDetector
extends java.lang.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.
Modifier and Type | Field and Description |
---|---|
protected java.awt.image.BufferedImage |
edgesImage |
protected int |
height |
protected int |
width |
Constructor and Description |
---|
CannyEdgeDetector()
Constructs a new detector with default parameters.
|
Modifier and Type | Method and Description |
---|---|
java.awt.image.BufferedImage |
getEdgesImage()
Obtains an image containing the edges detected during the last call to
the process method.
|
float |
getGaussianKernelRadius()
The radius of the Gaussian convolution kernel used to smooth the source
image prior to gradient calculation.
|
int |
getGaussianKernelWidth()
The number of pixels across which the Gaussian kernel is applied.
|
float |
getHighThreshold()
The high threshold for hysteresis.
|
float |
getLowThreshold()
The low threshold for hysteresis.
|
java.awt.image.BufferedImage |
getSourceImage()
The image that provides the luminance data used by this detector to
generate edges.
|
boolean |
isContrastNormalized()
Whether the luminance data extracted from the source image is normalized
by linearizing its histogram prior to edge extraction.
|
void |
process() |
void |
setContrastNormalized(boolean contrastNormalized)
Sets whether the contrast is normalized
|
void |
setEdgesImage(java.awt.image.BufferedImage edgesImage)
Sets the edges image.
|
void |
setGaussianKernelRadius(float gaussianKernelRadius)
Sets the radius of the Gaussian convolution kernel used to smooth the
source image prior to gradient calculation.
|
void |
setGaussianKernelWidth(int gaussianKernelWidth)
The number of pixels across which the Gaussian kernel is applied.
|
void |
setHighThreshold(float threshold)
Sets the high threshold for hysteresis.
|
void |
setLowThreshold(float threshold)
Sets the low threshold for hysteresis.
|
void |
setSourceImage(java.awt.image.BufferedImage image)
Specifies the image that will provide the luminance data in which edges
will be detected.
|
void |
writeEdges(int[] pixels) |
protected int height
protected int width
protected java.awt.image.BufferedImage edgesImage
public CannyEdgeDetector()
public java.awt.image.BufferedImage getSourceImage()
public void setSourceImage(java.awt.image.BufferedImage image)
image
- a source of luminance datapublic java.awt.image.BufferedImage getEdgesImage()
public void setEdgesImage(java.awt.image.BufferedImage edgesImage)
edgesImage
- expected (though not required) to be nullpublic float getLowThreshold()
public void setLowThreshold(float threshold)
threshold
- a low hysteresis thresholdpublic float getHighThreshold()
public void setHighThreshold(float threshold)
threshold
- a high hysteresis thresholdpublic int getGaussianKernelWidth()
public void setGaussianKernelWidth(int gaussianKernelWidth)
gaussianKernelWidth
- a radius for the convolution operation in
pixels, at least 2.public float getGaussianKernelRadius()
public void setGaussianKernelRadius(float gaussianKernelRadius)
public boolean isContrastNormalized()
public void setContrastNormalized(boolean contrastNormalized)
contrastNormalized
- true if the contrast should be normalized,
false otherwisepublic void process()
public void writeEdges(int[] pixels)