Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shrink/resize an image without interpolation

Tags:

python

opencv

I have an image F of size 1044*1408, it only has 3 integer values 0, 2, and 3.

I want to shrink it to 360*480. Now I am using Z= cv2.resize(F,(480,380)). But Z is interpolated, it has many unique values, more than just 0, 2 and 3. I can't just round up the interpolated values to the closest integer, because I will get some 1s.

F is read from a tif file and manipulated, it is an ndarray now. So I can't use PIL: F = F.resize((new_width, new_height)) as F is not from F = Image.open(*).

like image 485
Echo Avatar asked Sep 16 '16 02:09

Echo


People also ask

How do I shrink an image without losing quality?

One way to do this is to use a program like Photoshop. With Photoshop, you can resize an image without losing quality by using the "Image Size" dialog box. In the "Image Size" dialog box, you can change the width and height of the image. You can also change the resolution.

How do I resize an image without distortion in Python?

Without any distortion you have 2 options: a) crop part of the image to make it the same aspect ratio. b) add part of the image (e.g. black pixels) to the sides of the images to make it the same aspect ratio. If you do not have the same aspect ratio, it will not be possible to obtain it without distortion. – Rick M.


2 Answers

You may use INTER_NEAREST:

Z= cv2.resize(F,(480,380),fx=0, fy=0, interpolation = cv2.INTER_NEAREST)
like image 101
Humam Helfawi Avatar answered Oct 25 '22 14:10

Humam Helfawi


Alternately, you can also use skimage.transform.resize. Argument order = 0 enforces nearest-neighbor interpolation.

   Z = skimage.transform.resize(F,
                               (480,380),
                               mode='edge',
                               anti_aliasing=False,
                               anti_aliasing_sigma=None,
                               preserve_range=True,
                               order=0)
like image 20
Daniel R. Livingston Avatar answered Oct 25 '22 14:10

Daniel R. Livingston