15

I know that in OpenCV 2.1 we had a function to set ROI: cvSetImageROI(), but such a function does not exist in 2.4 (or at least I cant find it in its manuals and help section.)

however here is the only helpful code I could find which uses opencv 2.4 for mage ROI, but I am having trouble understanding it:

// define image ROI
cv::Mat imageROI;
imageROI= image(cv::Rect(385,270,logo.cols,logo.rows));
// add logo to image 
cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);

Here they want to add a very small log to a big image at the bottom right of the original image.

So what I understand from here is that another matrix is created to hold the ROI. Its dimensions given using the rect function, and size is given equal to that of the small logo they want to add.

Then thsi is what confuses me: cv::addWeighted(imageROI,1.0,logo,0.3,0.,imageROI); here the source 1 of addWeighted is the ROI dimensions set, source 2 is the logo and the destination is also the ROI dimensions set. Is this correct? or am I missing something?

After this the result is shown with the logo added to the big image. Where in these commands was the big image included.

Also before asking here I wanted to try the code myself to maybe help clarify the situation. but I get this error, as the image() is not recognized: 'image': identifier not found

int _tmain(int argc, _TCHAR* argv[])
{
Mat src1, imageROI, logo;

logo = imread("c:\\car1.jpg", -1);

imageROI= image(Rect(385,270,logo.cols,logo.rows));

addWeighted(imageROI,1.0,logo,0.3,0.,imageROI);


namedWindow("meh", CV_WINDOW_AUTOSIZE);
imshow("meh", imageROI);
waitKey(0);


return 0;

}

Jim G.
  • 15,141
  • 22
  • 103
  • 166
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73

1 Answers1

22
cv::Mat imageROI;
imageROI= image(cv::Rect(385,270,logo.cols,logo.rows));

The cv::Mat constructor wich takes a rectangle as a parameter:

Mat::Mat(const Mat& m, const Rect& roi)

returns a matrix that is pointing to the ROI of the original image, located at the place specified by the rectangle. so imageROI is really the Region of Interest (or subimage/submatrix) of the original image "image". If you modify imageROI it will consequently modify the original, larger matrix.

As for your example, the problem is that you are calling the constructor from an object which does not exists (image). You should replace:

imageROI= image(Rect(385,270,logo.cols,logo.rows));

by:

imageROI= src1(Rect(385,270,logo.cols,logo.rows));

assuming that src1 is your "big image" that you want to insert the logo into (the logo being car1.jpg). You should not forget to first read your big image, by the way!

remi
  • 3,914
  • 1
  • 19
  • 37
  • That is great. Thanks a lot. Thats the reason I was getting confused, I thought image was a function and did not find any mention of the big image. Thank you for clearing that out. Also one last question regarding this topic would be, how do I find the pixel position of the apex of the rectangle. I mean suppose I want to add my logo to the top left of src 1, then in the `src1(Rect(385,270,logo.cols,logo.rows));` what position should I give in place of the said 385,270? Is there an easy way to find this? Thank you – StuckInPhDNoMore Oct 03 '12 at 21:18
  • You can use the coordinates inside your ROI, that is 385,270 in the image corresponds to 0,0 in your ROI. This will work as long as you use the accessor of cv::Mat, like Mat::at(int row, int col). If you use the raw data directly, you have to pay attention that the actual data layout in the ROI is the data layout of the original image – remi Oct 04 '12 at 07:47