OpenCV - Sizes of input arguments do not match - addWeighted

前端 未结 2 1441
失恋的感觉
失恋的感觉 2021-01-07 22:46

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(         


        
相关标签:
2条回答
  • 2021-01-07 23:28

    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

    0 讨论(0)
  • 2021-01-07 23:39

    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!

    0 讨论(0)
提交回复
热议问题