如果我创建了一个白色图像并且我有第二个图像,如何在 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;
但它不起作用。
你快到了
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);