4

I work on Win7 x64, with openCV and Visual Studio 2010, programming in c++. I want copying an image (call it image) to a rectangular area of another image (call it RR_image). This area, however, is rotated.

here is image:

enter image description here

and here is RR_image: enter image description here (as you see, I've already rotated first image)

I would copy first image in red rectangle.

How many and what ways are there to do this?

I know about ROI and: img1.copyTo(img2.rowRange(...), img2.colRange(...));

Thanks!

Cristina1986
  • 505
  • 1
  • 8
  • 21
  • See related: http://stackoverflow.com/questions/22786072/is-it-possible-to-copy-a-rotated-image-into-a-rotatedrect-roi-of-another-image-w – ThomasW Oct 02 '14 at 09:26

2 Answers2

2

The main problem is that OpenCV doesn't support transparency in images, which is what you need to accomplish that task in an easy way.

So, one way to do it is to write a custom function/method to iterate over the rotated (source) image copying the pixels to the destination image, while ignoring the background pixels (a specific shade of gray, for instance). This approach is very limited, though.

EDIT:

OpenGL can certainly do the job, but it brings a complexity that you probably don't want on your hands right know. Fortunately there are other alternatives like ImakeMagick, which provides a C++ API through ImageMagick++ and it can certainly handle transparent images. Take a look at A Gentle Introduction to Magick++.

The QImage class from Qt can probably do this as well.

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • I read on Internet that OpenCV does not support transparency.. too bad! So should I look for other libraries to copy an image to another and have transparency? like OpenGL? – Cristina1986 Jan 24 '13 at 20:22
  • Thank you for your answer! I'll search a solution with QT or ImakeMagick! But, Suppose that I solve transparency problem, then how can I copy my image in the red rectangule? I cannot use Roi, right? – Cristina1986 Jan 24 '13 at 20:56
  • You will have to use Qt or ImageMagick to do the copying itself, that's the point. After the copy has been done, then you can convert the resulting data back to a `cv::Mat`. – karlphillip Jan 24 '13 at 23:12
2

I solved my problem with Qt library and OpenCV.

Here how:

Given image and RR_image in order to copy the former into the latter, I used this code:

QPainter painter(&RR_image);
painter.drawImage( (float)p1.x(),(float)p2.y(), image);

RR_image and image must be a QImage object, so I have to covert them from Mat to QImage with this code:

cvtColor(image, image,CV_BGR2RGB);
QImage image= QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_RGB888);

The result is: enter image description here

Hope this will help someone!

Community
  • 1
  • 1
Cristina1986
  • 505
  • 1
  • 8
  • 21