0

I have trouble resizing a 6x6 image to 120x120. It looks like the resized image is somewhat shifted by 1 pixel. This happens with the cvResize and with cv::resize. My code looks like this:

warpPerspective(greyImg, warpedImg, homography, Size(6, 6));
Mat bigWarpedImg = Mat(120,120,CV_8UC1);
resize(warpedImg, bigWarpedImg, Size(0,0), 20, 20, INTER_NEAREST);

warpedImg looks like this (I resized it with gimp to make it easier to recognize): http://picasaweb.google.com/103165673068768416583/Opencv#5565090881969794706

bigWarpedImg looks like this: http://picasaweb.google.com/103165673068768416583/Opencv#5565090880773608210

As you can see, in bigWarpedImg the left and upper border is to small whereas the right and bottom border is too thick. It looks like a bug in OpenCV. Is this one or do I use this function incorrectly?

binford
  • 1,655
  • 1
  • 18
  • 36

2 Answers2

2
Mat bigWarpedImg = Mat(120,120,CV_8UC1);

this line is unneccessary - resize will allocate the target Mat to make it fit, so Mat bigWarpedImg would be fine.

Not sure about the resizing - I always use the

resize(warpedImg, bigWarpedImg, Size(120,120), 0, 0, INTER_NEAREST);

form of resize and never noticed such behaviour. I'd say it's a bug though, from the documentation it shouldn't act like that.

etarion
  • 16,935
  • 4
  • 43
  • 66
0

It could be because you're using nearest interpolation. Try the better ones (I think bicubic).

Utkarsh Sinha
  • 3,295
  • 4
  • 30
  • 46
  • There is no need for interpolation if the new size is a (natural number) multiple of the original size. – binford Jan 25 '11 at 12:32
  • I just tried it. Resized a 640x480 image to 64x48 size using nearest and bilinear interpolations. The nearest had the one pixel shift, bilinear (and above) did not. – Utkarsh Sinha Jan 28 '11 at 17:01
  • Thanks for testing this. However, I dont want to spend cpu cycles if I dont have to. If I resize from SMALL to BIG (not the other way round) and the big image size being a multiple of the small image, interpolation is just not nessecary (it does not give better results). – binford Feb 01 '11 at 12:12
  • It doesn't matter if you resize from small->big or big->small. You're essentially using the exact same formula to calculate intermediate positions. I'd suggest you write your own routines that draw squares (of appropriate size) and color to increase the size of the image. Like 1 pixel in small = 24px square in big image. – Utkarsh Sinha Feb 01 '11 at 17:02