opencv筆記(1):圖像縮放

世間萬圖,皆可縮放。在使用opencv的過程當中,所學過的一些圖像縮放大法,以很鹹魚的方式記錄於此。更多opencv筆記在「浪學」公衆號。bash

首先,導入相關的庫,讀入原圖像函數

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顯示圖片會更好點
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
%matplotlib inline

imshow(img)
複製代碼

顯示原圖像以下:ui

浪學

圖像縮放有幾種方法spa

1)第一種方法,調用resize函數3d

dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
dst = cv2.resize(img, (dstHeight,dstWidth))

imshow(dst)
複製代碼

2)第二種方法,直接進行像素操做code

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函數映射cdn

matScale = np.float32([[0.5,0,0],[0,0.5,0]])
dst = cv2.warpAffine(img,matScale,(int(height/2),int(width/2)))

imshow(dst)
複製代碼

三種方法的結果都以下blog

浪學

忘他忘我,無喜無憂。圖片

鹹魚一世,隨性葛優。string

相關文章
相關標籤/搜索