opencv中的resize插值⽅式⽐较
可以看到,是⽤pytorch提供的插值函数,插值⽅式是最近邻插值。查看pytorch提供的关于interpolate的⽂档,它是由area这个插值⽅式的,如图所⽰
⽽在utils/datasets.py⽂件⾥的resize函数⾥插值⽅式是固定的,没有作为输⼊参数传进来。接下来,我们就来⽐较⼀下,在opencv提供的resize函数⾥,INTER_AREA,INTER_LINEAR和INTER_NEAREST这三种插值⽅式的区别。我们先⽤⼿机拍⼀张图⽚
⼿机拍摄的图⽚都是长⽅形的,图⽚⾼度⼤于宽度。分别⽤opencv提供的resize函数的三种插值⽅式,对原图resize到416x416。python程序如下:
import cv2
if __name__=='__main__':resize函数c++
imgpath = 'srcimg.jpg'
img = cv2.imread(imgpath)
print('srcimg.shape = ', img.shape)
img1 = size(img, (416, 416), interpolation=cv2.INTER_AREA)
img2 = size(img, (416, 416), interpolation=cv2.INTER_LINEAR)
img3 = size(img, (416, 416), interpolation=cv2.INTER_NEAREST)
cv2.imwrite('INTER_AREA.jpg', img1)
cv2.imwrite('INTER_LINEAR.jpg', img2)
cv2.imwrite('INTER_NEAREST.jpg', img3)
cv2.namedWindow('INTER_AREA', cv2.WINDOW_NORMAL)
cv2.imshow('INTER_AREA', img1)
cv2.namedWindow('INTER_LINEAR', cv2.WINDOW_NORMAL)
cv2.imshow('INTER_LINEAR', img2)
cv2.namedWindow('INTER_NEAREST', cv2.WINDOW_NORMAL)
cv2.imshow('INTER_NEAREST', img3)
dif = img1 - img2
dif = dif.astype(np.float32)
err = np.mean(np.abs(dif))
print('INTER_AREA with INTER_LINEAR : mean_error =', err, 'max_error =', np.max(np.abs(dif)), 'min_err =',    np.min(np.abs((dif))))
dif = img1 - img3
dif = dif.astype(np.float32)
err = np.mean(np.abs(dif))
print('INTER_AREA with INTER_NEAREST : mean_error =', err, 'max_error =', np.max(np.abs(dif)), 'min_err =',
np.min(np.abs((dif))))
cv2.waitKey(0)
cv2.destroyAllWindows()
在Pycharm⾥运⾏程序后,得到三种插值的结果图⽚,INTER_AREA的结果是:
INTER_LINEAR的结果是
INTER_NEAREST的结果是
从图⽚质量上看,INTER_AREA明显优于INTER_LINEAR和INTER_NEAREST的,在INTER_LINEAR和INTER_NEAREST的图⽚中有很多瑕疵,纸币中的数字模糊不清。计算resize后的图⽚像素差异的情况如下:
可以看到INTER_AREA与INTER_LINEAR和INTER_NEAREST的差异还是蛮⼤的。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。