After using a LineSegmentDetector on an image, how can I retrieve the data, and can I choose how small of a line segment to detect?

Viewed 843

I'm trying to design a program that will recognize the rectangle in the image below. After running the Line Segment Detector from OpenCV, I'm left with the second image.

I can clearly see the rectangle, and I want to start filtering out garbage line segments, but I can't seem to find any way to retrieve the data of the line segments created.

It would also be helpful if, with the initial .detect method, I could explicity say "no lines smaller than 20 pixels". Is that possible?

A full working sample of my code is below:

public class VinDetector {

public static void main(String args[]) throws Exception {
    try {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        File input = new File("image.jpg");
        BufferedImage image = ImageIO.read(input);  

        byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        Mat sourceImg = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3);
        sourceImg.put(0, 0, data);

        Mat destImage = new Mat(image.getHeight(),image.getWidth(),CvType.CV_8UC1);
        Imgproc.cvtColor(sourceImg, destImage, Imgproc.COLOR_RGB2GRAY);

        MatOfFloat4 lines = new MatOfFloat4();
        LineSegmentDetector ls = Imgproc.createLineSegmentDetector();
        ls.detect(destImage, lines);

        System.out.println(ls.compareSegments(new Size(destImage.width(), destImage.height()), lines, lines));


        Mat lineImage = Imgcodecs.imread("image black.jpg");
        ls.drawSegments(lineImage, lines);
        Imgcodecs.imwrite("image TEST.jpg", lineImage);


    } catch (Exception e) {
        System.out.println("error: " + e.getMessage());
    }
}

}

Original After line segment detector

0 Answers
Related