图像缩放有放大、缩小、等比缩放和非等比缩放几种
import cv2
img = cv2.imread('../img/zidan.jpg')
imgInfo = img.shape
print(imgInfo)
height = imgInfo[0]
width = imgInfo[1]
#图像缩放有放大、缩小、等比缩放和非等比缩放几种
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
dstImg = cv2.resize(img,(dstWidth,dstHeight))
cv2.imshow('等比缩小一倍',dstImg)
cv2.waitKey(0)
效果图:
插值方法有:最近邻域插值 像素关系重采样 立方插值 双线性插值(默认)
目标图片某一点的x坐标对应原图片的坐标=目标图片的x坐标*(原图片的宽度/目标图片的宽度)
双线性插值法:当目标图像中的某一个像素点对应原图像的像素点值为小数时;
如图所示:
A1 = A2 = 15*0.2 + 16*0.8
B1 = B2 = 22*0.3 + 23*0.7
最终值 = A1*0.3 + A2*0.7 或 B1*0.2 + B2*0.8
通过双线性插值法缩放图片:
import cv2
import numpy as np
img = cv2.imread('../img/zidan.jpg',1)
imgInfo = img.shape
print(imgInfo)
width = imgInfo[0]
height = imgInfo[1]
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
newImg = np.zeros((dstWidth,dstHeight,3),np.uint8)
for i in range(0,dstWidth):
for j in range(0,dstHeight):
newWidth = int(i*(width*1.0/dstWidth))
newHeight = int(j*(height*1.0/dstHeight))
newImg[i,j] = img[newWidth,newHeight]
cv2.imshow('newimg',newImg)
cv2.waitKey(0)
效果图: