Echo
Echo

Reputation: 685

Shrink/resize an image without interpolation

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(*).

Upvotes: 11

Views: 18883

Answers (2)

Daniel R. Livingston
Daniel R. Livingston

Reputation: 1229

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)

Upvotes: 11

Humam Helfawi
Humam Helfawi

Reputation: 20294

You may use INTER_NEAREST:

Z= cv2.resize(F,(480,380),fx=0, fy=0, interpolation = cv2.INTER_NEAREST)

Upvotes: 16

Related Questions