Mercury
Mercury

Reputation: 1965

Getting the source ROI coordinates of a cv::Mat

in opencv there is no difference between an image and a ROI of an image. a cv::Mat object can be either:

cv::Mat img = cv::imread(fileName);
cv::Mat imgROI(img,cv::Rect(1,2,30,40));

My question is how can I extract the original ROI coordinates in the original image, i.e. some function that preforms the following:

cv::Rect originalROIcoords  = roiLocationInOriginalImg(imgROI);
cv::Rect originalROIcoords2 = roiLocationInOriginalImg(img );

originalROIcoords should be (1,2,30,40), while originalROIcoords2 should be (0,0,w,h), where w and h are the original image width and height respectively.

Thanks.

Ohad

Upvotes: 2

Views: 5742

Answers (2)

xyz
xyz

Reputation: 101

Be carefull with ROI of ROI because locateROI returns offset related to root image and it's not related to ROI parent

cv::Mat img(100,100,CV_8UC1);               // the root image
cv::Mat imgROI(img,cv::Rect(10,10,60,60));  // a ROI in the root image
cv::Mat roiROI(imgROI,cv::Rect(5,5,30,30)); // a ROI into a ROI

Point offset;
Size wholesize;

Get the offset of 1st ROI:

imgROI.locateROI(wholesize,offset);
cout << "imgRoi Offset: " << offset.x <<","<< offset.y << endl;

imgRoi Offset: 10,10

Get the offset of roi of ROI:

roiROI.locateROI(wholesize,offset);
cout << "roiRoi Offset: " << offset.x <<","<< offset.y << endl;

roiRoi Offset: 15,15

same is for wholesize

Upvotes: 10

&#201;tienne
&#201;tienne

Reputation: 4994

To know the coordinates of a submatrix in the original matrix, you can use the function Mat::locateROI

// locates matrix header within a parent matrix
void locateROI( Size& wholeSize, Point& ofs ) const;

Upvotes: 2

Related Questions