C++ OpenCV - 输入参数的大小不匹配 - addWeighted
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8888388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenCV - Sizes of input arguments do not match - addWeighted
提问by mrcaramori
I am trying to apply the Canny operator in a certain location of an image with the following code:
我正在尝试使用以下代码在图像的某个位置应用 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);
And it is giving me the following error when the addWeighted is executed:
执行 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
Do you have any suggestion of what the problem might be? I've been stuck on this for a long time...
您对可能出现的问题有什么建议吗?我已经被困在这个问题上很长时间了......
Thank you.
谢谢你。
回答by john ktejik
Easy. You do not have the same number of channels in the 2 images to merge.
简单。您在要合并的 2 个图像中没有相同数量的通道。
cvtColor(devilROI, canny, CV_RGB2GRAY);
Is taking your 3 channel image and turning it into a 1 channel greyscale image. You need the same number of channels to use addWeighted
正在拍摄您的 3 通道图像并将其转换为 1 通道灰度图像。您需要相同数量的通道才能使用 addWeighted
回答by mrcaramori
Ok, I think I got it.
好的,我想我明白了。
I tried using the Mat::copyTo, then I got the:
我尝试使用 Mat::copyTo,然后我得到了:
(scn ==1 && (dcn == 3 || dcn == 4))
error.
错误。
Then I found thisStackoveflow topic, which gave me the idea of converting back to RGB, then I tried the following and it worked:
然后我找到了这个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);
So, if anyone has any other suggestion, I would be grateful.
所以,如果有人有任何其他建议,我将不胜感激。
Thank you!
谢谢!