Setting ROI by 4 Points [closed]

asked 2015-04-23 08:46:54 -0600

Storiy gravatar image

updated 2015-04-23 11:47:34 -0600

berak gravatar image

I have a Point array (i have 4 coordinates at all) and need to set custom ROI by those. But all examples i was able to find only use 2 coordinates of an image. In addiction when i try to use cv::Point to set ROI, i am getting an error

error: cannot convert 'cv::Point' to 'int' for argument '1' to 'CvRect cvRect(int, int, int, int)'

Would be realy thankful for help, if someone can explain me how it works.


Sure, here it is:

cv::Point pointArray[4];
int i = 0;

cv::Mat ref = cv::imread("image.png");
cv::GaussianBlur(ref, ref, cv::Size(5, 5), 0, 0);
cv::Mat tpl = cv::imread("template1.png");
if (ref.empty() || tpl.empty())
    return -1;

cv::Mat gref, gtpl;
cv::cvtColor(ref, gref, CV_BGR2GRAY);
cv::cvtColor(tpl, gtpl, CV_BGR2GRAY);

cv::Mat res(ref.rows-tpl.rows+1, ref.cols-tpl.cols+1, CV_32FC1);
cv::matchTemplate(gref, gtpl, res, CV_TM_CCOEFF_NORMED);
cv::threshold(res, res, 0.8, 1., CV_THRESH_TOZERO);
while (true) 
{ 
       double minval, maxval, threshold = 0.8; cv::Point minloc, maxloc; cv::minMaxLoc(res, &minval, &maxval, &minloc, &maxloc); 

       if (maxval >= threshold) 
       {
               cv::floodFill(res, maxloc, cv::Scalar(0), 0, cv::Scalar(.1), cv::Scalar(1.));
               int x = maxloc.x + tpl.cols/2;
               int y = maxloc.y + tpl.rows/2;
               pointArray[i] = cv::Point(x,y);
               i++;
               cv::circle(ref, cv::Point(x, y), 2, CV_RGB(0, 0, 255));

       } else
               break;

  //  cvSetImageROI(ref, cvRect(pointArray[1],pointArray[2]));

}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-07 13:02:50.897832

Comments

1

Could you please write down your code to see what's wrong? Regarding this sentence: all examples i was able to find only use 2 coordinates of an image. To set a ROI you need 4 numbers, whether they're 2 pair of point coordinates, (x1, x2, y1, y2) or a point (top-left) and a width and height (x1, y1, w, h). What is exactly inside your array?

LorenaGdL gravatar imageLorenaGdL ( 2015-04-23 08:52:47 -0600 )edit
2

First of all, you shouldn't use the old C interface but the C++ one. Therefore, I would recommend not to use the cvSetImageROI function. Something as simple as this should work:

Rect r(10, 10, 100, 100);
Mat imgROI = img(r);

But I think your main problem is in understanding the Rect and Point structures and templates. Read the info about them here: http://docs.opencv.org/modules/core/d...

The error is because the cvRect was defined in the old C interface this way: CvRect cvRect(int x, int y, int width, int height) . Using the Rect template you will be able to directly use your Point structures

LorenaGdL gravatar imageLorenaGdL ( 2015-04-23 09:16:33 -0600 )edit
1

remember, that you can only have an axis-aligned roi.

then, keep a vector<Point> points(4) ,

and just get the Rect r = boundingRect(points); Mat imgRoi = img(r)

berak gravatar imageberak ( 2015-04-23 11:51:35 -0600 )edit

It helped a lot, thanks a lot both of you. Wish it was answers so i can mark them "correct"

Storiy gravatar imageStoriy ( 2015-04-24 07:22:31 -0600 )edit