2012-01-17 8 views
9

Sto cercando di applicare l'operatore Canny in una determinata posizione di un'immagine con il seguente codice:OpenCV - Misure di argomenti di input non corrispondono - 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); 

E mi sta dando il seguente errore quando l'addWeighted viene eseguito:

 
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 

Avete qualche suggerimento su quale potrebbe essere il problema? Sono stato bloccato su questo a lungo ...

Grazie.

+0

quale riga in particolare genera l'errore? - Non preoccuparti, vedo che è 'addWeighted'. –

+0

@ mathematical.coffee addWeighted, ha modificato la domanda. Grazie. – mrcaramori

risposta

9

Facile. Non hai lo stesso numero di canali nelle 2 immagini da unire.

cvtColor(devilROI, canny, CV_RGB2GRAY); 

Sta prendendo l'immagine a 3 canali e trasformandola in un'immagine in scala di grigi a 1 canale. È necessario lo stesso numero di canali per utilizzare aggiungiPeso Peso

2

Ok, penso di averlo capito.

Ho provato ad utilizzare il Mat :: copyTo, poi ho avuto il: errore di

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

.

poi ho trovato this argomento Stackoveflow, che mi ha dato l'idea di riconversione RGB, poi ho provato la seguente e ha funzionato:

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); 

Quindi, se qualcuno ha qualche altro suggerimento, sarei grato.

Grazie!

+0

come circa il codice Python ?? – Allan

Problemi correlati