利用opencv提高图像分辨率和降低分辨率(双三次插值),并计算PSNR的值
程序员文章站
2023-12-31 13:16:28
...
import cv2 #opencv读取的格式是BGR
import matplotlib.pyplot as plt
导入需要的包
img=cv2.imread('img005.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
读入图片,并把图片转为rgb格式,因为matplotlib展示图片要rgb格式
img.shape
输出(768, 1024, 3)
BiCubic_small = cv2.resize(img,(int(img.shape[1]*0.5),int(img.shape[0]*0.5)),interpolation=cv2.INTER_CUBIC)
BiCubic_big = cv2.resize(BiCubic_small,(1024,768),interpolation=cv2.INTER_CUBIC)
# BiCubic_interpolation = cv2.resize(img,(img.shape[1]*2,img.shape[0]*2),interpolation=cv2.INTER_CUBIC)
plt.subplot(131)
plt.imshow(BiCubic_small)
plt.title(BiCubic_small.shape)
plt.subplot(132)
plt.imshow(BiCubic_big)
plt.title(BiCubic_big.shape)
plt.subplot(133)
plt.imshow(img)
plt.title(img.shape)
plt.show()
diff1 = img - BiCubic_big
mse = np.mean(np.square(diff1))
psnr = 10 * np.log10(255 * 255 / mse)
print(psnr)
输出结果为: