利用OpenCV来进行图片的缩放,主要有四个步骤,
1.加载缩放的图片
2.获取图片信息
3.调用resize方法
4.检查最终结果
代码部分
# 1 加载缩放的图片
import cv2
img = cv2.imread('2.jpg',1)
#2 获取图片信息
imgInfo = img.shape
print(imgInfo) #打印出图片的宽、高、
# 图片的高、宽
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
# 1 放大 缩小 2 等比例 非等比例
# 等比例缩小
# 乘的系数是相同的就是等比例的
dstHeight = int(height*0.5)
dstWidth = int(width*0.5)
# 最近临域插值 双线性插值 像素关系重采样 立方插值
# 3.调用resize方法
dst = cv2.resize(img,(dstWidth,dstHeight))
cv2.imshow('image',dst)
cv2.waitKey(0)
最终的效果图
![](https://img.haomeiwen.com/i6455290/140aa2bb47b4da8e.png)
最近临域插值算法源码实现图片缩放
# 1 info 2 空白模板 3 计算xy像素点
import cv2
import numpy as np
img = cv2.imread('2.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dstHeight = int(height/2)
dstWidth = int(width/2)
dstImage = np.zeros((dstHeight,dstWidth,3),np.uint8) #0-255
for i in range(0,dstHeight): #行
for j in range(0,dstWidth):#列
iNew = int(i*(height*1.0/dstHeight))
jNew = int(j*(width*1.0/dstWidth))
dstImage[i,j] = img[iNew,jNew]
cv2.imshow('dst',dstImage)
cv2.waitKey(0)
最终效果与上面一致
网友评论