世间万图,皆可缩放。在使用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