Ask Your Question

Murilo Pereira's profile - activity

2018-02-15 18:14:05 -0500 asked a question Different results of code language conversion

Different results of code language conversion Hello everyone, I'm developing an OMR Application in Java/Android, I get a

2018-02-14 11:46:58 -0500 marked best answer HoughLinesP generates an empty image

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)
2018-02-14 10:49:07 -0500 commented question HoughLinesP generates an empty image

Thanks, I just translated the code for java and copied the image, I did not pay attention to this detail. I reduced it f

2018-02-14 08:05:02 -0500 commented question HoughLinesP generates an empty image

I let just the important part of code for this case. I tried to remove the adaptiveThreshold, no success. The image that

2018-02-14 08:04:47 -0500 commented question HoughLinesP generates an empty image

I let just the important part of code for this case. I tryied to remove the adaptiveThreshold, no success. The image tha

2018-02-14 08:04:26 -0500 commented question HoughLinesP generates an empty image

I let just the part of code important for this case. I tryied to remove the adaptiveThreshold, no success. The image tha

2018-02-14 08:04:18 -0500 commented question HoughLinesP generates an empty image

I just the part of code important for this case. I tryied to remove the adaptiveThreshold, no success. The image that I'

2018-02-14 07:52:31 -0500 received badge  Editor (source)
2018-02-14 07:52:31 -0500 edited question HoughLinesP generates an empty image

HoughLinesP generates an empty image I'm developing an app wich uses OMR (Optical mark reconize) to read bubblesheets, I

2018-02-14 07:19:03 -0500 asked a question HoughLinesP generates an empty image

HoughLinesP generates an empty image I'm developing an app wich uses OMR (Optical mark reconize) to read bubblesheets, I