Steph
Steph

Reputation: 639

Place image onto another image

If I created a white image and I have a second image, how do I place the 2nd image in the center of the white image in Opencv and c++?

I tried this

int x = 10,
y = 20, 
width = 200,
height = 200;
white= face_region(Rect(x, y, width, height));
face_region.copyTo(white);
imshow("white",white);
waitKey(0);
return 0;

but it does not work.

Upvotes: 0

Views: 118

Answers (1)

Sergei Nosov
Sergei Nosov

Reputation: 1675

You were almost there

Mat white(768, 1024, CV_8UC3);
Mat red(100, 100, CV_8UC3);
Mat blue(100, 100, CV_8UC3);

white.setTo(Scalar(255, 255, 255));
red.setTo(Scalar(0, 0, 255));
blue.setTo(Scalar(255, 0, 0));

// this code places red image to the center square
red.copyTo(white(Rect(white.cols/2 - red.cols/2,
                      white.rows/2 - red.rows/2,
                      red.cols,
                      red.rows)));

// this code places blue image to the center circle
Mat circle_mask(blue.size(), CV_8UC1);
cv::circle(circle_mask, Point(circle_mask.cols/2, circle_mask.rows/2), 50, Scalar(255), CV_FILLED);
blue.copyTo(white(Rect(white.cols/2 - blue.cols/2,
                       white.rows/2 - blue.rows/2,
                       blue.cols,
                       blue.rows)), circle_mask);

cv::imshow("image", white);
waitKey(0);

Upvotes: 3

Related Questions