基于python的空域变换
空域变换加法运算减法运算乘法运算逻辑运算缩放平移旋转
后续
空域变换
空域:是指图像所在的平面,即像素位置所在的空间。空域变换:对像素点的位置和灰度值根据图像变化目的需要,对图像矩阵进行运算操作,形成另一幅图像。空域变换分类:算术逻辑变换、几何变换、灰度变换、直方图变换。
加法运算
主要应用
去除叠加性噪声生成图像叠加效果
import cv2
as cv
img1
= cv
.imread
("1.jpg")
img2
= cv
.imread
("2.jpg")
print(img1
.shape
,img2
.shape
)
img1
=cv
.resize
(img1
,(img2
.shape
[1],img2
.shape
[0]))
image
=cv
.addWeighted
(img1
,0.6,img2
,0.4,0.0,)
cv
.imshow
('img1',image
)
cv
.waitKey
(0)
cv
.destroyAllWindows
()
减法运算
“主要运用”
显示两幅图像的差异,检测同一场景两幅图像之间的变化,如:视频中镜头边界的检测去除不需要的叠加性图案图像分割:如分割运动的车辆,减法去掉静止部分,剩余的是运动元素和噪声
import cv2
as cv
img1
=cv
.imread
('5.png')
img2
=cv
.imread
('6.png')
dst
=cv
.add
(img1
,img2
)
dst1
=cv
.subtract
(img1
,img2
)
cv
.imshow
('dst',dst1
)
cv
.imshow
('dst1',dst
)
cv
.waitKey
(0)
cv
.destroyAllWindows
()
乘法运算
主要应用 图像的局部显示,如:用二值蒙板图像与原图像做乘法
import cv2
as cv
img1
=cv
.imread
('5.png')
img2
=cv
.imread
('6.png')
dst
=img1
*img2
cv
.imshow
('181360152',dst
)
cv
.waitKey
(0)
cv
.destroyAllWindows
()
逻辑运算
非运算主要运用:图像求反,g(x,y)=255-f(x,y)与运算主要用于:两个图像相交子集,提取感兴趣子图像,g(x,y)=f(x,y)^h(x,y)
import cv2
as cv
img1
=cv
.imread
('LinuxLogo.jpg')
img2
=cv
.imread
('WindowsLogo.jpg')
and_img
=cv
.bitwise_and
(img1
,img2
)
or_img
=cv
.bitwise_or
(img1
,img2
)
not_img
=cv
.bitwise_not
(img1
)
xor_img
=cv
.bitwise_xor
(img1
,img2
)
cv
.imshow
('181360152',and_img
)
cv
.imshow
('181360152zhang',or_img
)
cv
.imshow
('181360152yang',not_img
)
cv
.imshow
('181360152-',xor_img
)
cv
.waitKey
(0)
cv
.destroyAllWindows
()
缩放
import cv2
import math
import numpy
as np
class Img:
def __init__(self
,image
,rows
,cols
,center
=[0,0]):
self
.src
=image
self
.rows
=rows
self
.cols
=cols
self
.center
=center
def Move(self
,delta_x
,delta_y
):
self
.transform
=np
.array
([[1,0,delta_x
],[0,1,delta_y
],[0,0,1]])
def Zoom(self
,factor
):
self
.transform
=np
.array
([[factor
,0,0],[0,factor
,0],[0,0,1]])
def Horizontal(self
):
self
.transform
=np
.array
([[1,0,0],[0,-1,self
.cols
-1],[0,0,1]])
def Vertically(self
):
self
.transform
=np
.array
([[-1,0,self
.rows
-1],[0,1,0],[0,0,1]])
def Rotate(self
,beta
):
self
.transform
=np
.array
([[math
.cos
(beta
),-math
.sin
(beta
),0],
[math
.sin
(beta
), math
.cos
(beta
),0],
[ 0, 0, 1]])
def Process(self
):
self
.dst
=np
.zeros
((self
.rows
,self
.cols
),dtype
=np
.uint8
)
for i
in range(self
.rows
):
for j
in range(self
.cols
):
src_pos
=np
.array
([i
-self
.center
[0],j
-self
.center
[1],1])
[x
,y
,z
]=np
.dot
(self
.transform
,src_pos
)
x
=int(x
)+self
.center
[0]
y
=int(y
)+self
.center
[1]
if x
>=self
.rows
or y
>=self
.cols
or x
<0 or y
<0:
self
.dst
[i
][j
]=255
else:
self
.dst
[i
][j
]=self
.src
[x
][y
]
if __name__
=='__main__':
src
=cv2
.imread
('123.jpg',0)
rows
= src
.shape
[0]
cols
= src
.shape
[1]
cv2
.imshow
('src', src
)
img
=Img
(src
,rows
,cols
,[248,231])
img
.Zoom
(0.5)
img
.Process
()
cv2
.imshow
('dst', img
.dst
)
cv2
.waitKey
(0)
平移
import cv2
import math
import numpy
as np
class Img:
def __init__(self
,image
,rows
,cols
,center
=[0,0]):
self
.src
=image
self
.rows
=rows
self
.cols
=cols
self
.center
=center
def Move(self
,delta_x
,delta_y
):
self
.transform
=np
.array
([[1,0,delta_x
],[0,1,delta_y
],[0,0,1]])
def Zoom(self
,factor
):
self
.transform
=np
.array
([[factor
,0,0],[0,factor
,0],[0,0,1]])
def Horizontal(self
):
self
.transform
=np
.array
([[1,0,0],[0,-1,self
.cols
-1],[0,0,1]])
def Vertically(self
):
self
.transform
=np
.array
([[-1,0,self
.rows
-1],[0,1,0],[0,0,1]])
def Rotate(self
,beta
):
self
.transform
=np
.array
([[math
.cos
(beta
),-math
.sin
(beta
),0],
[math
.sin
(beta
), math
.cos
(beta
),0],
[ 0, 0, 1]])
def Process(self
):
self
.dst
=np
.zeros
((self
.rows
,self
.cols
),dtype
=np
.uint8
)
for i
in range(self
.rows
):
for j
in range(self
.cols
):
src_pos
=np
.array
([i
-self
.center
[0],j
-self
.center
[1],1])
[x
,y
,z
]=np
.dot
(self
.transform
,src_pos
)
x
=int(x
)+self
.center
[0]
y
=int(y
)+self
.center
[1]
if x
>=self
.rows
or y
>=self
.cols
or x
<0 or y
<0:
self
.dst
[i
][j
]=255
else:
self
.dst
[i
][j
]=self
.src
[x
][y
]
if __name__
=='__main__':
src
=cv2
.imread
('123.jpg',0)
rows
= src
.shape
[0]
cols
= src
.shape
[1]
cv2
.imshow
('src', src
)
img
=Img
(src
,rows
,cols
,[248,231])
img
.Move
(-30, -50)
img
.Process
()
cv2
.imshow
('dst', img
.dst
)
cv2
.waitKey
(0)
旋转
import cv2
import math
import numpy
as np
class Img:
def __init__(self
,image
,rows
,cols
,center
=[0,0]):
self
.src
=image
self
.rows
=rows
self
.cols
=cols
self
.center
=center
def Move(self
,delta_x
,delta_y
):
self
.transform
=np
.array
([[1,0,delta_x
],[0,1,delta_y
],[0,0,1]])
def Zoom(self
,factor
):
self
.transform
=np
.array
([[factor
,0,0],[0,factor
,0],[0,0,1]])
def Horizontal(self
):
self
.transform
=np
.array
([[1,0,0],[0,-1,self
.cols
-1],[0,0,1]])
def Vertically(self
):
self
.transform
=np
.array
([[-1,0,self
.rows
-1],[0,1,0],[0,0,1]])
def Rotate(self
,beta
):
self
.transform
=np
.array
([[math
.cos
(beta
),-math
.sin
(beta
),0],
[math
.sin
(beta
), math
.cos
(beta
),0],
[ 0, 0, 1]])
def Process(self
):
self
.dst
=np
.zeros
((self
.rows
,self
.cols
),dtype
=np
.uint8
)
for i
in range(self
.rows
):
for j
in range(self
.cols
):
src_pos
=np
.array
([i
-self
.center
[0],j
-self
.center
[1],1])
[x
,y
,z
]=np
.dot
(self
.transform
,src_pos
)
x
=int(x
)+self
.center
[0]
y
=int(y
)+self
.center
[1]
if x
>=self
.rows
or y
>=self
.cols
or x
<0 or y
<0:
self
.dst
[i
][j
]=255
else:
self
.dst
[i
][j
]=self
.src
[x
][y
]
if __name__
=='__main__':
src
=cv2
.imread
('123.jpg',0)
rows
= src
.shape
[0]
cols
= src
.shape
[1]
cv2
.imshow
('src', src
)
img
=Img
(src
,rows
,cols
,[248,231])
img
.Rotate
(-math
.radians
(180))
img
.Process
()
cv2
.imshow
('dst', img
.dst
)
cv2
.waitKey
(0)
后续
如果想了解更多物联网、智能家居项目知识,可以关注我的项目实战专栏。 或者关注公众号。
编写不易,感谢支持。