图片缩放
# 1、 图片加载 2、图片信息 3、resize方法 4、检查结果
import cv2
img= cv2.imread('timg.jpeg',1) #1代表彩色
imgInfo =img.shape #图片是一个矩阵,长对应行 宽对应列
print(imgInfo) #(1080, 1920, 3)长度、宽度、颜色组成方式
height= imgInfo[0] #imgInfo里面一共3个数 (1080, 1920, 3)
width=imgInfo[1]
mode= imgInfo[2] #颜色组成方式 现在一个像素点是3种颜色 ,所以为3(0,1,2)
#1、放大和缩小两种 2、等比例和非等比例缩放
dstHeight =int (height*0.5)
dstWidth= int(width*0.5)
#最近邻域插值 双线性插值 像素关系重采样 立方插值
dst =cv2.resize(img,(dstWidth,dstHeight))
cv2.imshow('img',dst)
cv2.waitKey(10000)
最近邻域插值法
目标上的每一个点都来自于原图像
newX= x*(src 行/目标 ) newX =1*(10/5)=2
newY= y*(src 列/目标 ) newY =1*(10/5)=2
12.3 -> 12双线性插值法
- 步骤1
(15.2 , 22.3)这个点进行投影,先水平或先竖直均可
22.3-22=0.3 B1这个点占(15,22)-(15,23)这段距离的左边30%右边70%
15.2-15=0.2 20% 80% - 步骤2
- 步骤1
方法1
A1*0.3+A2*70
方法2
B1*20%+B2*80
- 代码实现
import cv2
import numpy as np
img= cv2.imread('timg.jpeg',1)
imgInfo = img.shape
height= imgInfo[0]
width= imgInfo[1]
dstHeight=int(height/2)
dstWidth=int(width/2)
dstImg =np.zeros((dstHeight,dstWidth,3),np.uint8) #0-255 每个像素的数据类型?
#创建一个空白模板 (dstHeight,dstWidth,3)图片的基本信息
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))
dstImg[i,j]=img[iNew,jNew]
cv2.imshow('dst',dstImg)
cv2.waitKey(10000)
图片剪切
#从 x: 250 剪切到 670 y:820-1350
import cv2
img=cv2.imread('timg.jpeg',1)
imgInfo =img.shape
dst= img[250:670,820:1350]
cv2.imshow('剪切好的',dst)
cv2.waitKey(10000)
图片移位
- API
#从 x: 100 剪切到 200 y:200-300
import cv2
import numpy as np
img=cv2.imread('timg.jpeg',1)
cv2.imshow('src',img)
imgInfo=img.shape
height=imgInfo[0]
width=imgInfo[1]
matshift= np.float32([[1,0,100],[0,1,200]])#两行三列
dst=cv2.warpAffine(img,matshift,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(10000)
水平平移了100px 竖直平移了200px
2. 算法原理
[[1,0,100],[0,1,200]]
[ [1,0]
[0,1] ] 2*2 矩阵A
[[100],[200]] 2*1 矩阵B
xy=c
A*C+B=当前移位之后的矩阵
A*C+B=[
[ [ 1*x+0*y ],[0*x+1*y]]+[ [100],[200] ]
]
3. 源代码
#从 x: 100 剪切到 200 y:200-300
import cv2
import numpy as np
img=cv2.imread('timg.jpeg',1)
cv2.imshow('src',img)
imgInfo=img.shape
dst =np.zeros(img.shape,np.uint8)
height=imgInfo[0]
width=imgInfo[1]
for i in range(0,height):
for j in range(0,width-100):
#因为上面创建dst的时候图像并没有变大,而是多出去的部分没有了 所以 width-100
dst[i,j+100]=img[i,j]
cv2.imshow('img',dst)
cv2.waitKey(10000)
图片镜像
import numpy as np
from numpy import uint8
img= cv2.imread('haha.png',1)
cv2.imshow('src',img)
imgInfo =img.shape
height= imgInfo[0]
width =imgInfo[1]
deep=imgInfo[2]
newImginfo=(height*2,width,deep)
dst = np.zeros(newImginfo,np.uint8)
for i in range(0,height):
for j in range(0,width):
dst[i,j]= img[i,j]
# x y =2*h-y-1
dst[height*2-i-1,j]=img[i,j]
for i in range(0,width):
dst[height,i]=(0,0,255)#bgr
cv2.imshow('dst',dst)
cv2.waitKey(10000)
图片缩放
import cv2
import numpy as np
from numpy import uint8
img= cv2.imread('timg.jpeg',1)
cv2.imshow('src',img)
imgInfo =img.shape
height= imgInfo[0]
width =imgInfo[1]
matScale= np.float32([[0.5,0,0],[0,0.5,0]])
dst= cv2.warpAffine(img,matScale,(int(width/2),int(height/2)))
cv2.imshow('suoxiao',dst)
cv2.waitKey(10000)
仿射变换
图片旋转
import cv2
import numpy as np
from numpy import uint8
img= cv2.imread('haha.png',1)
imgInfo =img.shape
height= imgInfo[0]
width =imgInfo[1]
matRotate =cv2.getRotationMatrix2D((height*0.5,width*0.5),45,0.5)
#第三个系数为1的话,多出去的部分不显示
#作用:得到一个旋转矩阵 3个参数 1、 旋转中心 2、角度 3、缩放系数
dst =cv2.warpAffine(img,matRotate,(height,width))
cv2.imshow('dst',dst)
cv2.waitKey(10000)