Ask Your Question
0

HoughLinesP generates an empty image

asked 2018-02-14 07:16:53 -0600

Murilo Pereira gravatar image

updated 2018-02-14 07:52:31 -0600

I'm developing an app wich uses OMR (Optical mark reconize) to read bubblesheets, I'm using the OpenCV API but I'm having some troubbles with HoughLinesP, when I use it that returns an empty image to me. I used the function of Android, Log.e, to see the total, cols and rows of the image before and after the HoughLinesP.

My code is a conversion of a C++ code to Java, both uses OpenCV.

Before I get the following result:

[ORIGINAL IMAGE]: 83840||262||320

After I get the following result:

[LINES IMAGE]: 0||0||1

My code:

private void scanImage(){
    Mat img = Imgcodecs.imread(mediaStorageDir().getPath() + "/" + "test2.jpg", Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);

    Log.e("[CANAIS]", String.valueOf(img.channels()));

    Size sz = new Size(3,3);
    Imgproc.GaussianBlur(img, img, sz, 0);
    Imgproc.adaptiveThreshold(img, img, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 75, 10);
    Core.bitwise_not(img, img);

    Mat img2 = new Mat();
    Imgproc.cvtColor(img, img2, Imgproc.COLOR_GRAY2RGB);

    Mat img3 = new Mat();
    Imgproc.cvtColor(img, img3, Imgproc.COLOR_GRAY2RGB);

    MatOfInt4 lines = new MatOfInt4();

    Log.e("[ORIGINAL IMAGE]", "" + img.total() + "||" + img.rows() + "||" + img.cols());

    Imgproc.HoughLinesP(img, lines, 1, Math.PI/180,80,400,10);

    Log.e("[LINES IMAGE]", "" + lines.total() + "||" + lines.rows() + "||" + lines.cols());

    for(int i = 0; i < lines.total(); i++){
        Mat l = new Mat();
        l.put(i, 0, lines.get(i, 0));
        Point pr = new Point();
        Point ps = new Point();
        pr.x = Double.valueOf(l.get(0, 0).toString());
        pr.y = Double.valueOf(l.get(1, 0).toString());

        ps.x = Double.valueOf(l.get(2, 0).toString());
        ps.y = Double.valueOf(l.get(3,0).toString());

        Scalar scalar = new Scalar(0,0,255);

        Imgproc.line(img2, pr, ps, scalar, 3, Imgproc.LINE_AA, 0);
    }

    showImage(img2);
    MatOfInt4 mt4 = new MatOfInt4(lines);
    LinkedList<Point> corners = new LinkedList<>();
    for(int i = 0; i < lines.total(); i++){
        for(int x = i + 1; x <lines.total(); x++){
            MatOfInt4 gen = new MatOfInt4();
            MatOfInt4 gen1 = new MatOfInt4();
            gen1.put(x, 0, mt4.get(x, 0));
            gen.put(i, 0, mt4.get(i, 0));
            Point pt = computeIntersect(gen, gen1);
            if(pt.x >= 0 && pt.y >= 0 && pt.x < img.cols() && pt.y < img.rows()){
                corners.addLast(pt);
            }
        }
    }

    Point center = new Point(0,0);
    MatOfPoint mtp = new MatOfPoint(center);
    for(int i = 0; i < corners.size(); i++){
        center.x += corners.get(i).x;
        center.y += corners.get(i).y;
    }

    center.x *= 1./ corners.size();
    center.y *= 1./ corners.size();

    sortCorners(corners, center);

    Rect r = Imgproc.boundingRect(mtp);
    Log.e("[RECT]", r.toString());

    Mat quad = Mat.zeros(r.height, r.width, CvType.CV_8UC3);
    LinkedList<Point> quad_pts = new LinkedList<>();
    quad_pts.addLast(new Point(0,0));
    quad_pts.addLast(new Point(quad.cols(),0));
    quad_pts.addLast(new Point(quad.cols(),quad.rows()));
    quad_pts.addLast(new Point(0,quad.rows()));

    Mat transmtx = Imgproc.getPerspectiveTransform(Converters.vector_Point2f_to_Mat(corners), Converters.vector_Point2f_to_Mat(quad_pts));
    Imgproc.warpPerspective(img3, quad, transmtx, quad.size());

    showImage(quad);


}
private Point computeIntersect(MatOfInt4 a, MatOfInt4 b){
    Point generc = new Point();
    generc.x = -1;
    generc.y = -1 ...
(more)
edit retag flag offensive close merge delete

Comments

could you try to reduce the code to the bare minimum, so folks here can try to reproduce the problem ? (also, an image would be useful)

it did not find any lines in your image, try to remove the adaptiveThreshold (anything hough related works on gradients, and not with binary images)

berak gravatar imageberak ( 2018-02-14 07:25:27 -0600 )edit

I let just the important part of code for this case. I tried to remove the adaptiveThreshold, no success. The image that I'm using is at the end of the post.

Murilo Pereira gravatar imageMurilo Pereira ( 2018-02-14 08:04:18 -0600 )edit

if your image is [320 x 262] and your minimum line length 400, hmmm

berak gravatar imageberak ( 2018-02-14 08:56:33 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-02-14 10:49:07 -0600

Murilo Pereira gravatar image

Thanks, I just translated the code for java and copied the image, I did not pay attention to this detail. I reduced it from 400 to 200 and the problem was solved, thank you very much.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-02-14 07:16:53 -0600

Seen: 469 times

Last updated: Feb 14 '18