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)
复制代码

三种方法的结果都如下

忘他忘我,无喜无忧。

咸鱼一世,随性葛优。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值