将图像放在另一个图像上

时间:2014-03-31 07:12:34

标签: c++ opencv

如果我创建了白色图像并且我有第二张图像,如何在Opencv和c ++中将第二张图像放在白图像的中心?

我试过这个

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;

但它不起作用。

1 个答案:

答案 0 :(得分:3)

你几乎就在那里

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);
相关问题