Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV draw an image over another image

Tags:

image

opencv

Is there an OpenCV function to draw an image over another image? I have one big image of Mat type. And I have a small image of Mat type (5x7). I want to draw this small image over the big image at specified coordinates.

like image 579
vitaly Avatar asked Jun 12 '12 06:06

vitaly


People also ask

How do I put one image on top of another in OpenCV?

How do I put one image on top of another in OpenCV? You can add two images with the OpenCV function, cv. add(), or simply by the numpy operation res = img1 + img2. Both images should be of same depth and type, or the second image can just be a scalar value.


2 Answers

Use Mat::rowRange() and Mat::colRange() to specify the area to which you want to draw in the destination Mat. Code:

Mat src( 5,  7, CV_8UC1, Scalar(1)); // 5x7 Mat dst(10, 10, CV_8UC1, Scalar(0)); // 10x10  src.copyTo(dst.rowRange(1, 6).colRange(3, 10)); 

Results in the following:

before copyTo():

dst:     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 ) 

after copyTo():

dst:     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 1 1 1 1 1 1 1 )     ( 0 0 0 1 1 1 1 1 1 1 )     ( 0 0 0 1 1 1 1 1 1 1 )     ( 0 0 0 1 1 1 1 1 1 1 )     ( 0 0 0 1 1 1 1 1 1 1 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 )     ( 0 0 0 0 0 0 0 0 0 0 ) 
like image 148
Eran W Avatar answered Sep 22 '22 09:09

Eran W


Create a Region Of Interest within the big image and then copy the small image to that region:

cv::Rect roi( cv::Point( originX, originY ), cv::Size( width, height )); cv::Mat destinationROI = bigImage( roi ); smallImage.copyTo( destinationROI ); 

If you are certain the small image fits into the big image then you could simply do:

cv::Rect roi( cv::Point( originX, originY ), smallImage.size() ); smallImage.copyTo( bigImage( roi ) ); 
like image 22
Rodrigo Avatar answered Sep 25 '22 09:09

Rodrigo