I am trying to apply the Canny operator in a certain location of an image with the following code:
//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:
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.
Easy. You do not have the same number of channels in the 2 images to merge.
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
Ok, I think I got it.
I tried using the Mat::copyTo, then I got the:
(scn ==1 && (dcn == 3 || dcn == 4))
error.
Then I found this Stackoveflow topic, which gave me the idea of converting back to RGB, then I tried the following and it worked:
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With