5

I have a 512x160 pixel black and white image that I want to upscale (to 4096x1280) using OpenCV. It is very important that pixels that were negative (white) remain negative. cv2.resize appears to anti-alias the image by default, thereby creating falsely-positive pixels. Is there a way to disable the anti-aliasing?

Edit: From what I can see here are the interpolation methods:

  • INTER_NEAREST - nearest neighbor interpolation
  • INTER_LINEAR - bilinear interpolation
  • INTER_CUBIC - bicubic interpolation
  • INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire'-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
  • INTER_LANCZOS4 - Lanczos interpolation over 8x8 neighborhood.
  • INTER_MAX - mask for interpolation codes.
  • WARP_FILL_OUTLIERS - flag, fills all of the destination image pixels. If some of them correspond to outliers in the source image, they are set to zero.
  • WARP_INVERSE_MAP - flag, inverse transformation.
saurabheights
  • 3,967
  • 2
  • 31
  • 50
Ricky
  • 113
  • 1
  • 1
  • 5
  • 4
    in c++ you can choose the interpolation type, probably same for python. You might want to use CV_INTER_NEAREST mode – Micka Apr 11 '16 at 16:37
  • @Micka Thanks, what exactly does 'nearest neighbor interpolation' mean? – Ricky Apr 11 '16 at 16:43
  • after resizing it is not clear which color a new pixel will get. typically you interpolate between source image's pixels in that area. INTER_NEAREST instead chooses the color of the nearest pixel in the source image. – Micka Apr 11 '16 at 16:46
  • 3
    For anyone interested INTER_NEAREST was still giving me some 'false positive' pixels. I solved the problem by resizing the image as an np array: `np.repeat(np.repeat(image,8, axis=0), 8, axis=1)` – Ricky Apr 11 '16 at 17:06
  • 1
    I found [`scipy.misc.imresize`](http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.imresize.html) a better alternative to `cv2.resize` for enlarging the images when working with OpenCV in Python. According to [this answer](http://stackoverflow.com/a/16510074/3962537) [`scipy.ndimage.zoom`](http://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html#scipy.ndimage.zoom) is also an option. – Dan Mašek Apr 11 '16 at 19:37

1 Answers1

2

Interpolation decides how to do the antialiasing, its sort of the reverse because in this case the image doesn't have enough resolution to display its original resolution. In python the solution would be to use cv2.INTER_NEAREST:

scale_ratio = 0.5
img_resized = cv2.resize(img, None, fx=scale_ratio, fy=scale_ratio, interpolation=cv2.INTER_NEAREST)
GuySoft
  • 1,723
  • 22
  • 30