2012-01-17 588 views
9

我試圖運用Canny算子與下面的代碼的圖像的特定位置:的OpenCV - 輸入參數大小不匹配 - addWeighted

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

你有什麼樣的問題可能是什麼建議嗎? 我一直停留在這個很長一段時間......

謝謝。

+0

這行特別引發錯誤? - 不用擔心,我看到它是'加權'。 – 2012-01-17 01:42:07

+0

@ mathematical.coffee addWeighted,編輯該問題。謝謝。 – mrcaramori 2012-01-17 01:45:28

回答

9

簡單。兩個圖像中沒有相同數量的通道要合併。

cvtColor(devilROI, canny, CV_RGB2GRAY); 

正在將您的3通道圖像變成1通道灰度圖像。您需要使用相同的通道數的addWeighted

2

好的,我想我明白了。

我嘗試使用墊:: 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); 

因此,如果任何人有任何其他建議,我會感激。

謝謝!

+0

python代碼如何? – Allan 2015-10-08 01:48:51