3

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.

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92
  • Did you look [here at line 3099](https://github.com/Itseez/opencv/blob/03fc3d1ceb867cbd6882e2a2809a196582d0efc1/modules/imgproc/src/imgwarp.cpp)? Maybe a coefficient or parameter is slightly different. – LovaBill Feb 02 '15 at 16:17
  • GIMP doesn't offer X and Y axis... I suppose they are 0 (as OpenCV are by default) – Rafael Ruiz Muñoz Feb 02 '15 at 16:26

1 Answers1

2

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 ;)

Rafael Ruiz Muñoz
  • 5,333
  • 6
  • 46
  • 92