OpenCV Error: Sizes of input arguments do not match when resize

asked 2017-04-15 03:50:53 -0500

Tom James gravatar image

updated 2017-04-29 03:09:44 -0500

I want to modify the size of the image if the input image(img and pattern)is too big. The input images are img which is the picture including objects and background and the pattern which is the picture of the background. After changing the size, I use pattern to subtract img, which is expected to remove the background of the picture. However, I received "OpenCV Error: Sizes of input arguments do not match ". While, if I do not change the size and just conduct the subtraction the error won't emerge. So, I suspect that there is something wrong with my code which is used to change the size of the input picture. Can you help me find it out?

There is something need to point out and that is I make the program reveal the size of the pictures before and after the resize activity and it seemed to word, but the error is still there and the output image that contain objects only can't be shown. Here is may code which is used to change the size of the p[icture:

Mat removeLight(Mat img, Mat pattern, int method);

int main(int argc,char*argv[])
    Mat img=imread(argv[1],0);//input
    Mat pattern= imread(argv[2],0);//background
    //Mat pattern=255*Mat::ones(img.rows,img.cols,CV_8UC1);
    Mat objects;//objects without background
    int method;//which way to remove the background
    else if(!strcmp(argv[3],"division"))
        cout<<"The input is wrong!"<<endl;
        return 0;
    objects=removeLight(img, pattern, method);
    imshow("image without background",objects);


Mat removeLight(Mat img, Mat pattern, int method)
    Mat aux;
    aux= Mat::ones(img.cols,img.rows,CV_8UC1);
    //if method is nomalization
        //require change our image to 32 float for division
        Mat img32, pattern32;
        //Divide the image by the pattern
        aux= 1-(img32/pattern32);
        //Scake it to convert to 8biit format
        //convert 8 bits format
        aux.convertTo(aux, CV_8UC1);
        Mat mask= img<=pattern;
    return aux;

The hint in the terminal revealed :

rows:4160 cols3120

rows:416 cols312


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 type), nor 'array op scalar', nor 'scalar op array') in compare, file /home/haoxuan/_SOFT/opencv-3.2.0/modules/core/src ... (more)

edit retag flag offensive close merge delete



the error does not come from the resizing code you show.

could you add the lines, that throw the actual error ?

berak gravatar imageberak ( 2017-04-15 04:08:20 -0500 )edit

I'm sorry for adding the rest of the codes after so long time for I was engaged in my study. Now I have added all of the lines of my code. Can you see where it goes wrong?

Tom James gravatar imageTom James ( 2017-04-28 08:44:56 -0500 )edit

the first argument of the main function which is argv[1] is used to indicate the path of the input image, and the second is for the background. the third argument is used to indicate which way to remove the background. "difference" means using subtraction, while "division" means using the division method. the two methods are stated in the book of < opencv by example >

Tom James gravatar imageTom James ( 2017-04-28 08:50:01 -0500 )edit

you only resize() your images, if img.rows>2048, i guess, there's your problem.

img and pattern need to have exactly the same size for >= and subtract()

berak gravatar imageberak ( 2017-04-29 01:46:07 -0500 )edit

the two pictures have been shown. But in my computer, the size of the two pictures are both 3120*4160, the size of the two images should be the same

Tom James gravatar imageTom James ( 2017-04-29 03:12:46 -0500 )edit

I changed the "resize(pattern,pattern,Size((img.cols)/10,(img.rows)/10),CV_INTER_LINEAR);" into: "resize(pattern,pattern,Size((pattern.cols)/10,(pattern.rows)/10),CV_INTER_LINEAR);" And the problem seemed to be solved. However, the sizes of the two pictures should be the same and img.cols should be equal to pattern.cols. Why didn't the former code works?

Tom James gravatar imageTom James ( 2017-04-29 03:24:44 -0500 )edit