Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a labeled mask with nearest neighbor interpolation using scikit-image

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.

like image 290
cdeepakroy Avatar asked May 02 '18 18:05

cdeepakroy


People also ask

Which interpolation is best for image resizing?

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.

What is nearest Neighbour interpolation in image processing?

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.

What is interpolation resize?

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.


2 Answers

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

like image 127
cdeepakroy Avatar answered Oct 04 '22 21:10

cdeepakroy


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/

like image 42
aykcandem Avatar answered Oct 04 '22 21:10

aykcandem