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.
If you are enlarging the image, you should prefer to use INTER_LINEAR or INTER_CUBIC interpolation. If you are shrinking the image, you should prefer to use INTER_AREA interpolation. Cubic interpolation is computationally more complex, and hence slower than linear interpolation.
Nearest neighbour interpolation is the simplest approach to interpolation. Rather than calculate an average value by some weighting criteria or generate an intermediate value based on complicated rules, this method simply determines the “nearest” neighbouring pixel, and assumes the intensity value of it.
Image interpolation occurs when you resize or distort your image from one pixel grid to another. Image resizing is necessary when you need to increase or decrease the total number of pixels, whereas remapping can occur when you are correcting for lens distortion or rotating an image.
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
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With