Ask Your Question
0

How to detect colored rectangle from image in android? [closed]

asked 2016-05-24 06:36:57 -0600

Hardik Patel gravatar image

updated 2016-05-27 07:47:53 -0600

hello all, i want detect specific colored rectangle from paper sheet which indicate border of paper.so i want detect that rectangle and crop it so i can do further operation on sheet.

i tried this code..

        Mat rgbMat = new Mat();
        Utils.bitmapToMat(resultBitmap1, rgbMat);
        Mat grayMat = new Mat(resultBitmap1.getHeight(), resultBitmap1.getWidth(),
                CvType.CV_8U, new Scalar(1));
        List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
        Mat hierarchy = new Mat();

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 2);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
        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)
            {
                Point tmppnt=new Point(rect.x,rect.y);
                pointList.add(tmppnt);
                Imgproc.drawContours(rgbMat, contours, i, new Scalar(255, 0, 0),3);
            }
        }

        Utils.matToBitmap(rgbMat, resultBitmap1);
        sourceImageView.setImageBitmap(resultBitmap1);

please help me..

thanks

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Hardik Patel
close date 2016-07-01 08:18:07.291950

Comments

take a look at this. maybe it will be helpful for your next step.

sturkmen gravatar imagesturkmen ( 2016-05-24 07:29:46 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2016-05-24 07:07:33 -0600

lama123 gravatar image

updated 2016-05-24 23:28:58 -0600

Steps

1) Just scan through the whole image and make all pixels which are not of color blue to zero and others to white(255,255,255).

2) Convert to gray

3) Then do a cca.

Don't convert the image to gray scale right from start. You will loose the color information.

Here is the output of the algorithm

image description

This is the extraction of blue rectangles using inRange method

image description

This shows the green bounding boxes drawn over the original image

The code is given below.It is in C++

Mat image;
// read the input image
image = imread("D:\\work\\Forums\\opencv\\omr.png");

// get the blue rectangles
Mat blIm;
Scalar lower(180, 30,30);
Scalar upper(240, 100, 100);
inRange(image, lower, upper, blIm);
imshow("blue image", blIm);


//  apply a connected component analysis with statistics
//  (from opencv 3.0 onwards)
// cca
Mat labels, stats, centroids;
int numObjects = connectedComponentsWithStats(blIm, 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);
    int width = stats.at<int>(i, CC_STAT_WIDTH);
    int height = stats.at<int>(i, CC_STAT_HEIGHT);
    // draw rectangle
    rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);
}

imshow("BoundingBoxes", image);
edit flag offensive delete link more

Comments

Can you please provide sample code.that helps me more to understand. Thanks

Hardik Patel gravatar imageHardik Patel ( 2016-05-24 08:56:54 -0600 )edit

i understand all code but i dont know this part

int left = stats.at<int>(i, CC_STAT_LEFT); int top = stats.at<int>(i, CC_STAT_TOP); int width = stats.at<int>(i, CC_STAT_WIDTH); int height = stats.at<int>(i, CC_STAT_HEIGHT); // draw rectangle rectangle(image, Rect(left, top, width, height), Scalar(0,255,0), 2);

Hardik Patel gravatar imageHardik Patel ( 2016-05-25 01:20:19 -0600 )edit

sorry, no java here :-(

First question - That is exactly what i am doing in your second question. i get the top left coordinate of the detected rectangle plus its width and height and then use the "rectangle" draw method in opencv to draw the rectangle on the image with color green.

If you want to draw on new mat, just pass it as the first argument to rectangle method..

lama123 gravatar imagelama123 ( 2016-05-25 01:28:22 -0600 )edit

Yes Thanks.. i understand all thing and also converted to java..only one this i won't able to convert

int left= stats.at<int>(i, CC_STAT_LEFT);

Hardik Patel gravatar imageHardik Patel ( 2016-05-25 02:19:07 -0600 )edit

This is accessing the matrix stats of type int. i is the index and cc_stat_left is an opencv defined constant to get the top-left x position of the detected bounding box. Stats is the matrix filled by CCA. I hope in java you should have a similar method.

lama123 gravatar imagelama123 ( 2016-05-25 04:35:08 -0600 )edit

Thanks but i tried but doesn't get anything usefull. i done many RND but can't find.please help me anyone

thanks

Hardik Patel gravatar imageHardik Patel ( 2016-05-25 07:06:13 -0600 )edit

In fact you don't need to use connectedcomponentwithstats itself. You can instead apply find contours on blue image(blIm) and then get bounding boxes, since you already know how to use contours.

lama123 gravatar imagelama123 ( 2016-05-25 09:16:16 -0600 )edit

Thanks you very much..you save my day..you are great.i done it.just tell me one thing

Mat blIm; Scalar lower(180, 30,30); Scalar upper(240, 100, 100); inRange(image, lower, upper, blIm);

this code returns only blue rectangle right?

and thanks onces more

Hardik Patel gravatar imageHardik Patel ( 2016-05-26 00:54:41 -0600 )edit

Thanks to Open CV :-)

yes.. the method return a gray scale image with blue rectangles set to value 255(white) and all others to 0(black).

lama123 gravatar imagelama123 ( 2016-05-26 01:11:01 -0600 )edit

off-cource many thanks to Open CV..and one more thing when i m trying to detect filled circle with find contour it detect all circle and rectangle.but only one circle is not detected.

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        Imgproc.threshold(grayMat, grayMat, 100, 255, Imgproc.THRESH_BINARY);
        Core.bitwise_not(grayMat, grayMat);

        Imgproc.findContours(grayMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);

Thanks

Hardik Patel gravatar imageHardik Patel ( 2016-05-27 01:06:02 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-05-24 06:36:57 -0600

Seen: 6,490 times

Last updated: May 27 '16