OpenCV - 输入参数的大小不匹配 - addWeighted

时间:2012-01-17 00:50:49

标签: c++ opencv

我正在尝试使用以下代码在图像的某个位置应用Canny运算符:

//region of interest from my RGB image
Mat devilROI = img(Rect(r->x+lowerRect.x, 
                        r->y + lowerRect.y, 
                        lowerRect.width, 
                        lowerRect.height));
Mat canny;
//to grayscale so I can apply canny
cvtColor(devilROI, canny, CV_RGB2GRAY);
//makes my region of interest with Canny
Canny(canny, canny, low_threshold, high_threshold);
//back to the original image
addWeighted(devilROI, 1.0, canny, 0.3, 0., devilROI);

执行addWeighted时,它给出了以下错误:

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array') in arithm_op, file C:\OpenCV2.3\ opencv\modules\core\src\arithm.cpp, line 1227
terminate called after throwing an instance of 'cv::Exception'
what():  C:\OpenCV2.3\opencv\modules\core\src\arithm.cpp:1227: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function arithm_op

您对问题可能是什么有任何建议吗? 我已经坚持了很长时间......

谢谢。

2 个答案:

答案 0 :(得分:10)

易。您要合并的2张图片中没有相同数量的频道。

cvtColor(devilROI, canny, CV_RGB2GRAY);

拍摄3通道图像并将其转换为1通道灰度图像。您需要使用相同数量的频道才能使用addWeighted

答案 1 :(得分:3)

好的,我想我明白了。

我尝试使用Mat :: copyTo,然后我得到了:

 (scn ==1 && (dcn == 3 || dcn == 4))

错误。

然后我找到了this Stackoveflow主题,它给了我转换回RGB的想法,然后我尝试了以下它并且它起作用了:

Mat devilROI = img(Rect(r->x+lowerRect.x, 
                        r->y + lowerRect.y, 
                        lowerRect.width, 
                        lowerRect.height));
Mat canny;
cvtColor(devilROI, canny, CV_BGR2GRAY);
Canny(canny, canny, low_threshold, high_threshold);
cvtColor(canny, canny, CV_GRAY2BGR);
addWeighted(devilROI, 1.0, canny, 0.3, 0., devilROI);

所以,如果有人有任何其他建议,我将不胜感激。

谢谢!