11

In PIL the highest quality resize from what I've seen seems to be:

img = img.resize((n1, n2), Image.ANTIALIAS)

For openCV this seems to be the way to do it:

small = cv2.resize(image, (0,0), fx=0.5, fy=0.5) 

So my question is, is there an additional parameter needed or will this reduce the size with least quality lost?

alfredox
  • 4,082
  • 6
  • 21
  • 29

1 Answers1

16

From the documentation:

To shrink an image, it will generally look best with CV_INTER_AREA interpolation, whereas to enlarge an image, it will generally look best with CV_INTER_CUBIC (slow) or CV_INTER_LINEAR (faster but still looks OK).

The default for resize is CV_INTER_LINEAR. Change the interpolation to CV_INTER_AREA since you wish to shrink the image:

small = cv2.resize(image, (0,0), fx=0.5, fy=0.5, interpolation = cv2.INTER_AREA)

You may wish to compare the results of both interpolations for visual verification that you are getting the best quality.

chembrad
  • 887
  • 3
  • 19
  • 33
  • I am actually going to do both shrinking and expanding an image. It has to be an exact size and if it's not, I will need to resize it up or down. So what I am getting is that CV_INTER_LINEAR is faster and the standard good for downsizing, but CV_INTER_AREA should be used for up-sizing? I need the maximum quality. Do you know how much slower INTER_AREA is? – alfredox Nov 07 '15 at 20:24
  • @alfredox INTER_AREA is the best for making the image smaller when talking about quality. INTER_CUBIC for making the image bigger. INTER_LINEAR seems to be the fastest. I'd imagine you could do some performance testing to compare each interpolation--I have not done this myself. Re-sizing can be expensive (performance-wise). – chembrad Nov 07 '15 at 20:42
  • I got an error, apparently CV_INTER_AREA doesn't exist in cv2? – alfredox Nov 08 '15 at 17:14
  • 2
    I don't use Python but I believe the code would be `cv2.INTER_AREA` rather than `CV_INTER_AREA`. Are you trying that? – chembrad Nov 08 '15 at 17:31
  • My bad, I should've said that I did try that, it doesn't exist in the cv2 import. – alfredox Nov 09 '15 at 07:24
  • If you want to rescale without aliasing only linear or cubic interpolations would not be sufficient. You need to additionally use gaussian smoothing. – Mohammad Jun 08 '21 at 02:50