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

标签 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

您对可能出现的问题有什么建议吗? 这个问题我纠结了好久……

谢谢。

最佳答案

简单。两张图片中要合并的 channel 数不同。

cvtColor(devilROI, canny, CV_RGB2GRAY);

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

关于c++ - OpenCV - 输入参数的大小不匹配 - addWeighted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8888388/

相关文章:

C++11 字符串属性和 gcc 版本

c++ - 如何使用 C++ uniform_int_distribution 进行无替换采样

python - 如何使用 OpenCV imshow 在没有键盘反馈的情况下循环显示最新图像

java - 相机预览给出灰色图像

python - 如何找到模板和旋转图像之间的正确点对应关系?

python - 为什么Numpy的RGB图像阵列具有4层而不是3层?

c++ - 汇编如何做参数传递: by value,引用,不同类型/数组的指针?

c++ - 构建 cpp-netlib 失败 - 未添加 lib 前缀

c++ - is_constant_evaluated() 应该产生 constexpr 变量?

python - 无法在 matplotlib 中显示正常图像,它一直使用 jet colormap 显示