C++ 使用opencv复制/混合不同大小的图像
我试图混合两个图像。如果它们的大小相同,这很容易,但如果其中一个图像较小或较大,cv::addWeighted将失败 图像A(预计更大) 图B(预计更小) 我试图创建一个投资回报率-试图创建一个a大小的第三个图像,并在其中复制B-我似乎做不好。请帮忙C++ 使用opencv复制/混合不同大小的图像,c++,opencv,image-processing,C++,Opencv,Image Processing,我试图混合两个图像。如果它们的大小相同,这很容易,但如果其中一个图像较小或较大,cv::addWeighted将失败 图像A(预计更大) 图B(预计更小) 我试图创建一个投资回报率-试图创建一个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;
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());
double alpha=0.7;//某物
int min_x=(A.cols-B.cols)/2);
int min_y=(A.rows-B.rows)/2);
int宽度、高度;
如果(最小值x<0){
min_x=0;width=(*输入_图像).at(0).cols-1;
}
else width=(*输入_图像).at(1).cols-1;
如果(最小值小于0){
最小值y=0;高度=(*输入图像).at(0).rows-1;
}
else height=(*输入_图像).at(1).行-1;
cv::Rect roi=cv::Rect(最小x,最小y,宽度,高度);
cv::Mat大图(A);
//不知道如何将B复制到roi中,或者即使有必要。。。并保持图像大小不变
cv::addWeighted(较大的图像,alpha,A,1-alpha,0.0,out_图像,A.depth());
甚至像CVSETIGEGROI一样的东西可能会工作,但我找不到C++等价物——可能会帮助——但是我不知道如何使用它来保持图像内容,只在ROI……/P>中放置另一个图像。
// 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));
您可以使用
addWeighted()
函数轻松混合两个图像
addWeighted(src1, alpha, src2, beta, 0.0, dst);
声明两个图像
src1 = imread("c://test//blend1.jpg");
src2 = imread("c://test//blend2.jpg");
声明
alpha
和beta
的值,然后调用该函数。你完了。您可以在链接中找到详细信息:您希望out\u image
是(1)与B
混合的a
的裁剪部分(因此B
的大小)还是(2)与B
混合的选定部分的a
的大小(因此a
)?与BWhy混合的选定截面的A-尺寸为width=(*input_images).at(0).cols-1
?为什么不(*input_images).at(0).cols
?@Thalia,您能告诉我您在if
语句中为高度和宽度指定的*input_images
是什么吗?我仍然得到了与我一直得到的相同错误:输入参数的大小不匹配(操作既不是'array op array'(其中数组具有相同的大小和相同的通道数),也不是未知函数中的“数组op scalar op array”…\arithm.cpp,第1277Try行cv::Rect roi=cv::Rect(minu x,minu y,B.cols,B.rows)
它有点工作了-谢谢-除了结果是图像A的大小调整到图像B的大小-并且两者混合在一起。我用A(roi)作为输出这很完美!谢谢!我现在对roi有了更好的理解。@Jacob很好的解释。谷歌把我带到这里。:D