opencv笔记(1):图像缩放

世间万图,皆可缩放。在使用opencv的过程中,所学过的一些图像缩放大法,以很咸鱼的方式记录于此。更多opencv笔记可搜索「浪学」微信公众号~

首先,导入相关的库,读入原图像

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)

显示原图像如下:
浪学图文

图像缩放有几种方法
1)第一种方法,调用resize函数

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

2)第二种方法,直接进行像素操作

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函数映射

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.csdn.net/banaoxie0070/article/details/89452775
今日推荐