Ask Your Question
1

OpenCV crop function fatal signal 11

asked 2012-12-10 06:08:58 -0600

Silvestr gravatar image

updated 2020-10-05 06:39:14 -0600

Hello I am doing an android app which uses OpenCV to detect rectangles/squares, to detect them I am using functions (modified a bit) from squares.cpp. Points of every square found I am storing in vector<vector<point>> squares, then i pass it to the function which choose the biggest one and store it in vector<point> theBiggestSq. The problem is with the cropping function which code i will paste below (i will post the link to youtube showing the problem too). If the actual square is far enough from the camera it works ok but if i will close it a bit in some point it will hang. I will post the print screen of the problem from LogCat and there are the points printed out (the boundaries points taken from theBiggestSq vector, maybe it will help to find the solution).

void cutAndSave(vector<Point> theBiggestSq, Mat image){
    RotatedRect box = minAreaRect(Mat(theBiggestSq));
    // Draw bounding box in the original image (debug purposes)
    //cv::Point2f vertices[4];
    //box.points(vertices);
    //for (int i = 0; i < 4; ++i)
    //{
    //cv::line(img, vertices[i], vertices[(i + 1) % 4], cv::Scalar(0, 255, 0), 1, CV_AA);
    //}
    //cv::imshow("box", img);
    //cv::imwrite("box.png", img);
    // Set Region of Interest to the area defined by the box
    Rect roi;
    roi.x = box.center.x - (box.size.width / 2);
    roi.y = box.center.y - (box.size.height / 2);
    roi.width = box.size.width;
    roi.height = box.size.height;
    // Crop the original image to the defined ROI

    //bmp=Bitmap.createBitmap(box.size.width / 2, box.size.height / 2, Bitmap.Config.ARGB_8888);
    Mat crop = image(roi);
    //Mat crop = image(Rect(roi.x, roi.y, roi.width, roi.height)).clone();
    //Utils.matToBitmap(crop*.clone()* ,bmp);
    imwrite("/sdcard/OpenCVTest/1.png", bmp);
    imshow("crop", crop);

}

video of my app and its problems image description

cords printed respectively are: roi.x roi.y roi.width roi.height

Another problem is that the boundaries drawn should have a green colour but as you see on the video they are distorted (flexed like those boundaries would be made from glass?).

Thank you for any help. I am new in openCV doing it from only one month so please be tolerant.

edit retag flag offensive close merge delete

Comments

i recently deleted hundreds of unanswered questions due to they do not seem useful. although this problem answered many times i could not delete this one respecting quality of question.

sturkmen gravatar imagesturkmen ( 2020-10-04 08:52:13 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-10-04 08:19:05 -0600

you need to be sure if the Rect roi is inside the image bounds

roi = roi & Rect(0, 0, image.cols, image.rows);
Mat crop = image(roi);
edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-12-10 06:08:58 -0600

Seen: 883 times

Last updated: Oct 04 '20