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(*)
.
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.
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.
You may use INTER_NEAREST
:
Z= cv2.resize(F,(480,380),fx=0, fy=0, interpolation = cv2.INTER_NEAREST)
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)
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