Ask Your Question

overlay smaller image over bigger image in a particular area

asked 2014-08-21 04:38:05 -0500

borjaevo gravatar image


I'm trying to overlay a smaller image into bigger image in a concrete area. With images of same size i have no problems, but when call to add function size and type of both images have to be the same. Other problem is, first of all i black out the area where i will paste the other image but this is not very efficient. I would like to know if are there another ways to do it.

Thanks and sorry for my english.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2014-08-21 04:50:18 -0500

berak gravatar image

if smallImg and bigImg have the same format, and you're sure it 'fits' at the given position, try:

 smallImg.copyTo( bigImg.submat( y, smallImg.rows(), x, smallImg.cols() ) );
edit flag offensive delete link more


Thank you! i'll try

borjaevo gravatar imageborjaevo ( 2014-08-21 04:52:53 -0500 )edit

answered 2020-06-30 15:03:31 -0500

copyTo is the correct approach, however be mindful that the function will fail without warning in many cases. A common pitfall is if you try to copy an RGBa image into GBR or vice versa

var selectedArea = RGBaImgToDisplay.submat(resultRect)

will not work, unless you first make sure that sampleImageColor is also an RGBa like so:

cvtColor(bgrImage, sampleImageColor, Imgproc.COLOR_BGR2RGBA);

Then it works as expected

edit flag offensive delete link more

Question Tools


Asked: 2014-08-21 04:38:05 -0500

Seen: 3,055 times

Last updated: Aug 21 '14