Hiro
Hiro

Reputation: 83

Perfoming Image filtering with OpenCV & C++, error : "Sizes of input arguments do not match"

Here's how I call my image and define my button :

img = imread("lena.jpg");

createButton("Show histogram", showHistCallback, NULL, QT_PUSH_BUTTON, 0);
createButton("Equalize histogram", equalizeCallback, NULL, QT_PUSH_BUTTON, 0);
createButton("Cartoonize", cartoonCallback, NULL, QT_PUSH_BUTTON, 0);

imshow("Input", img);
waitKey(0);
return 0;

I can call and show my image properly. Function Show histogram and equalize histogram also work properly. But when I tried to call Cartoonize, I got this error :

[ WARN:0] global /home/hiro/Documents/OpenCV/opencv-4.3.0-source/modules/core/src/matrix_expressions.cpp (1334) 
assign OpenCV/MatExpr: processing of multi-channel arrays might be changed in the future: https://github.com/opencv/opencv/issues/16739
terminate called after throwing an instance of 'cv::Exception'
what():OpenCV(4.3.0) /home/hiro/Documents/OpenCV/opencv-4.3.0-source/modules/core/src/arithm.cpp:669: 
error: (-209: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 function 'arithm_op'

So I'm guessing my error comes from CartoonCallback function, channel error. I have made sure that my mutiplication is between image of same channels, I converted everything back to 3 channels, yet I can't seem to figure out where the error comes from. Here's the code :

void cartoonCallback(int state, void* userdata){
    Mat imgMedian;
    medianBlur(img, imgMedian, 7);

    Mat imgCanny; 
    Canny(imgMedian, imgCanny, 50, 150); //Detect edges with canny
    Mat kernel = getStructuringElement (MORPH_RECT, Size(2,2));
    dilate(imgCanny, imgCanny, kernel); //Dilate image

    imgCanny = imgCanny/255;
    imgCanny = 1 - imgCanny;

    Mat imgCannyf; //use float values to allow multiply between 0 and 1
    imgCanny.convertTo(imgCannyf, CV_32FC3);
    blur(imgCannyf, imgCannyf, Size(5,5));

    Mat imgBF; 
    bilateralFilter(img, imgBF, 9, 150.0, 150.0); //apply bilateral filter
    Mat result = imgBF/25; //truncate color
    result = result*25;

    Mat imgCanny3c; //Create 3 channels for edges
    Mat cannyChannels[] = {imgCannyf, imgCannyf, imgCannyf};
    merge(cannyChannels, 3, imgCanny3c);

    Mat resultFloat; 
    result.convertTo(imgCanny3c, CV_32FC3); //convert result to float
    multiply(resultFloat, imgCanny3c, resultFloat);

    resultFloat.convertTo(result, CV_8UC3); //convert back to 8 bit
    imshow("Cartoonize", result);
}

Any suggestion ?

Upvotes: 0

Views: 612

Answers (1)

stateMachine
stateMachine

Reputation: 5815

The problem is within this snippet:

cv::Mat resultFloat; // You prepare an output mat... with no dimensions nor type
result.convertTo(imgCanny3c, CV_32FC3); //convert result to float..ok
cv::multiply(resultFloat, imgCanny3c, resultFloat); //resultFloat is empty and has no dimensions!

As you can see, you pass resultFloat to cv::multiply(operand1, operand2, output), but resultFloat is empty, without dimensions nor type and then attempt to multiply it with imgCanny3c. This seems the cause of the error.

Upvotes: 2

Related Questions