An important aspect is the interpolation parameter: there are several ways how to resize an image. Here img is thus a numpy array containing the original image, whereas res is a numpy array containing the resized image. Res = cv2.resize(img, dsize=(54, 140), interpolation=cv2.INTER_CUBIC) It will give you better results for less than 10times zoom and if the image is large than it's better than CUBIC.Īs for my program I was shiriking image 4 times so for me the AREA+LANCZOS4 works better.Yeah, you can install opencv (this is a library used for image processing, and computer vision), and use the cv2.resize function. I came to the conclusion that if you are shrinking image <10 times then go for the LANCZOS4. So I did furthure tests only for AREA+CUBIC and AREA+LANCZOS4. Turns out the images with lot of texture/abstraction gave highest psnr using CUBIC. This results were vague so I opened the files for which CUBIC gave the highes psnr. 19/204 has the highest psnr and 158/347 have the 2nd highes psnr using AREA + CUBIC. Here the AREA and CUBIC gave the 2nd best result. So here are the results of 2nd Maximum only. I also wanted to include the 2nd maximum. For results I picked maximum and 2nd maximum psnr and calculated the count.įor the Maximum the count for interpolation is show in image below.įrom this test the maximum psnr is given by the combination of AREA and LANCZOS4 which gave max psnr for 141/204 images. Tested this on 165 images of different shapes. And after enlarging I calculated psnr with orignal image. And in the end it supports the answer of I tested on these interpolation method, for both combination, shrinking and enlarging. See here for results in each interpolation. INTER_LANCZOS4 – a Lanczos interpolation over 8×8 pixel neighborhood. INTER_CUBIC – a bicubic interpolation over 4×4 pixel neighborhood.But when the image is zoomed, it is similar to the INTER_NEAREST method. It may be a preferred method for image decimation, as it gives moire’-free results. INTER_AREA – resampling using pixel area relation.INTER_LINEAR – a bilinear interpolation (used by default).INTER_NEAREST – a nearest-neighbor interpolation.Some of the possible interpolation in openCV are: Img = cv2.resize(img, newSize, interpolation=cv2.INTER_CUBIC) #INTER_CUBIC interpolation Max(min(ht, int(h / f)), 1)) # scale according to f (result at least 1 and at most wt or ht) And copy interpolated sampled image on the target image like: # create target image and copy sample image into it To overcome such problem you should find out the new size of the given image where the interpolation can be made. Īlso, as pointed out by SaulloCastro, another related answer demonstrated scipy's interpolation, and that there the defualt method is the cubic interpolation (with saturation). However, converting to 'uint8' will automatically saturate the values to. the cv2.resize() function does not work with the default 'int64' type. ]Įdit: As berak pointed out, converting the type to float (from int64) allows for values outside the original range. Should I use INTER_CUBIC (and clip), INTER_AREA, or INTER_LINEAR?Īn example for values outside of range using INTER_CUBIC: a = np.array( ).reshape( ( 3, 3 ) )ī = cv2.resize( a.astype('float'), ( 4, 4 ), interpolation = cv2.INTER_CUBIC ) It is my understanding that using INTER_AREA is valid for down-sampling an image, but works similar to nearest neighbor for upsampling it, rendering it less than optimal for my purpose. Another is to use a different interpolation method. This is undesirable, since the resized array is supposed to still represent an image. If I opt to use cv2.INTER_CUBIC, I may get values outside this range. I have a numpy array that I wish to resize using opencv.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |