基于python的空域变换
基于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 cvimg1=cv.imread('5.png')
img2=cv.imread('6.png')
#img1=cv.imread('LinuxLogo.jpg')
#img2=cv.imread('WindowsLogo.jpg')
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 cvimg1=cv.imread('5.png')
img2=cv.imread('6.png')
dst=img1*img2cv.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 cvimg1=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 npclass Img:def __init__(self,image,rows,cols,center=[0,0]):self.src=image #原始图像self.rows=rows #原始图像的行self.cols=cols #原始图像的列self.center=center #旋转中心,默认是[0,0]def Move(self,delta_x,delta_y): #平移#delta_x>0左移,delta_x<0右移#delta_y>0上移,delta_y<0下移self.transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]])def Zoom(self,factor): #缩放#factor>1表示缩小;factor<1表示放大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): #旋转#beta>0表示逆时针旋转;beta<0表示顺时针旋转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]=255else: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 npclass Img:def __init__(self,image,rows,cols,center=[0,0]):self.src=image #原始图像self.rows=rows #原始图像的行self.cols=cols #原始图像的列self.center=center #旋转中心,默认是[0,0]def Move(self,delta_x,delta_y): #平移#delta_x>0左移,delta_x<0右移#delta_y>0上移,delta_y<0下移self.transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]])def Zoom(self,factor): #缩放#factor>1表示缩小;factor<1表示放大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): #旋转#beta>0表示逆时针旋转;beta<0表示顺时针旋转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]=255else: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 npclass Img:def __init__(self,image,rows,cols,center=[0,0]):self.src=image #原始图像self.rows=rows #原始图像的行self.cols=cols #原始图像的列self.center=center #旋转中心,默认是[0,0]def Move(self,delta_x,delta_y): #平移#delta_x>0左移,delta_x<0右移#delta_y>0上移,delta_y<0下移self.transform=np.array([[1,0,delta_x],[0,1,delta_y],[0,0,1]])def Zoom(self,factor): #缩放#factor>1表示缩小;factor<1表示放大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): #旋转#beta>0表示逆时针旋转;beta<0表示顺时针旋转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]=255else: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)
后续
如果想了解更多物联网、智能家居项目知识,可以关注我的项目实战专栏。
或者关注公众号。
编写不易,感谢支持。
基于python的空域变换相关推荐
- Python+opencv 机器视觉 - 基于霍夫圈变换算法检测图像中的圆形实例演示
Python+opencv 机器视觉 - 基于霍夫圈变换算法检测图像中的圆形实例演示 第一章:霍夫变换检测圆 ① 实例演示1 ② 实例演示2 ③ 霍夫变换函数解析 第二章:Python + openc ...
- 基于python的图像Gabor变换及特征提取
基于python的图像Gabor变换及特征提取 1.前言 2. "Gabor帮主"简介 3."Gabor帮主"大招之图像变换 3."Gabor帮主&q ...
- 【CV】OpenCV(基于Python)学习笔记
以下内容中的页码均来自<OpenCV 4详解 : 基于Python> 目录 第2章 载入.显示与保存数据 2.2 图像的读取与显示 2.2.1 图像读取函数 cv.imread() 2.2 ...
- 基于Python的频谱分析(一)
1.傅里叶变换 傅里叶变换是信号领域沟通时域和频域的桥梁,在频域里可以更方便的进行一些分析.傅里叶主要针对的是平稳信号的频率特性分析,简单说就是具有一定周期性的信号,因为傅里叶变换采取的是有限取样 ...
- 基于python的快速傅里叶变换FFT(二)
基于python的快速傅里叶变换FFT(二) 本文在上一篇博客的基础上进一步探究正弦函数及其FFT变换. 知识点 FFT变换,其实就是快速离散傅里叶变换,傅立叶变换是数字信号处理领域一种很重要的算 ...
- 基于python的快速傅里叶变换FFT(一)
基于python的快速傅里叶变换FFT(一) FFT可以将一个信号变换到频域.有些信号在时域上是很难看出什么特征的,但是如果变换到频域之后,就很容易看出特征了.这就是很多信号分析采用FFT变换的原因. ...
- 从Theano到Lasagne:基于Python的深度学习的框架和库
从Theano到Lasagne:基于Python的深度学习的框架和库 [日期:2015-08-03] 来源:http://creative-punch.net/ 作者:Creative Punch ...
- python计算商品总价_GitHub - ideaOzy/data_analysis: 基于Python的南京二手房数据采集及可视化分析...
基于Python的南京二手房数据采集及可视化分析 1 内容简介 首先通过爬虫采集链家网上所有南京二手房的房源数据,并对采集到的数据进行清洗:然后,对清洗后的数据进行可视化分析,探索隐藏在大量数据背后的 ...
- 频谱分析:基于python画出时域频域波形
一,FFT解释 FFT(Fast Fourier Transformation)是离散傅氏变换(DFT)的快速算法.即为快速傅氏变换.它是根据离散傅氏变换的奇.偶.虚.实等特性,对离散傅立叶变换的算法 ...
最新文章
- pandas.set_index()
- 趋势科技4月移动client病毒报告
- initMNN: init numpy failed mnn笔记
- John细说PHP的验证码
- hibernate_day03_一对多相关操作
- matlab中udt函数,《MATLAB信号处理超级学习手册》——2.5 离散时间信号中的运算...
- 一步一步学ROP之linux_x64篇
- Javascript图片滚动
- Python中的文件对比(difflib文件对比、Linux文件对比)
- swiper监听滚动条_swiper Scrollbar滚动条组件详解
- Nginx常用使用解读及配置
- php 调试环境配置
- Matlab 求全要素生产率,如何用DEAP进行全要素生产率分析
- 语料库资源————(一)
- 深入理解计算机系统02——信息的表示与处理
- 手绘板的制作——重置与橡皮擦(2)
- matlab 读取.pgm,【数字图像处理】pbm/pgm/ppm图片的读写(Matlab)
- PAT 甲级1021 Deepest Root
- 框架协议、合同的下达
- A870省电内核超频内核编译及制作