Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which kind of interpolation best for resizing image?

I have a numpy array that I wish to resize using opencv. Its values range from 0 to 255. If I opt to use cv2.INTER_CUBIC, I may get values outside this range. This is undesirable, since the resized array is supposed to still represent an image. One solution is to clip the results to [0, 255]. Another is to use a different interpolation method. 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.

Should I use INTER_CUBIC (and clip), INTER_AREA, or INTER_LINEAR?

an example for values outside of range using INTER_CUBIC:

a = np.array( [ 0, 10, 20, 0, 5, 2, 255, 0, 255 ] ).reshape( ( 3, 3 ) ) [[  0  10  20]  [  0   5   2]  [255   0 255]]  b = cv2.resize( a.astype('float'), ( 4, 4 ), interpolation = cv2.INTER_CUBIC ) [[   0.            5.42489886   15.43670964   21.29199219]  [ -28.01513672   -2.46422291    1.62949324  -19.30908203]  [  91.88964844   25.07939219   24.75106835   91.19140625]  [ 273.30322266   68.20603609   68.13853455  273.15966797]] 

Edit: As berak pointed out, converting the type to float (from int64) allows for values outside the original range. the cv2.resize() function does not work with the default 'int64' type. However, converting to 'uint8' will automatically saturate the values to [0..255].

Also, as pointed out by SaulloCastro, another related answer demonstrated scipy's interpolation, and that there the defualt method is the cubic interpolation (with saturation).

like image 661
Roee E Avatar asked May 25 '14 09:05

Roee E


People also ask

Why may interpolation be needed when resizing an image?

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.

What are the 3 interpolation methods for images?

Image interpolation is generally achieved through one of three methods: nearest neighbor, bilinear interpolation, or bicubic interpolation.

What is the best interpolation algorithm?

Lanczos-3 interpolation clearly provides the best result. It is the default algorithm used in all our standard tools for image upsampling tasks. Bicubic spline interpolation is acceptable, but less accurate than Lanczos and leads to significant dispersion of small-scale bright structures.

How do you interpolate an image?

Image SizeTurn on the "Constrain Proportions" check box to resize your image by the same percentage both horizontally and vertically. Turn on the "Resample Image" check box to tell Photoshop you want to enlarge and interpolate your file, not just reinterpret its dimensions by changing its resolution.


1 Answers

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. However, the quality of the resulting image will be higher.

like image 137
Shivam Kushwaha Avatar answered Oct 05 '22 14:10

Shivam Kushwaha