圖像縮放有放大、縮小、等比縮放和非等比縮放幾種
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)
效果圖: