opencv_day05

#设计程序,使用函数cv2.resize()来生成一个与原始数组等大小的数组
#根据题目要求
#缩放,在opencv中利用cv2.resize()实现图像的缩放
#情况一:通过参数dsize指定,如果指定了dsize的值,那么无论是否指定参数fx和fy的值,都由dsize
#来标记图像的大小
#dsize内第一个参数对应缩放后图像的宽度,第二个参数对应缩放后图像的高度
import cv2
import numpy as np
img=np.ones([2,4,3],dtype=np.uint8)
size=img.shape[:2]
print(size)
rst=cv2.resize(img,size)
print('img.shape=\n',img.shape)
print('img=\n',img)
print('rst.shape=\n',rst.shape)
print('rst=\n',rst)

输出:
(2, 4)
img.shape=
(2, 4, 3)
img=
[[[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]

[[1 1 1]
[1 1 1]
[1 1 1]
[1 1 1]]]
rst.shape=
(4, 2, 3)
rst=
[[[1 1 1]
[1 1 1]]

[[1 1 1]
[1 1 1]]

[[1 1 1]
[1 1 1]]

[[1 1 1]
[1 1 1]]]

#使用函数cv2.resize()完成一个简单的图像缩放
import cv2
img=cv2.imread('./image/iu.jpeg')
rows,cols=img.shape[:2]#因为是BGR图像,有3个通道,是3维数组
size=(int(cols*0.9),int(rows*0.5))
rst=cv2.resize(img,size)
print('img.shape=',img.shape)
print('rst.shape=',rst.shape)

输出:
img.shape= (560, 700, 3)
rst.shape= (280, 630, 3)

#设计程序,控制函数cv2.resize()的fx参数,fy参数.完成图像缩放
#通过参数fx和fy指定,若参数dsize是None,那么通过目标图像的大小通过参数fx和fy来决定
import cv2
img=cv2.imread('./image/iu.jpeg')
rst=cv2.resize(img,None,fx=2,fy=0.5)
print('img.shape=',img.shape)
print('rst.shape=',rst.shape)

输出:
img.shape= (560, 700, 3)
rst.shape= (280, 1400, 3)

#设计程序,使用cv2.flip()完成图像的翻转
import cv2
img=cv2.imread('./image/iu.jpeg')
x=cv2.flip(img,0)#绕x轴翻转得到
y=cv2.flip(img,1)#绕y轴翻转得到
xy=cv2.flip(img,-1)#围绕x,y轴翻转得到
cv2.imshow('img',img)
cv2.imshow('x',x)
cv2.imshow('y',y)
cv2.imshow('xy',xy)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

在这里插入图片描述

#仿射变换是指图像经过一系列的几何变换来实现平移,旋转等操作
#仿射图像R=变换矩阵M*原始图像O
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
height,width=img.shape[:2]
x=100
y=200
M=np.float32([[1,0,x],[0,1,y]])
move=cv2.warpAffine(img,M,(width,height))
cv2.imshow('original',img)
cv2.imshow('move',move)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#设计程序,完成图像旋转
#retval=cv2.getRotationMatrix2D(center,angle,scale)
#center为旋转中心,angle为旋转角度,scale为变换尺度
import cv2
img=cv2.imread('./image/iu.jpeg')
height,width=img.shape[:2]
#正数逆时针转,负数顺时针转
M=cv2.getRotationMatrix2D((width/2,height/2),-45,0.6)
rotate=cv2.warpAffine(img,M,(width,height))
cv2.imshow('original',img)
cv2.imshow('rotation',rotate)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#设计程序,完成图像仿射
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
rows,cols,ch=img.shape
#src代表输入图像的三个点坐标,dst代表输出图像的三个点坐标
#记住此处第一个参数对应列数,第二个参数对应行数
p1=np.float32([[0,0],[cols-1,0],[0,rows-1]])
p2=np.float32([[0,rows*0.33],[cols*0.85,rows*0.25],[cols*0.15,rows*0.7]])
M=cv2.getAffineTransform(p1,p2)
dst=cv2.warpAffine(img,M,(cols,rows))
cv2.imshow('Original',img)
cv2.imshow('result',dst)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#设计程序,完成图像透视
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
rows,cols=img.shape[:2]
print(rows,cols)
pts1=np.float32([[150,50],[400,50],[60,450],[310,450]])
pts2=np.float32([[50,50],[rows-50,50],[50,cols-50],[rows-50,cols-50]])
#生成函数cv2.warpPerspective()所使用的转换矩阵
#该函数是cv2.getPerspectiveTransform(),其中第一个参数代表输入图像的四个顶点坐标
#dst代表输出图像的四个顶点坐标
M=cv2.getPerspectiveTransform(pts1,pts2)
#透视变换可以将矩形映射成任意四边形,而前面的仿射变换可以将矩形映射成任意平行四边形
dst=cv2.warpPerspective(img,M,(cols,rows))
cv2.imshow('img',img)
cv2.imshow('dst',dst)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#重映射
#把一幅图像内的像素点,放置到另外一幅图像内的指定位置
#dst=cv2.remap(src,map1,map2,interpolation[,borderValue]])
#我们想将目标图像中某个点A映射为原始图像内第x行第y列上的像素点,则将A点所对应的参数map1
#设为y,map2设为x
import cv2
import numpy as np
img=np.random.randint(0,256,(4,5),dtype=np.uint8)
print(img)
rows,cols=img.shape
mapx=np.ones(img.shape,np.float32)*3
mapy=np.ones(img.shape,np.float32)*0
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print('img=\n',img)
print('mapx=\n',mapx)
print('mapy=\n',mapy)
print('rst=\n',rst)

输出:
img=
[[162 145 62 39 102]
[ 79 228 233 79 212]
[144 178 230 70 228]
[249 187 84 200 65]]
mapx=
[[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]
[3. 3. 3. 3. 3.]]
mapy=
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
rst=
[[39 39 39 39 39]
[39 39 39 39 39]
[39 39 39 39 39]
[39 39 39 39 39]]

#复制
#通过cv2.remap()函数来实现图像的复制
#设计程序,使用函数cv2.remap()完成数组复制,
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
rows,cols=img.shape
mapx=np.zeros(img.shape,np.float32)
mapy=np.zeros(img.shape,np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),j)
        mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print('img=\n',img)
print('mapx=\n',mapx)
print('mapy=\n',mapy)
print('rst=\n',rst)

输出:
img=
[[199 139 239 126 99]
[ 25 149 165 13 134]
[ 19 96 141 107 97]
[147 158 20 26 96]]
mapx=
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
mapy=
[[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3.]]
rst=
[[199 139 239 126 99]
[ 25 149 165 13 134]
[ 19 96 141 107 97]
[147 158 20 26 96]]

#设计程序,使用函数cv2.remap()完成图像复制
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
rows,cols=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),j)
        mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow('original',img)
cv2.imshow('result',rst)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#设计程序,使用函数cv2.remap()实现数组绕x轴转
#OpenCv中行号的下标是从0开始的,存在对称关系为,'当前行号+对称行号=总行数-1'
#map1的值保持不变,map2的值变为rows-1-i
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
rows,cols=img.shape[:2]
mapx=np.zeros(img.shape,np.float32)
mapy=np.zeros(img.shape,np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),j)
        mapy.itemset((i,j),rows-1-i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print('img=\n',img)
print('mapx=\n',mapx)
print('mapy=\n',mapy)
print('rst=\n',rst)

输出:
img=
[[117 149 178 222 155]
[144 187 14 10 200]
[198 145 35 198 11]
[249 245 5 149 148]]
mapx=
[[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]
[0. 1. 2. 3. 4.]]
mapy=
[[3. 3. 3. 3. 3.]
[2. 2. 2. 2. 2.]
[1. 1. 1. 1. 1.]
[0. 0. 0. 0. 0.]]
rst=
[[249 245 5 149 148]
[198 145 35 198 11]
[144 187 14 10 200]
[117 149 178 222 155]]

#下面是实际操作
#设计程序,使用函数cv2.remap()实现图像绕x轴的翻转
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
rows,cols=img.shape[:2]
mapx=np.zeros(img.shape[:2],np.float32)
mapy=np.zeros(img.shape[:2],np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),j)
        mapy.itemset((i,j),rows-1-i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow('original',img)
cv2.imshow('result',rst)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

#绕y轴翻转,当前列号+对称列号=总列数-1
import cv2
import numpy as np
img=np.random.randint(0,256,size=[4,5],dtype=np.uint8)
rows,cols=img.shape
mapx=np.zeros(img.shape,np.float32)
mapy=np.zeros(img.shape,np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),cols-1-j)
        mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
print('img=\n',img)
print('mapx=\n',mapx)
print('mapy=\n',mapy)
print('rst=\n',rst)

输出:
img=
[[161 88 150 61 223]
[ 37 149 36 110 236]
[ 96 207 172 149 224]
[ 12 76 8 56 12]]
mapx=
[[4. 3. 2. 1. 0.]
[4. 3. 2. 1. 0.]
[4. 3. 2. 1. 0.]
[4. 3. 2. 1. 0.]]
mapy=
[[0. 0. 0. 0. 0.]
[1. 1. 1. 1. 1.]
[2. 2. 2. 2. 2.]
[3. 3. 3. 3. 3.]]
rst=
[[223 61 150 88 161]
[236 110 36 149 37]
[224 149 172 207 96]
[ 12 56 8 76 12]]

#设计程序,使用函数cv2.remap()实现图像绕y轴的翻转
import cv2
import numpy as np
img=cv2.imread('./image/iu.jpeg')
rows,cols=img.shape[:2]
mapx=np.ones(img.shape[:2],np.float32)
mapy=np.ones(img.shape[:2],np.float32)
for i in range(rows):
    for j in range(cols):
        mapx.itemset((i,j),cols-1-j)
        mapy.itemset((i,j),i)
rst=cv2.remap(img,mapx,mapy,cv2.INTER_LINEAR)
cv2.imshow('img',img)
cv2.imshow('rst',rst)
cv2.waitKey()
cv2.destroyAllWindows()

输出:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值