计算机视觉任务中,经常要使用图像插值方法来改变图像的尺寸,如图像金字塔、图像超分辨的预处理等,可以说图像插值方法是计算机视觉任务的基本操作。本文对opencv里的图像插值方法进行分析比较。
opencv里改变图像尺寸的函数是resize,下边是opencv2.3.14的resize函数的参考文档:
C++: void resize(InputArray src, OutputArray dst, Size dsize, double fx=0, double fy=0, int interpolation=INTER_LINEAR)
Parameters:
- src – input image.
- dst – output image; it has the size dsize (when it is non-zero) or the size computed from src.size(), fx, and fy; the type of dst is the same as of src.
- dsize –
output image size; if it equals zero, it is computed as:
Either dsize or both fx and fy must be non-zero.
- fx –
scale factor along the horizontal axis; when it equals 0, it is computed as
- fy –
scale factor along the vertical axis; when it equals 0, it is computed as
- interpolation –
interpolation method:
- INTER_NEAREST - a nearest-neighbor interpolation
- INTER_LINEAR - a bilinear interpolation (used by default)
- INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
- INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
- INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
参考文档详细内容请参考:https://docs.opencv.org/2.4.13/modules/imgproc/doc/geometric_transformations.html?highlight=resize#cv2.resize。简单说明一下,src是输入图像,dst是输出图像,dsize是输出图像的尺寸,由用户指定,fx和fy是缩放因子,dsize与fx、fy两组参数只能选择一组使用,interpolation是可选的插值方法。
本文主要对opencv不同的插值方法进行比较,也就是INTER_NEAREST、INTER_LINEAR、INTER_AREA、INTER_CUBIC和INTER_LANCZOS4。实验所使用的图像为Art:

原始图像尺寸为1390×1110,选用的缩放因子为3,使用PSNR与SSIM作为评价标准。本文内容主要分为以下几个部分:
- 插值方法之间的相似度比较
- 插值方法下采样上采样的比较——BGR三通道
- 插值方法下采样上采样的比较——亮度通道Y,由CV_BGR2YUV转换
- 插值方法下采样上采样的比较——亮度通道Y,由CV_BGR2YCrCb转换
1、插值方法之间的相似度比较
对于原始尺寸1390×1110的Art图像,按照缩放因子3对原始图像进行下采样,得到低分辨率图像。使用不同的插值方法进行这一操作,比较低分辨率图像之间的相似程度,结果如下:
nearest | bilinear | bicubic | area | lanczos4 | |
nearest | 27.261/0.850 | 27.002/0.843 | 28.300/0.882 | 26.994/0.842 | |
bilinear | 50.176/0.999 | 38.294/0.980 | 49.538/0.998 | ||
bicubic | 37.007/0.975 | 55.615/0.999 | |||
area | 36.966/0.975 | ||||
lanczos4 |
由上表可知,插值方法bilinear、bicubic和lanczos4最相似度最高。
2、插值方法下采样上采样的比较——BGR三通道
对原始图像,按照缩放因子3对原始图像下采样再上采样,opencv读入彩色图像为BGR三通道图像,上采样下采样操作分别对每一个单通道进行。下表的结果是使用对应插值方法得到的结果与原始图像(ground truth)的比较结果:
down|up | nearest | bilinear | bicubic | area | lanczos4 |
nearest | 25.439/0.756 | 28.052/0.840 | 27.643/0.833 | 26.863/0.800 | 27.529/0.827 |
bilinear | 30.019/0.869 | 33.044/0.917 | 33.429/0.920 | 31.278/0.890 | 33.281/0.915 |
bicubic | 29.837/0.866 | 33.009/0.917 | 33.185/0.918 | 31.101/0.888 | 32.971/0.911 |
area | 30.734/0.879 | 32.848/0.910 | 34.031/0.923 | 31.872/0.895 | 34.208/0.925 |
lanczos4 | 29.807/0.865 | 32.940/0.916 | 33.122/0.917 | 31.070/0.887 | 32.915/0.911 |
3、插值方法下采样上采样的比较——亮度通道Y,由CV_BGR2YUV转换
对原始图像,首先将读入的BGR彩色图像使用opencv的函数CV_BGR2YUV转换到YUV三通道,然后再将Y通道分离出来,仅对Y通道做下采样再上采样。下表的结果是对Y通道使用对应的插值方法得到的结果与原始图像的Y通道比较的结果:
down|up | nearest | bilinear | bicubic | area | lanczos4 |
nearest | 25.604/0.766 | 28.243/0.847 | 27.828/0.840 | 27.061/0.808 | 27.710/0.833 |
bilinear | 30.192/0.874 | 33.161/0.920 | 33.517/0.923 | 31.464/0.894 | 33.365/0.917 |
bicubic | 30.006/0.871 | 33.124/0.920 | 33.271/0.920 | 31.284/0.892 | 33.051/0.914 |
area | 30.914/0.884 | 32.991/0.913 | 34.136/0.926 | 32.067/0.899 | 34.305/0.928 |
lanczos4 | 29.975/0.871 | 33.053/0.919 | 33.206/0.920 | 31.250/0.891 | 32.995/0.913 |
4、插值方法下采样上采样的比较——亮度通道Y,由CV_BGR2YCrCb转换
对原始图像,首先将读入的BGR彩色图像使用opencv的函数CV_BGR2YCrCb转换到YCrCb三通道,然后再将Y通道分离出来,仅对Y通道做下采样再上采样。下表的结果是对Y通道使用对应的插值方法得到的结果与原始图像的Y通道比较的结果:
down|up | nearest | bilinear | bicubic | area | lanczos4 |
nearest | 25.726/0.765 | 28.347/0.847 | 27.931/0.840 | 27.155/0.808 | 27.812/0.833 |
bilinear | 30.271/0.874 | 33.213/0.920 | 33.565/0.923 | 31.514/0.894 | 33.409/0.918 |
bicubic | 30.082/0.871 | 33.174/0.920 | 33.317/0.921 | 31.330/0.892 | 33.093/0.914 |
area | 31.002/0.884 | 33.039/0.914 | 34.184/0.926 | 32.121/0.899 | 34.350/0.928 |
lanczos4 | 30.051/0.871 | 33.104/0.920 | 33.252/0.921 | 31.296/0.891 | 33.035/0.914 |