use of georegression.fitting.line.ModelManagerLinePolar2D_F32 in project BoofCV by lessthanoptimal.
the class FactoryDetectLineAlgs method lineRansac.
/**
* Detects line segments inside an image using the {@link DetectLineSegmentsGridRansac} algorithm.
*
* @see DetectLineSegmentsGridRansac
*
* @param regionSize Size of the region considered. Try 40 and tune.
* @param thresholdEdge Threshold for determining which pixels belong to an edge or not. Try 30 and tune.
* @param thresholdAngle Tolerance in angle for allowing two edgels to be paired up, in radians. Try 2.36
* @param connectLines Should lines be connected and optimized.
* @param imageType Type of single band input image.
* @param derivType Image derivative type.
* @return Line segment detector
*/
public static <I extends ImageGray<I>, D extends ImageGray<D>> DetectLineSegmentsGridRansac<I, D> lineRansac(int regionSize, double thresholdEdge, double thresholdAngle, boolean connectLines, Class<I> imageType, Class<D> derivType) {
ImageGradient<I, D> gradient = FactoryDerivative.sobel(imageType, derivType);
ModelManagerLinePolar2D_F32 manager = new ModelManagerLinePolar2D_F32();
GridLineModelDistance distance = new GridLineModelDistance((float) thresholdAngle);
GridLineModelFitter fitter = new GridLineModelFitter((float) thresholdAngle);
ModelMatcher<LinePolar2D_F32, Edgel> matcher = new Ransac<>(123123, manager, fitter, distance, 25, 1);
GridRansacLineDetector<D> alg;
if (derivType == GrayF32.class) {
alg = (GridRansacLineDetector) new ImplGridRansacLineDetector_F32(regionSize, 10, matcher);
} else if (derivType == GrayS16.class) {
alg = (GridRansacLineDetector) new ImplGridRansacLineDetector_S16(regionSize, 10, matcher);
} else {
throw new IllegalArgumentException("Unsupported derivative type");
}
ConnectLinesGrid connect = null;
if (connectLines)
connect = new ConnectLinesGrid(Math.PI * 0.01, 1, 8);
return new DetectLineSegmentsGridRansac<>(alg, connect, gradient, thresholdEdge, imageType, derivType);
}
use of georegression.fitting.line.ModelManagerLinePolar2D_F32 in project BoofCV by lessthanoptimal.
the class VisualizeLineRansac method process.
public void process(BufferedImage image) {
int regionSize = 40;
I input = GeneralizedImageOps.createSingleBand(imageType, image.getWidth(), image.getHeight());
D derivX = GeneralizedImageOps.createSingleBand(derivType, image.getWidth(), image.getHeight());
D derivY = GeneralizedImageOps.createSingleBand(derivType, image.getWidth(), image.getHeight());
GrayF32 edgeIntensity = new GrayF32(input.width, input.height);
GrayF32 suppressed = new GrayF32(input.width, input.height);
GrayF32 orientation = new GrayF32(input.width, input.height);
GrayS8 direction = new GrayS8(input.width, input.height);
GrayU8 detected = new GrayU8(input.width, input.height);
ModelManager<LinePolar2D_F32> manager = new ModelManagerLinePolar2D_F32();
GridLineModelDistance distance = new GridLineModelDistance((float) (Math.PI * 0.75));
GridLineModelFitter fitter = new GridLineModelFitter((float) (Math.PI * 0.75));
ModelMatcher<LinePolar2D_F32, Edgel> matcher = new Ransac<>(123123, manager, fitter, distance, 25, 1);
ImageGradient<I, D> gradient = FactoryDerivative.sobel(imageType, derivType);
System.out.println("Image width " + input.width + " height " + input.height);
ConvertBufferedImage.convertFromSingle(image, input, imageType);
gradient.process(input, derivX, derivY);
GGradientToEdgeFeatures.intensityAbs(derivX, derivY, edgeIntensity);
// non-max suppression on the lines
// GGradientToEdgeFeatures.direction(derivX,derivY,orientation);
// GradientToEdgeFeatures.discretizeDirection4(orientation,direction);
// GradientToEdgeFeatures.nonMaxSuppression4(edgeIntensity,direction,suppressed);
GThresholdImageOps.threshold(edgeIntensity, detected, 30, false);
GridRansacLineDetector<GrayF32> alg = new ImplGridRansacLineDetector_F32(40, 10, matcher);
alg.process((GrayF32) derivX, (GrayF32) derivY, detected);
MatrixOfList<LineSegment2D_F32> gridLine = alg.getFoundLines();
ConnectLinesGrid connect = new ConnectLinesGrid(Math.PI * 0.01, 1, 8);
// connect.process(gridLine);
// LineImageOps.pruneClutteredGrids(gridLine,3);
List<LineSegment2D_F32> found = gridLine.createSingleList();
System.out.println("size = " + found.size());
LineImageOps.mergeSimilar(found, (float) (Math.PI * 0.03), 5f);
// LineImageOps.pruneSmall(found,40);
System.out.println("after size = " + found.size());
ImageLinePanel gui = new ImageLinePanel();
gui.setBackground(image);
gui.setLineSegments(found);
gui.setPreferredSize(new Dimension(image.getWidth(), image.getHeight()));
BufferedImage renderedBinary = VisualizeBinaryData.renderBinary(detected, false, null);
ShowImages.showWindow(renderedBinary, "Detected Edges");
ShowImages.showWindow(gui, "Detected Lines");
}
use of georegression.fitting.line.ModelManagerLinePolar2D_F32 in project BoofCV by lessthanoptimal.
the class CommonGridRansacLineDetectorChecks method checkObvious.
/**
* Give it a single straight line and see if it can detect it. Allow the region size to be changed to check
* for issues related to that
* @param regionSize
*/
protected void checkObvious(int regionSize) {
// System.out.println("regionSize = "+regionSize);
int where = 25;
GrayU8 edgeImage = new GrayU8(width, height);
D derivX = GeneralizedImageOps.createSingleBand(derivType, width, height);
D derivY = GeneralizedImageOps.createSingleBand(derivType, width, height);
for (int i = 0; i < height; i++) {
edgeImage.set(where, i, 1);
GeneralizedImageOps.set(derivX, where, i, 20);
}
ModelManagerLinePolar2D_F32 manager = new ModelManagerLinePolar2D_F32();
GridLineModelDistance distance = new GridLineModelDistance(0.9f);
GridLineModelFitter fitter = new GridLineModelFitter(0.9f);
ModelMatcher<LinePolar2D_F32, Edgel> matcher = new Ransac<>(123123, manager, fitter, distance, 25, 1);
GridRansacLineDetector<D> alg = createDetector(regionSize, 5, matcher);
alg.process(derivX, derivY, edgeImage);
MatrixOfList<LineSegment2D_F32> lines = alg.getFoundLines();
assertEquals(width / regionSize, lines.getWidth());
assertEquals(height / regionSize, lines.getHeight());
int gridCol = where / regionSize;
for (int i = 0; i < lines.height; i++) {
List<LineSegment2D_F32> l = lines.get(gridCol, i);
assertTrue(l.size() == 1);
LineSegment2D_F32 a = l.get(0);
assertTrue(Math.abs(a.slopeY()) > 1);
assertTrue(Math.abs(a.slopeX()) < 0.01);
}
}
Aggregations