使用opencv复制/混合不同大小的图像 [英] Copy / blend images of different sizes using opencv

查看:303
本文介绍了使用opencv复制/混合不同大小的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to blend two images. It is easy if they have the same size, but if one of the images is smaller or larger cv::addWeighted fails.

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

Image A (expected to be larger) Image B (expected to be smaller)

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

I tried to create a ROI - tried to create a third image of the size of A and copy B inside - I can't seem to get it right. Please help.

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中放置另一个图像...

Even something like cvSetImageROI - may work but I can't find the c++ equivalent - may help - but I don't know how to use it to still keep the image content, only place another image inside ROI...

推荐答案

// 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

Note that if you want to blend B directly into A, you just need roi.

cv::addWeighted(A(roi),alpha,B,1-alpha,0.0,A(roi));

这篇关于使用opencv复制/混合不同大小的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆