图片缩放
最近临域插值 双线性插值 原理
- src(原图像) 1020 dst(目标图片) 510
- 可以用原图像上的点src表示目标图像上的点dst
#例如:原图像的点(2,4)表示目标图像的点(1,2) - 知道目标图像(x,y)如何计算原图像的(x,y)?公式
#newx=x*(src行/目标行) 例如:newx=1*(10/5)=2
#newy=y*(src列/目标列) 例如:newy=2*(20/10)=4 - 计算出是小数12.3取12 -->最近邻域插值法(取最近的)
#比如有一个点(15.2,22.3)–>(15,22)(最近邻域插值法)
双线插值法:(API中resize()方法就是实现了该方法)
- 双线性插值
A1 = 20% 上+80%下 A2
B1 = 30% 左+70%右 B2
1 最终点 = A1 30% + A2 70%
2 最终点 = B1 20% + B2 80% - 实质:矩阵运算
代码实现
# 1 info获取图片宽度高度等 2 创建空白模版 3 计算缩放后像素点x y
import cv2
import numpy as np
img = cv2.imread('image0.jpg',1) # 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) #目标模板 3个基本元素 np.uint8 0-255
# 循环遍历图片 到目标高度和宽度结束
for i in range(0,dstHeight): #行
for j in range(0,dstWidth): #列
iNew = int(i*(height*1.0/dstHeight)) #强制转化 计算新的i
jNew = int(j*(width*1.0/dstWidth))
dstImage[i,j] = img[iNew,jNew] #目标图像i j 为计算所得的i j
cv2.imshow('dst',dstImage)
cv2.waitKey(0)
# 实现方法1 opencv API resize 2 算法原理 3 自己实现源码
-
问题
AttributeError: ‘NoneType’ object has no attribute 'shape’
-
原因
找不到这个image0图片,检查image0是否在当前目录