python寻峰算法_python中的快速寻峰与质心
我正在尝试用python开发一个快速算法来查找图像中的峰值,然后找到这些峰值的质心。我已经用scipy.ndimage.label和ndimage.find_对象编写了以下代码来定位对象。这似乎是代码中的瓶颈,在500x500图像中定位20个对象大约需要7毫秒。我想把它放大到更大的(2000x2000)图像,但是时间会增加到几乎100毫秒。所以,我想知道是否有一个更快的选择。
这是我到目前为止的代码,它有效,但速度很慢。首先,我用一些高斯峰来模拟我的数据。这一部分很慢,但实际上我将使用真实的数据,所以我不太在乎加速这一部分。我希望能很快找到山峰。import time
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage
import matplotlib.patches
plt.figure(figsize=(10,10))
ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(223)
ax4 = plt.subplot(224)
size = 500 #width and height of image in pixels
peak_height = 100 # define the height of the peaks
num_peaks = 20
noise_level = 50
threshold = 60
np.random.seed(3)
#set up a simple, blank image (Z)
x = np.linspace(0,size,size)
y = np.linspace(0,size,size)
X,Y = np.meshgrid(x,y)
Z = X*0
#now add some peaks
def gaussian(X,Y,xo,yo,amp=100,sigmax=4,sigmay=4):
return amp*np.exp(-(X-xo)**2/(2*sigmax**2) - (Y-yo)**2/(2*sigmay**2))
for xo,yo in size*np.random.rand(num_peaks,2):
widthx = 5 + np.random.randn(1)
widthy = 5 + np.random.randn(1)
Z += gaussian(X,Y,xo,yo,amp=peak_height,sigmax=widthx,sigmay=widthy)
#of course, add some noise:
Z = Z + scipy.ndimage.gaussian_filter(0.5*noise_level*np.random.rand(size,size),sigma=5)
Z = Z + scipy.ndimage.gaussian_filter(0.5*noise_level*np.random.rand(size,size),sigma=1)
t = time.time() #Start timing the peak-finding algorithm
#Set everything below the threshold to zero:
Z_thresh = np.copy(Z)
Z_thresh[Z_thresh
print 'Time after thresholding: %.5f seconds'%(time.time()-t)
#now find the objects
labeled_image, number_of_objects = scipy.ndimage.label(Z_thresh)
print 'Time after labeling: %.5f seconds'%(time.time()-t)
peak_slices = scipy.ndimage.find_objects(labeled_image)
print 'Time after finding objects: %.5f seconds'%(time.time()-t)
def centroid(data):
h,w = np.shape(data)
x = np.arange(0,w)
y = np.arange(0,h)
X,Y = np.meshgrid(x,y)
cx = np.sum(X*data)/np.sum(data)
cy = np.sum(Y*data)/np.sum(data)
return cx,cy
centroids = []
for peak_slice in peak_slices:
dy,dx = peak_slice
x,y = dx.start, dy.start
cx,cy = centroid(Z_thresh[peak_slice])
centroids.append((x+cx,y+cy))
print 'Total time: %.5f seconds\n'%(time.time()-t)
###########################################
#Now make the plots:
for ax in (ax1,ax2,ax3,ax4): ax.clear()
ax1.set_title('Original image')
ax1.imshow(Z,origin='lower')
ax2.set_title('Thresholded image')
ax2.imshow(Z_thresh,origin='lower')
ax3.set_title('Labeled image')
ax3.imshow(labeled_image,origin='lower') #display the color-coded regions
for peak_slice in peak_slices: #Draw some rectangles around the objects
dy,dx = peak_slice
xy = (dx.start, dy.start)
width = (dx.stop - dx.start + 1)
height = (dy.stop - dy.start + 1)
rect = matplotlib.patches.Rectangle(xy,width,height,fc='none',ec='red')
ax3.add_patch(rect,)
ax4.set_title('Centroids on original image')
ax4.imshow(Z,origin='lower')
for x,y in centroids:
ax4.plot(x,y,'kx',ms=10)
ax4.set_xlim(0,size)
ax4.set_ylim(0,size)
plt.tight_layout
plt.show()
尺寸=500的结果:
编辑:如果峰值的数量很大(~100),图像的大小很小,那么瓶颈实际上就是中心部分。所以,也许这部分的速度也需要优化。
python寻峰算法_python中的快速寻峰与质心相关推荐
- python归一化代码_python中的快速图像归一化
我觉得你的时间安排得很慢.也许你的安装出了问题?在 我试过这个测试程序:#!/usr/bin/python3 import sys import numpy as np import cv2 from ...
- python寻峰算法_python/scipy的寻峰算法
我可以自己写一些东西,通过找到一阶导数的零交叉点或其他东西,但它似乎是一个足够通用的函数,可以包含在标准库中.有人知道吗? 我的特殊应用是一个二维数组,但通常它会用于在FFT等中查找峰值. 具体地说, ...
- python去噪算法_Python实现图像去噪方式(中值去噪和均值去噪)
实现对图像进行简单的高斯去噪和椒盐去噪. 代码如下: import numpy as np from PIL import Image import matplotlib.pyplot as plt ...
- python入门算法_Python 算法入门教程
分治算法介绍 今天我们聊一聊计算机中非常重要和常用的一种算法:分治算法.它在计算机领域应用广泛,几乎无处不在.不仅计算机领域,在信号处理领域,分而治之也是十分常见的一种信号处理方法.著名快速傅里叶变换 ...
- python求对数_python中取对数
技术 | Python从零开始系列连载(二十七) 我们接着上一期的Python,继续跟大家分享有关Python中常用的数值计算和正则表达式. 运算符 1)数值运算 +.-.*./ % (求余数) // ...
- python归并算法_python归并算法
python数据结构与算法总结 python常用的数据结构与算法就分享到此处,本月涉及数据结构与算法的内容有如下文章: <数据结构和算法对python意味着什么?> <顺序表数据结构 ...
- python分治算法_Python算法:分治法
本节主要介绍分治法策略,提到了树形问题的平衡性以及基于分治策略的排序算法 本节的标题写全了就是:divide the problem instance, solve subproblems recur ...
- python map用法_Python中ChainMap的一种实用用法
Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 简而言之ChainMap:将多个字典视为一个,解锁Python超能力. Python标准库中的集合模块包含 ...
- python基本统计量_Python中简单统计量的计算
本篇文章给大家带来的内容是关于Python中简单统计量的计算,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 1.这些操作都要确保已经在电脑中安装好了Anaconda集成库,如果安装好 ...
最新文章
- GDC2016 Epic Games【Bullet Train】 新风格的VR-FPS的制作方法
- Facebook大牛、HipHop作者赵海平加入阿里巴巴
- 身体曲线如何反映出健康
- linux查找指定修改时间的文件夹,linux 查找某个日期以后修改过哪些文件 shell脚本...
- sql如何遍历几百万的表_SQL Server中遍历表中记录的方法
- jupyter notebook 安装教程
- 2018年第31周-hive支持的Delete和Update的配置
- 4、Windows2008 R2安装Vcenter5.0
- WeChat for Linux
- 网络安全相关证书有哪些?
- pug安装与使用教程
- 解决数据库数据粘贴到excel中换行、换列问题
- 移动硬盘无法访问怎么办?还能恢复数据吗?
- html将图片做成书的封面,把照片做成杂志 个人照片制作成报纸、杂志封面效果|封面设计软件[图文教程]...
- nginx高效学习方法
- 孙路弘说话就是生产力笔记
- MVCC和快照读丶当前读
- 高级计算机网络考试复习
- 北京化工大学计算机调剂要求,2020年北京化工大学考研调剂信息
- 人人车总部维权现场:员工无奈强冲 维权群近千人(图)
热门文章
- 无名2021/01/18
- 2022帆软BI数据分析大赛强势来袭,瓜分20W奖金
- 数学图形(2.2)N叶结
- 京东上什么卖得最好?
- 【更新】本地提权工具公开|CVE-2020-0796:微软发布SMBv3协议“蠕虫级”漏洞补丁通告
- NOI Online #2 普及组 第二题:荆轲刺秦王
- 怎样把多个pdf合并为一份?多个pdf怎么合并成一个pdf?
- python 报错in module,Centos 7 python 编译报错 ImportError: No module named six 解决办法
- Oracle导入导出dmp文件步骤
- 如何判断JS中变量的类型