缩放
cv2.resize()
- 在shape属性中,第一个值对应的是行数,第二个值对应的是列数
- 在dsize参数中,第一个值对应的是列数,第二个值对应的是行数
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
rows,cols=img.shape[:2]
#dsize参数中,行和列倒转
size=(int(cols*0.9),int(rows*0.5))#列缩小为0.9倍,行缩小为0.5倍
rst=cv2.resize(img,size)
print(img.shape)
print(rst.shape)
(256, 256, 3)
(128, 230, 3)
控制fx参数和fy参数,完成图像的缩放
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
#fx控制列变换(2倍),fy控制行变换(0.5倍)
rst=cv2.resize(img,None,fx=2,fy=0.5)
print(img.shape)
print(rst.shape)
(256, 256, 3)
(128, 512, 3)
翻转
cv2.flip()
第二个参数
- 0表示绕着x中翻转
- 正数表示绕着y翻转
- 负数表示绕着x,y轴同时翻转
import cv2
import numpy as np
img=cv2.imread("D:/zwy.jpg")
x=cv2.flip(img,0)
y=cv2.flip(img,1)
z=cv2.flip(img,-1)
cv2.imshow("img",img)
cv2.imshow("0",x)
cv2.imshow("1",y)
cv2.imshow("-1",z)
cv2.waitKey(0)
cv2.destroyAllWindows()
平移
cv2.warpAffine(src,M,dsize)
平移变换(x0,y0)
M:
[1 0 x0
0 1 y0 ]
右:正
下:正
左:负
上:负
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
height,weight=img.shape[:2]
x=100
y=200
M=np.float32([[1,0,x],[0,1,y]])
move=cv2.warpAffine(img,M,(height,weight))
cv2.imshow("move",move)
cv2.waitKey(0)
cv2.destroyAllWindows()
旋转
cv2.warpAffine(src,M,dsize)
cv2.getRotationMatrix2D(中心,正:逆/负:顺,缩放大小)
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
h,w=img.shape[:2]
M=cv2.getRotationMatrix2D((w/2,h/2),45,0.6)
rst=cv2.warpAffine(img,M,(w,h))
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyAllWindows()
仿射
仿射变换可以通过一系列的几何变换来实现平移,旋转多种操作。该变换能够保持图像的平直性和平行性,变换矩阵M
cv2.warpAffine(src,M,dsize)
rst=cv2.getAffineTransform(src,dst)
src表示输入图像的三个点的坐标(左上,右上,左下)
dst表示输出图像的三个点的坐标
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
#坐标顺序,先col后row,先列后行
p1=np.float32([[0,0],[col-1,0],[0,row-1]])
p2=np.float32([[0,row*0.33],[col*0.85,row*0.25],[col*0.15,row*0.7]])
M=cv2.getAffineTransform(p1,p2)
dst=cv2.warpAffine(img,M,(col,row))
cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
透视
透视变换可以将矩形映射成任意形状的四边形
cv2.warpPerspective(src,M,dsize)
四个点的顺序(左上,右上,左下,右下)
import cv2
import numpy as np
img=cv2.imread("D:/renlian.jpg")
row,col=img.shape[:2]
p1=np.float32([[150,50],[400,50],[150,450],[400,450]])
p2=np.float32([[50,50],[row-100,200],[200,col-100],[row-1,col-1]])
#左上 右上 左下 右下
M=cv2.getPerspectiveTransform(p1,p2)
dst=cv2.warpPerspective(img,M,(col,row))
cv2.imshow("dst",dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
重映射
把一副图像的像素点放置到另外一幅图像的指定位置
dst=cv2.remap(src,map1,map2)
- 用来指定列的参数map1(mapx)内的值均为3
- 用来指定行的参数map2(mapy)内的值均为0
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
row,col=img.shape[:2]
map1=np.ones(img.shape,np.float32)*3
map2=np.zeros(img.shape,np.float32)
rst=cv2.remap(img,map1,map2,cv2.INTER_LINEAR)
print(img)
print(map1)
print(map2)
print(rst)
[[120 39 139 175 242]
[ 72 129 228 213 135]
[ 55 198 225 251 159]
[240 10 10 62 54]]
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[175 175 175 175 175]
[175 175 175 175 175]
[175 175 175 175 175]
[175 175 175 175 175]]
通过观察上述结果可知,目标图像(数组)dst内的所有值都来源于原始图像中第0行第3列上的175
复制
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
row,col=img.shape[:2]
mapx=np.zeros(img.shape,np.float32)
mapy=np.zeros(img.shape,np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),j)
mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print(img)
print(mapx)
print(mapy)
print(rst)
[[ 22 69 74 6 0]
[ 89 168 182 168 106]
[111 49 4 223 45]
[123 17 54 159 114]]
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
[[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3.]]
[[ 22 69 74 6 0]
[ 89 168 182 168 106]
[111 49 4 223 45]
[123 17 54 159 114]]
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
map1=np.zeros(img.shape[:2],np.float32)
map2=np.zeros(img.shape[:2],np.float32)
for i in range(col):
for j in range(row):
map1.itemset((i,j),j)
map2.itemset((i,j),i)
rst=cv2.remap(img,map1,map2,cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyWindow()
绕x轴翻转
使用与之前的函数方便
cv2.flip()
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
row,col=img.shape
mapx=np.zeros(img.shape,np.float32)
mapy=np.zeros(img.shape,np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),j)
mapy.itemset((i,j),row-1-i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print(img)
print(mapx)
print(mapy)
print(rst)
[[169 0 53 122 38]
[202 151 45 34 111]
[255 32 42 83 184]
[247 208 196 216 149]]
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
[[3. 3. 3. 3. 3.]
[2. 2. 2. 2. 2.]
[1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0.]]
[[247 208 196 216 149]
[255 32 42 83 184]
[202 151 45 34 111]
[169 0 53 122 38]]
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),j)
mapy.itemset((i,j),row-1-i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyWindow()
绕y轴翻转
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),col-j-1)
mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyWindow()
绕x轴,y轴翻转
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),col-j-1)
mapy.itemset((i,j),row-i-1)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyWindow()
x轴,y轴互换
- mapx的值调整为所在行的行号
- mapy的值调整为所在列的列号
注:如果行数和列数不一致,上述运算可能存在无法映射的情况,无法完成映射的值会被处理为0
import cv2
import numpy as np
img=cv2.imread("D:/hjb.jpg")
row,col=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(row):
for j in range(col):
mapx.itemset((i,j),i)
mapy.itemset((i,j),j)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow("img",img)
cv2.imshow("rst",rst)
cv2.waitKey(0)
cv2.destroyWindow()
图像缩放
- 在目标图像的x轴(0.25xlen,0.75xlen)区间内生成缩小图像;x轴其余区域的点取样自x轴上的任意一点的值
网友评论