Difference between revisions of "Example Detect Lines"
From BoofCV
Jump to navigationJump to search (Updated for v0.3) |
m |
||
(14 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<center> | <center> | ||
<gallery widths=300px heights=240px> | <gallery widths=300px heights=240px> | ||
Line 13: | Line 11: | ||
Example Code: | Example Code: | ||
* [https://github.com/lessthanoptimal/BoofCV/blob/ | * [https://github.com/lessthanoptimal/BoofCV/blob/v0.40/examples/src/main/java/boofcv/examples/features/ExampleLineDetection.java ExampleLineDetection.java] | ||
Concepts: | Concepts: | ||
Line 19: | Line 17: | ||
* Line segment detection | * Line segment detection | ||
Relevant | Relevant Examples: | ||
* [[ | * [[Example_Non_Maximum_Suppression| Non-Maximum Suppression]] | ||
* [ | |||
Related Videos: | |||
* [https://www.youtube.com/watch?v=8pn9Ebw90uk&t=901s Summary Video] | |||
= Example Code = | = Example Code = | ||
Line 32: | Line 32: | ||
// adjust the maximum number of found lines in the image | // adjust the maximum number of found lines in the image | ||
private static final int maxLines = 10; | private static final int maxLines = 10; | ||
private static ListDisplayPanel listPanel = new ListDisplayPanel(); | |||
/** | /** | ||
* Detects lines inside the image using different types of Hough detectors | * Detects lines inside the image using different types of Hough detectors | ||
* | * | ||
* @param | * @param buffered Input image. | ||
* @param imageType Type of image processed by line detector. | * @param imageType Type of image processed by line detector. | ||
*/ | */ | ||
public static<T extends | public static <T extends ImageGray<T>> | ||
void detectLines( BufferedImage buffered, Class<T> imageType ) { | |||
// convert the line into a single band image | // convert the line into a single band image | ||
T input = ConvertBufferedImage.convertFromSingle( | T input = ConvertBufferedImage.convertFromSingle(buffered, null, imageType); | ||
T blurred = input.createSameShape(); | |||
// Blur smooths out gradient and improves results | |||
GBlurImageOps.gaussian(input, blurred, 0, 5, null); | |||
// Detect edges of objects using gradient based hough detectors. If you have nice binary lines which are thin | |||
// there's another type of hough detector available | |||
DetectLine<T> detectorPolar = FactoryDetectLine.houghLinePolar( | |||
new ConfigHoughGradient(maxLines), null, imageType); | |||
DetectLine<T> detectorFoot = FactoryDetectLine.houghLineFoot( | |||
new ConfigHoughGradient(maxLines), null, imageType); | |||
DetectLine<T> detectorFootSub = FactoryDetectLine.houghLineFootSub( | |||
new ConfigHoughFootSubimage(3, 8, 5, edgeThreshold, maxLines, 2, 2), imageType); | |||
detectLines(buffered, blurred, detectorPolar, "Hough Polar"); | |||
detectLines(buffered, blurred, detectorFoot, "Hough Foot"); | |||
detectLines(buffered, blurred, detectorFootSub, "Hough Foot-Sub"); | |||
} | |||
List<LineParametric2D_F32> found = detector.detect( | private static <T extends ImageGray<T>> | ||
void detectLines( BufferedImage buffered, T gray, DetectLine<T> detector, String name ) { | |||
List<LineParametric2D_F32> found = detector.detect(gray); | |||
// display the results | // display the results | ||
ImageLinePanel gui = new ImageLinePanel(); | ImageLinePanel gui = new ImageLinePanel(); | ||
gui. | gui.setImage(buffered); | ||
gui.setLines(found); | gui.setLines(found); | ||
gui.setPreferredSize(new Dimension( | gui.setPreferredSize(new Dimension(gray.getWidth(), gray.getHeight())); | ||
listPanel.addItem(gui, name); | |||
} | } | ||
Line 72: | Line 82: | ||
* @param image Input image. | * @param image Input image. | ||
* @param imageType Type of image processed by line detector. | * @param imageType Type of image processed by line detector. | ||
*/ | */ | ||
public static<T extends | public static <T extends ImageGray<T>, D extends ImageGray<D>> | ||
void detectLineSegments( BufferedImage image , | void detectLineSegments( BufferedImage image, | ||
Class<T> imageType | Class<T> imageType ) { | ||
// convert the line into a single band image | // convert the line into a single band image | ||
T input = ConvertBufferedImage.convertFromSingle(image, null, imageType ); | T input = ConvertBufferedImage.convertFromSingle(image, null, imageType); | ||
// Comment/uncomment to try a different type of line detector | // Comment/uncomment to try a different type of line detector | ||
DetectLineSegment<T> detector = FactoryDetectLine.lineRansac(new ConfigLineRansac(40, 30, 2.36, true), imageType); | |||
List<LineSegment2D_F32> found = detector.detect(input); | List<LineSegment2D_F32> found = detector.detect(input); | ||
Line 89: | Line 96: | ||
// display the results | // display the results | ||
ImageLinePanel gui = new ImageLinePanel(); | ImageLinePanel gui = new ImageLinePanel(); | ||
gui. | gui.setImage(image); | ||
gui.setLineSegments(found); | gui.setLineSegments(found); | ||
gui.setPreferredSize(new Dimension(image.getWidth(),image.getHeight())); | gui.setPreferredSize(new Dimension(image.getWidth(), image.getHeight())); | ||
listPanel.addItem(gui, "Line Segments"); | |||
} | } | ||
detectLines(input, | public static void main( String[] args ) { | ||
BufferedImage input = UtilImageIO.loadImageNotNull(UtilIO.pathExample("simple_objects.jpg")); | |||
detectLines(input, GrayU8.class); | |||
// line segment detection is still under development and only works for F32 images right now | // line segment detection is still under development and only works for F32 images right now | ||
detectLineSegments(input, | detectLineSegments(input, GrayF32.class); | ||
ShowImages.showWindow(listPanel, "Detected Lines", true); | |||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 13:06, 17 January 2022
Lines are commonly found in man made environments. They can often be detected in location where other feature cannot due to the environments sparsity. Typical applications are for location targets and for evaluating the 3D structure of a building. Mathematically, a line has no end points and a line detector in BoofCV does not detect end points either. BoofCV does provide ways to detect line segments, which are lines with a beginning and end.
Line and line segment detection is still under development. No highly abstract and simplified interface is provided yet. Even though its under development still, these algorithms are still effective at detecting lines and line segments.
Example Code:
Concepts:
- Line detection
- Line segment detection
Relevant Examples:
Related Videos:
Example Code
public class ExampleLineDetection {
// adjusts edge threshold for identifying pixels belonging to a line
private static final float edgeThreshold = 25;
// adjust the maximum number of found lines in the image
private static final int maxLines = 10;
private static ListDisplayPanel listPanel = new ListDisplayPanel();
/**
* Detects lines inside the image using different types of Hough detectors
*
* @param buffered Input image.
* @param imageType Type of image processed by line detector.
*/
public static <T extends ImageGray<T>>
void detectLines( BufferedImage buffered, Class<T> imageType ) {
// convert the line into a single band image
T input = ConvertBufferedImage.convertFromSingle(buffered, null, imageType);
T blurred = input.createSameShape();
// Blur smooths out gradient and improves results
GBlurImageOps.gaussian(input, blurred, 0, 5, null);
// Detect edges of objects using gradient based hough detectors. If you have nice binary lines which are thin
// there's another type of hough detector available
DetectLine<T> detectorPolar = FactoryDetectLine.houghLinePolar(
new ConfigHoughGradient(maxLines), null, imageType);
DetectLine<T> detectorFoot = FactoryDetectLine.houghLineFoot(
new ConfigHoughGradient(maxLines), null, imageType);
DetectLine<T> detectorFootSub = FactoryDetectLine.houghLineFootSub(
new ConfigHoughFootSubimage(3, 8, 5, edgeThreshold, maxLines, 2, 2), imageType);
detectLines(buffered, blurred, detectorPolar, "Hough Polar");
detectLines(buffered, blurred, detectorFoot, "Hough Foot");
detectLines(buffered, blurred, detectorFootSub, "Hough Foot-Sub");
}
private static <T extends ImageGray<T>>
void detectLines( BufferedImage buffered, T gray, DetectLine<T> detector, String name ) {
List<LineParametric2D_F32> found = detector.detect(gray);
// display the results
ImageLinePanel gui = new ImageLinePanel();
gui.setImage(buffered);
gui.setLines(found);
gui.setPreferredSize(new Dimension(gray.getWidth(), gray.getHeight()));
listPanel.addItem(gui, name);
}
/**
* Detects segments inside the image
*
* @param image Input image.
* @param imageType Type of image processed by line detector.
*/
public static <T extends ImageGray<T>, D extends ImageGray<D>>
void detectLineSegments( BufferedImage image,
Class<T> imageType ) {
// convert the line into a single band image
T input = ConvertBufferedImage.convertFromSingle(image, null, imageType);
// Comment/uncomment to try a different type of line detector
DetectLineSegment<T> detector = FactoryDetectLine.lineRansac(new ConfigLineRansac(40, 30, 2.36, true), imageType);
List<LineSegment2D_F32> found = detector.detect(input);
// display the results
ImageLinePanel gui = new ImageLinePanel();
gui.setImage(image);
gui.setLineSegments(found);
gui.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
listPanel.addItem(gui, "Line Segments");
}
public static void main( String[] args ) {
BufferedImage input = UtilImageIO.loadImageNotNull(UtilIO.pathExample("simple_objects.jpg"));
detectLines(input, GrayU8.class);
// line segment detection is still under development and only works for F32 images right now
detectLineSegments(input, GrayF32.class);
ShowImages.showWindow(listPanel, "Detected Lines", true);
}
}