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.
5
  • 4
    in c++ you can choose the interpolation type, probably same for python. You might want to use CV_INTER_NEAREST mode Commented Apr 11, 2016 at 16:37
  • @Micka Thanks, what exactly does 'nearest neighbor interpolation' mean? Commented Apr 11, 2016 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. Commented Apr 11, 2016 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) Commented Apr 11, 2016 at 17:06
  • 1
    I found scipy.misc.imresize a better alternative to cv2.resize for enlarging the images when working with OpenCV in Python. According to this answer scipy.ndimage.zoom is also an option. Commented Apr 11, 2016 at 19:37

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.