哪种插值最适合调整图像大小? [英] Which kind of interpolation best for resizing image?

查看:180
本文介绍了哪种插值最适合调整图像大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个numpy数组,希望使用opencv进行调整. 其值的范围是0到255.如果我选​​择使用cv2.INTER_CUBIC,则可能会得到超出此范围的值.这是不希望的,因为调整大小后的数组应该仍然代表图像. 一种解决方案是将结果剪切为[0,255].另一个是使用不同的插值方法. 据我了解,使用INTER_AREA可以对图像进行下采样,但与最近邻居的图像上采样相似,因此对我而言并不是最佳选择.

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.

我应该使用INTER_CUBIC(和剪辑),INTER_AREA还是INTER_LINEAR?

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

使用INTER_CUBIC超出范围的值的示例:

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]]

正如berak所指出的那样,将类型转换为float(从int64)允许值超出原始范围. cv2.resize()函数不适用于默认的"int64"类型.但是,转换为'uint8'会将值自动饱和为[0..255].

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].

另外,正如SaulloCastro指出的那样,另一个相关的答案证明了scipy的插值,而defualt方法是三次插值(具有饱和度).

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

推荐答案

如果您要对图像进行放大,则最好使用 INTER_LINEAR INTER_CUBIC 插值. 如果您要缩小图像,则最好使用 INTER_AREA 插值法.

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.

这篇关于哪种插值最适合调整图像大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆