Ask Your Question
0

Crop a Region of Interest; RotatedRect

asked 2018-05-14 04:40:00 -0600

dura gravatar image

void detect_text(string input){ Mat large = imread(input);

Mat rgb;
// downsample and use it for processing
pyrDown(large, rgb);
pyrDown(rgb, rgb);
Mat small;
cvtColor(rgb, small, CV_BGR2GRAY);
// morphological gradient
Mat grad;
Mat morphKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
morphologyEx(small, grad, MORPH_GRADIENT, morphKernel);
// binarize
Mat bw;
threshold(grad, bw, 0.0, 255.0, THRESH_BINARY | THRESH_OTSU);
// connect horizontally oriented regions
Mat connected;
morphKernel = getStructuringElement(MORPH_RECT, Size(9, 1));
morphologyEx(bw, connected, MORPH_CLOSE, morphKernel);
// find contours
Mat mask = Mat::zeros(bw.size(), CV_8UC1);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(connected, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
// filter contours
for(int idx = 0; idx >= 0; idx = hierarchy[idx][0]){
    Rect rect = boundingRect(contours[idx]);
    Mat maskROI(mask, rect);
    maskROI = Scalar(0, 0, 0);
    // fill the contour
    drawContours(mask, contours, idx, Scalar(255, 255, 255), CV_FILLED);

    RotatedRect rrect = minAreaRect(contours[idx]);
    double r = (double)countNonZero(maskROI) / (rrect.size.width * rrect.size.height);

    Scalar color;
    int thickness = 1;
    // assume at least 25% of the area is filled if it contains text
    if (r > 0.25 &&
    (rrect.size.height > 8 && rrect.size.width > 8) // constraints on region size
    // these two conditions alone are not very robust. better to use something
    //like the number of significant peaks in a horizontal projection as a third condition
    ){
        thickness = 2;
        color = Scalar(0, 255, 0);

    }
    else
    {
        thickness = 1;
        color = Scalar(0, 0, 255);
    }


    Point2f pts[4];
    rrect.points(pts);
    for (int i = 0; i < 4; i++)
    {
        line(rgb, Point((int)pts[i].x, (int)pts[i].y), Point((int)pts[(i+1)%4].x, (int)pts[(i+1)%4].y), color, thickness);

    }

}

imwrite("contdd.jpg", rgb);

}

Hi I have a question about cropping an image with square detection/text recognition function applied to the image. Here is the result image image description. However, I want to save each text wrapped within the green squares individually like this: image description

Can anyone please help me with this?

edit retag flag offensive close merge delete

Comments

May I ask what you are working on? I am currently working on a similar project as well. I am trying to record whiteboard images.

KaanCidar gravatar imageKaanCidar ( 2018-05-15 03:14:21 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2018-05-14 06:10:38 -0600

berak gravatar image

updated 2018-05-14 20:46:03 -0600

you can't crop & save the rotatedrect directly (it's not axis-aligned), but you can save it's boundingRect:

Rect r = rrect.boundingRect();
Rect bounds(0,0,rgb.cols,rgb.rows);
Mat crop = rgb(r & bounds);  // make sure, we stay inside the image
imwrite(format("letters_%04d.png", idx), crop);
edit flag offensive delete link more

Comments

1

I tried this but it gave me this error:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat, file /tmp/opencv-3.2.0/modules/core/src/matrix.cpp, line 522 terminate called after throwing an instance of 'cv::Exception' what(): /tmp/opencv-3.2.0/modules/core/src/matrix.cpp:522: error: (-215) 0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows in function Mat Could you please kindly explain why this happens?

Thank you !

dura gravatar imagedura ( 2018-05-14 20:28:22 -0600 )edit

apologies, my bad.

i forgot, that rotated rects can be partly outside the image region, it's even in the docs.

please see update.

berak gravatar imageberak ( 2018-05-14 20:44:05 -0600 )edit
1

Thank you so much ! You saved my life! Hope you have a nice day:)

dura gravatar imagedura ( 2018-05-14 20:48:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-05-14 04:40:00 -0600

Seen: 2,418 times

Last updated: May 14 '18