opencv筆記(1):圖像縮放

世間萬(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é)」。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容