Ask Your Question

jamesarn0ld's profile - activity

2019-12-06 07:26:10 -0600 received badge  Famous Question (source)
2015-06-21 12:06:51 -0600 received badge  Notable Question (source)
2014-10-10 10:02:45 -0600 received badge  Popular Question (source)
2013-07-14 04:03:15 -0600 received badge  Editor (source)
2013-07-14 03:55:51 -0600 asked a question Android OpenCV find Square edges in contour then getPerspectiveTransform and warpPerspective

Hello, I have asked this question in stackoverflow (http://stackoverflow.com/questions/17637730/android-opencv-getperspectivetransform-and-warpperspective) but I think there will be a lot of people who can help me with my problem.

I am a bit confused with the parameters of getPerspectiveTransform as I cannot see a proper image. Here is my code. The original_image variable is the image that contains a square object (and some others) that I want to crop and create a new image (something like this http://stackoverflow.com/questions/17512234/android-opencv-find-largest-square-or-rectangle). The variables p1, p2, p3, and p4 are the coordinates of the corners of the largest square/rectangle in the image. p1 is the upper left, p2 is the upper right, p3 is the lower right, and p4 is the lower left (clockwise assigning).

    Mat src = new Mat(4,1,CvType.CV_32FC2);
    src.put((int)p1.y,(int)p1.x, (int)p2.y,(int)p2.x, (int)p4.y,(int)p4.x, (int)p3.y,(int)p3.x);
    Mat dst = new Mat(4,1,CvType.CV_32FC2);
    dst.put(0,0, 0,original_image.width(), original_image.height(),original_image.width(), original_image.height(),0);

    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src, dst);
    Mat cropped_image = original_image.clone();
    Imgproc.warpPerspective(untouched, cropped_image, perspectiveTransform, new Size(512,512));

When I try to display cropped_image, I get a "I don't know what it is" image. I think my parameters in getPerspectiveTransform() are incorrect (or is it). Please help. Thanks!

Update: When I debugged my code, I found out that the edges of my square/rectangle are incorrect, well some are quite right except for p4. This is my code to detect the edges of the square or rectangle in the image. My image is all black except for the contour of the largest square/rectangle which has a white outline.

    //we will find the edges of the new_image (corners of the square/rectangle)
    Point p1 = new Point(10000, 10000); //upper left; minX && minY
    Point p2 = new Point(0, 10000); //upper right; maxX && minY
    Point p3 = new Point(0, 0); //lower right; maxX && maxY
    Point p4 = new Point(10000, 0); //lower left; minX && maxY
    double[] temp_pixel_color;
    for (int x=0; x<new_image.rows(); x++) {
        for (int y=0; y<new_image.cols(); y++) {
            temp_pixel_color = new_image.get(x, y); //we have a black and white image so we only have one color channel
            if (temp_pixel_color[0] > 200) { //we found a white pixel
                if (x<=p1.x && y<=p1.y) { //for p1, minX && minY
                    p1.x = x;
                    p1.y = y;
                }
                else if (x>=p2.x && y<=p2.y) { //for p2, maxX && minY
                    p2.x = x;
                    p2.y = y;
                }
                else if (x>=p3.x && y>=p3.y) { //for p3, maxX && maxY
                    p3.x = x;
                    p3.y = y;
                }
                else if (x<=(int)p4.x && y>=(int)p4.y) { //for p4, minX && maxY
                    p4.x = x;
                    p4.y = y;
                }
            }
        }
    }

Here is my sample image. ignore the colored circles as they are drawn after the edges are detected: image description