9

I want to resize a labeled segmentation mask specifically with nearest neighbor interpolation.

scikit-image has two relevant functions: resize and rescale

But neither of these functions allow you to specify the interpolation method.

It is very important to use 'nearest neighbor' interpolation when resizing segmentation masks.

Does anyone know how to do this with scikit-image or scipy or even some other package that can be easily pip installed. I know how to do this in opencv but it cannot be pip installed on all platforms.

cdeepakroy
  • 2,203
  • 3
  • 19
  • 23

2 Answers2

19

Turns out that I dint read the documentation properly and there is an order parameter to both skimage.transform.resize and skimage.transform.rescale that can be used to specify the interpolation.

Order be in the range 0-5 with the following semantics: 0: Nearest-neighbor 1: Bi-linear (default) 2: Bi-quadratic 3: Bi-cubic 4: Bi-quartic 5: Bi-quintic

cdeepakroy
  • 2,203
  • 3
  • 19
  • 23
2

It is also possible with opencv. For instance, to resize image to 30x30 height, weight.

import cv2
img = cv2.imread('my_image.png')
img = cv2.resize(img,(30,30),interpolation=cv2.INTER_NEAREST)

Other interpolation methods are INTER_LINEAR, INTER_AREA, INTER_CUBIC, INTER_LANCZOS4. Check for details: https://pythonexamples.org/python-opencv-cv2-resize-image/

aykcandem
  • 806
  • 1
  • 6
  • 18