Ask Your Question
0

Find rectangle from image in android?

asked 2016-06-14 09:04:51 -0600

Hardik Patel gravatar image

updated 2016-07-01 08:41:08 -0600

hello,

i want to find four black cornered rectangle from below image

how to find those four border rectangle from this image?

i have tried with this code

 Mat rgbMat=ImageUtils.bitmapToMat(resultBitmap);

        Mat grayMat = new Mat(resultBitmap.getHeight(), resultBitmap.getWidth(),CvType.CV_8U, new Scalar(1));
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        List<Point> pointList=new ArrayList<Point>();
        for(int i=0; i<contours.size(); i++)
        {
            Rect rect = Imgproc.boundingRect(contours.get(i));
            double k = (rect.height+0.0)/rect.width;
            if (0.9<k && k<1.1 && rect.area() > 100)
            {
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0), 3);
            }
        }

        resultBitmap = ImageUtils.matToBitmap(rgbMat);

Please help me ..

Thanks in advance

edit retag flag offensive close merge delete

Comments

can you try again with the image ?

berak gravatar imageberak ( 2016-06-14 09:12:18 -0600 )edit

hello berak i tried with above code but can't find all rectangle.please help me

Hardik Patel gravatar imageHardik Patel ( 2016-06-15 00:31:10 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-06-16 06:14:03 -0600

lama123 gravatar image

updated 2016-06-17 00:07:02 -0600

Please find the code below

Mat image;
// read the input image
image = imread("D:\\omr.jpg");

Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);

GaussianBlur(gray, gray, Size(5,5) , 0, 0);

Mat thres;
threshold(gray, thres, 130, 255, CV_THRESH_BINARY_INV);
imshow("thres", thres);
waitKey();


//  apply a connected component analysis with statistics
//  (from opencv 3.0 onwards)
// cca
Mat labels, stats, centroids;
int numObjects = connectedComponentsWithStats(thres, labels, stats, centroids);

// Draw bounding boxes for objects in the original image
// exclude background = 0
int i;
for(i=1; i<numObjects; i++)
{
    int left = stats.at<int>(i, CC_STAT_LEFT);
    int top = stats.at<int>(i, CC_STAT_TOP);
    double width = stats.at<int>(i, CC_STAT_WIDTH);
    double height = stats.at<int>(i, CC_STAT_HEIGHT);
    int area = stats.at<int>(i, CC_STAT_AREA);

    double k = width/height;

    if(area > 500 && area < 2000 && k > 0.9 && k < 1.1)
    {
        // draw rectangle
        rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);
    }
}

imshow("BoundingBoxes", image);

The output image

image description

Here is the code using contours. I hope you are comfortable using it in Java.

Mat image;
// read the input image
image = imread("D:\\omr.jpg");

Mat gray;
cvtColor(image, gray, CV_BGR2GRAY);

GaussianBlur(gray, gray, Size(5,5) , 0, 0);

Mat thres;
threshold(gray, thres, 130, 255, CV_THRESH_BINARY_INV);
imshow("thres", thres);
waitKey();


vector<vector<Point> > contours;
// find contours
findContours(thres, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
for( int i = 0; i < contours.size(); i++ )
{
    Rect brect = boundingRect(Mat(contours[i]));

    double k = (brect.width+0.0)/brect.height;

    if(brect.area() > 500 && brect.area() < 2000 && k > 0.9 && k < 1.1)
    {

        RotatedRect minRect = minAreaRect( Mat(contours[i]) );

        // rotated rectangle
        Point2f rect_points[4];
        minRect.points( rect_points );
        for( int j = 0; j < 4; j++ )
            line( image, rect_points[j], rect_points[(j+1)%4], Scalar(0,255,0), 2, 8 );
    }
}


imshow("BoundingBoxes", image);

Regards

Amal

edit flag offensive delete link more

Comments

Thank you lama123 for your help. but i can't convert this code to java

int left = stats.at<int>(i, CC_STAT_LEFT);
int top = stats.at<int>(i, CC_STAT_TOP);
double width = stats.at<int>(i, CC_STAT_WIDTH);
double height = stats.at<int>(i, CC_STAT_HEIGHT);
int area = stats.at<int>(i, CC_STAT_AREA);

please help anybody for convert this code to java

Hardik Patel gravatar imageHardik Patel ( 2016-06-16 08:38:24 -0600 )edit

I have updated the code using contours in the previous answer, as you already know how to use contours in Java

lama123 gravatar imagelama123 ( 2016-06-17 00:08:45 -0600 )edit

Thankyou very very much lama123.you are great and very helpfull

Hardik Patel gravatar imageHardik Patel ( 2016-06-17 05:45:39 -0600 )edit

hello lama123. please just tell me how to check intensity of image?

Hardik Patel gravatar imageHardik Patel ( 2016-06-17 05:59:15 -0600 )edit

you mean intensity of a pixel at a particular position in image?

lama123 gravatar imagelama123 ( 2016-06-17 06:01:33 -0600 )edit

yes lama123. specially all corner where rectangle placed

Hardik Patel gravatar imageHardik Patel ( 2016-06-19 23:56:26 -0600 )edit

Scalar intensity = img.at<uchar>(y, x);

For more information check the section "Accessing pixel intensity values" in (http://docs.opencv.org/2.4/doc/user_g...)

lama123 gravatar imagelama123 ( 2016-06-20 05:10:40 -0600 )edit

hello @lama123, i am very thankfull if you help me on this topic http://answers.opencv.org/question/96...

Hardik Patel gravatar imageHardik Patel ( 2016-07-01 08:42:48 -0600 )edit

Hi @lama123, Can i find a digits and numbers by this method ? I'm new with opencv and can't know how to do that ?

Amr El saeed gravatar imageAmr El saeed ( 2017-02-11 00:19:40 -0600 )edit

Yes, i think you can use thresholding followed by findContours

lama123 gravatar imagelama123 ( 2017-04-29 00:55:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-14 09:04:51 -0600

Seen: 5,885 times

Last updated: Jul 01 '16