10

I would like to do a very simple thing: copy an area inside an image into a new area in a new image. In the OpenCV 2.3 cheatsheet, they suggest the following solution:

"Example 3. Copy image ROI to another image with conversion"

Rect r(1, 1, 10, 20);
Mat dstroi = dst(Rect(0,10,r.width,r.height));
src(r).convertTo(dstroi, dstroi.type(), 1, 0);

My code is the following:

Mat frameO, frameS;

original >> frameO;
stabilized >> frameS;

Mat output(frameO.rows+40, frameO.cols*2+60, CV_32FC3);
output.setTo(0);            
Rect r(0,0, frameO.cols, frameO.rows);
Mat destROI = output(Rect(20,20, frameO.cols, frameO.rows));
frameO(r).copyTo(destROI);

I just want to copy the image frameO in output at the location Rect(20,20, frameO.cols, frameO.rows).
Anyone can tell me why this is not working?

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Tulkkas
  • 973
  • 3
  • 10
  • 22
  • The code fragment you provide is incomplete. Could you please post the whole code, including the operation where you actually perform the copy. – ypnos Apr 13 '12 at 09:03
  • possible duplicate of [How to set ROI in OpenCV?](http://stackoverflow.com/questions/8206466/how-to-set-roi-in-opencv) – karlphillip Apr 13 '12 at 13:00
  • and also a possible duplicate of http://stackoverflow.com/questions/6566295/opencv-c-getting-region-of-interest-roi-using-cvmat – karlphillip Apr 13 '12 at 13:00
  • Sorry i missed the last line, i just edited it. – Tulkkas Apr 13 '12 at 15:20
  • Regarding your Comment karlphilip, in fact the question there was not answered. When doing frameO(r).copyTo(destROI); the image is well copied to destROI. If i display it, it will contain the new image but it does not affect output which where I want the image to end up. – Tulkkas Apr 13 '12 at 15:23
  • Do you see any reasons why it is not working? – Tulkkas Apr 19 '12 at 07:08

2 Answers2

18

Actually these commands were not working in OpenCV 2.3 but now the following works fine with the 2.4 release:

Mat frame1 = imread(nameReading1);

Mat output(frame1.rows*2, frame1.cols*2, frame1.type());
output.setTo(0);

frame1.copyTo(output(Rect(0, 0, frame1.cols, frame1.rows)));

This will copy frame1 in output as long as the type agree so be careful when you create output. frame1 will be copied in a ROI in output defined by Rect(0, 0, frame1.cols, frame1.rows).

Angie Quijano
  • 4,167
  • 3
  • 25
  • 30
Tulkkas
  • 973
  • 3
  • 10
  • 22
1

An alternative method for the same process is to use WarpAffine function.

Since you already know the ROI, you can create a sub MAT to store the temporary data and paste it in a new image of any desired size.

 Mat tempImage = imageTest.SubMat(rowStart, rowEnd, colStart, colEnd);
 Mat targetImage = new Mat(rowEnd - rowStart, colEnd - colStart, MatType.CV_64FC3);
 tempImage.CopyTo(targetImage);

Now you can paste the contents onto a new image as follows:

double[,] transMatDynamicScaling = new double[2, 3] { { 1, 0, desiredROW }, { 0, 1, desiredCol } }; Mat transformationMAT = new Mat(2, 3, MatType.CV_64FC1, transMatDynamicScaling);

Finally you can use warpfunction to paste in newImage (Remember to create a new image in memory)

Cv2.WarpAffine(targetImage, addImage1, transformDynamicScaling, addImage1.Size());

I know I am late for the game, maybe it will be useful for future users.

dcruise546
  • 58
  • 8