世間萬(wàn)圖,皆可縮放。在使用opencv的過(guò)程中,所學(xué)過(guò)的一些圖像縮放大法,以很咸魚的方式記錄于此。更多opencv筆記可搜索「浪學(xué)」微信公眾號(hào)~
首先,導(dǎo)入相關(guān)的庫(kù),讀入原圖像
import cv2
import numpy as np
img = cv2.imread('image.jpg',1)
imgInfo = img.shape
print(imgInfo)
width = imgInfo[0]
height = imgInfo[1]
?
# 在anaconda中,使用matplotlib顯示圖片會(huì)更好點(diǎn)
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
%matplotlib inline
?
imshow(img)
顯示原圖像如下:

浪學(xué)公眾號(hào)
圖像縮放有幾種方法
1)第一種方法,調(diào)用resize函數(shù)
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
dst = cv2.resize(img, (dstHeight,dstWidth))
imshow(dst)
2)第二種方法,直接進(jìn)行像素操作
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
?
dst = np.zeros((dstHeight,dstWidth,3),np.uint8)
for i in range(dstHeight):
for j in range(dstWidth):
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(width*1.0/dstWidth))
dst[i,j] = img[iNew,jNew]
imshow(dst)
3)第三種方法,使用warpAffine函數(shù)映射
matScale = np.float32([[0.5,0,0],[0,0.5,0]])
dst = cv2.warpAffine(img,matScale,(int(height/2),int(width/2)))
?
imshow(dst)
三種方法的結(jié)果都如下

浪學(xué)公眾號(hào)
忘他忘我,無(wú)喜無(wú)憂。咸魚一世,隨性葛優(yōu)。
歡迎vx關(guān)注「浪學(xué)」。