6

我正在尝试混合两个图像。如果它们具有相同的大小,这很容易,但如果其中一个图像更小或更大,则 cv::addWeighted 失败。

图像 A(预计更大) 图像 B(预计更小)

我试图创建一个 ROI - 试图创建 A 大小的第三个图像并在其中复制 B - 我似乎无法正确处理。请帮忙。

double alpha = 0.7; // something
int min_x = ( A.cols - B.cols)/2 );
int min_y = ( A.rows - B.rows)/2 );
int width, height;
if(min_x < 0) {
  min_x = 0; width = (*input_images).at(0).cols - 1;
}
else         width = (*input_images).at(1).cols - 1;
if(min_y < 0) {
  min_y = 0; height = (*input_images).at(0).rows - 1;
}
else         height = (*input_images).at(1).rows - 1;
cv::Rect roi = cv::Rect(min_x, min_y, width, height);            
cv::Mat larger_image(A);
// not sure how to copy B into roi, or even if it is necessary... and keep the images the same size
cv::addWeighted( larger_image, alpha, A, 1-alpha, 0.0, out_image, A.depth());

甚至像 cvSetImageROI 这样的东西 - 可能有效,但我找不到 c++ 等价物 - 可能有帮助 - 但我不知道如何使用它来保持图像内容,只在 ROI 内放置另一个图像......

4

2 回答 2

15
// min_x, min_y should be valid in A and [width height] = size(B)
cv::Rect roi = cv::Rect(min_x, min_y, B.cols, B.rows);  

// "out_image" is the output ; i.e. A with a part of it blended with B
cv::Mat out_image = A.clone();

// Set the ROIs for the selected sections of A and out_image (the same at the moment)
cv::Mat A_roi= A(roi);
cv::Mat out_image_roi = out_image(roi);

// Blend the ROI of A with B into the ROI of out_image
cv::addWeighted(A_roi,alpha,B,1-alpha,0.0,out_image_roi);

注意,如果你想B直接混合成A,你只需要roi.

cv::addWeighted(A(roi),alpha,B,1-alpha,0.0,A(roi));
于 2013-07-23T23:40:22.647 回答
-4

addWeighted()您可以使用函数轻松混合两个图像

addWeighted(src1, alpha, src2, beta, 0.0, dst);

声明两个图像

src1 = imread("c://test//blend1.jpg");
src2 = imread("c://test//blend2.jpg");

声明 and 的值,alpha然后beta调用函数。你完成了。您可以在链接中找到详细信息:使用 Opencv 混合图像

于 2016-06-25T08:11:11.760 回答