opencv的resize和torch.nn.functional.interpolate 比较
缩放图像效果比较,
分别用resize函数和interpolate函数,输出 nearset, linear, cubic, area mode的图像
代码如下:
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from matplotlib import pyplot as plt
from mmedit.core import tensor2img
def cv_scale_image(image, scale=2, interp=cv2.INTER_LINEAR):
"""
Args:
image: h,w,3 array
scale
interp:
INTER_NEAREST - 最邻近插值
INTER_LINEAR - 双线性插值,如果最后一个参数你不指定,默认使用这种方法
INTER_CUBIC - 4x4像素邻域内的双立方插值
INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值
INTER_AREA -
Returns:
"""
out = cv2.resize(image, dsize=None, fx=scale, fy=scale, interpolation=interp)
print(out.shape, out.dtype)
return out
def torch_scale_image(image, scale=2, interp='bilinear', align=True):
"""
Args:
image: h,w,3 array
scale:
interp: 'nearest'| 'linear'| 'bilinear'| 'bicubic'| 'trilinear'| 'area'。默认:'nearest'
Returns:
"""
t_image = torch.from_numpy(image / 255).permute(2, 0, 1).float().unsqueeze(0)
# print(t_image.min(), t_image.max(), t_image.shape)
if interp in ['linear' , 'bilinear' , 'bicubic', 'trilinear']:
image_out = F.interpolate(
input=t_image,
scale_factor=scale,
mode=interp,
align_corners=align)
else:
image_out = F.interpolate(
input=t_image,
scale_factor=scale,
mode=interp)
# print(image_out.min(), image_out.max(), image_out.shape, image_out.dtype)
return image_out
if __name__ == "__main__":
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
filename = r'D:\superresolution\realbasicvsr\RealBasicVSR-master\data\demo_000\00000000.png'
image = cv2.imread(filename)
image = image[..., ::-1]
image2 = image.copy()
# opencv resize函数
out10 = cv_scale_image(image, scale=4, interp=cv2.INTER_NEAREST)
out11 = cv_scale_image(image, scale=4, interp=cv2.INTER_LINEAR)
out12 = cv_scale_image(image, scale=4, interp=cv2.INTER_CUBIC)
out13 = cv_scale_image(image, scale=4, interp=cv2.INTER_AREA)
print(cv2.INTER_NEAREST, cv2.INTER_LINEAR,cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_LANCZOS4)
cv2.imwrite(filename[:-4] + 'cv' + '_nearest' + '.png', out10[..., ::-1])
cv2.imwrite(filename[:-4] + 'cv' + '_linear' + '.png', out11[..., ::-1])
cv2.imwrite(filename[:-4] + 'cv' + '_bicubic' + '.png', out12[..., ::-1])
cv2.imwrite(filename[:-4] + 'cv' + '_area' + '.png', out13[..., ::-1])
# torch interp函数
# 'linear'| 'bilinear'| 'bicubic'| 'trilinear'| 'area'
out20 = torch_scale_image(image2, scale=4, interp='nearest')
out21 = torch_scale_image(image2, scale=4, interp='bilinear')
out22 = torch_scale_image(image2, scale=4, interp='bicubic')
out23 = torch_scale_image(image2, scale=4, interp='area')
out20 = tensor2img(out20)
out21 = tensor2img(out21)
out22 = tensor2img(out22)
out23 = tensor2img(out23)
cv2.imwrite(filename[:-4] + 'torch_' + 'nearest.png', out20)
cv2.imwrite(filename[:-4] + 'torch_' + 'bilinear.png', out21)
cv2.imwrite(filename[:-4] + 'torch_' + 'bicubic.png', out22)
cv2.imwrite(filename[:-4] + 'torch_' + 'area.png', out23)
out21 = torch_scale_image(image2, scale=4, interp='bilinear', align=False)
out22 = torch_scale_image(image2, scale=4, interp='bicubic', align=False)
out21 = tensor2img(out21)
out22 = tensor2img(out22)
cv2.imwrite(filename[:-4] + 'torch_' + 'bilinear_align_false.png', out21)
cv2.imwrite(filename[:-4] + 'torch_' + 'bicubic_align_false.png', out22)
"""
align为False时, opencv和torch得到结果完全一致
"""
结论:align为False时, opencv和torch得到结果一致.
cubic图像对比结果如下: