Search code examples
opencvimage-scaling

What's wrong with resize's function on OpenCV?


this is what I wondered today:

I'm trying to scale an image into 4x4 pixels by this code:

Mat image4x4;

vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);

resize(imageFromFile, image4x4, Size(4,4), INTER_CUBIC);
imwrite("/Users/macuser/Desktop/4x4/"+names.at(i), image4x4, compression_params);

My result is this:

enter image description here

but with any other tool, as Photoshop, GIMP, the result is kind of this:

enter image description here

using CUBIC as well with this software.

What is wrong with my implementation? Am not I considering any parameter?

Thank you very much.


Example:

enter image description here

UPDATE: tried with scalers online and the same wrong result is what I got. So... maybe I'm interested to know in what GIMP does.


Solution

  • From what I learnt on Image Processing, resizing is treated with some iteration which are power of 2. The rest is interpolating.

    So what I did is: resizing to the closest power of 2 (so, find the first N which size < pow(2,N)) and then resizing to size = 4 by squares.

    So for example if my N is 10, 2^10 = 512, I had to resize to 256, 128, 64, 32, 16, 8 and 4:

    for (int i = 512; i>=4; i = i/2) {
            resize(image4x4, image4x4, Size(i,i), 0, 0, INTER_CUBIC);
    }
    

    and worked very good ;)