python - 直方图的边缘检测 - python

标签 python matplotlib

基本上我有一些数据,我已经根据这些数据制作了直方图。没有太大困难,只需使用 matplotlib.pyplot.hist(data,bins=num) 然而,我想做一种索贝尔边缘检测,基本上是 ith直方图条/箱(无论行话是什么)变成 -2*(i-1)th+0*(i)th+2*(i+1)th 我已经弄清楚/发现你可以做到(我的数据位于列式txt文件中)

import matplotlib.pyplot as plt

alldata = np.genfromtxt('filename', delimiter=' ')
data = alldata[:,18]
n,bins,patches = plt.hist(data,bins=30)

哪个返回/给予

>>> n
array([3,0,3,3,6,1,...etc])

>>> bins 
array([13.755,14.0298,14.3046,... etc])

>>> patches
<a list of 30 Patch objects>

在这里我可以对 n 执行操作得到我的索贝尔过滤的东西,(旁注:我只是在数组上迭代地执行此操作,有没有更简单的方法来处理像 a = [-2,0,2] 这样的东西?)

所以,我的问题和问题! 我不知道如何将结果重建为直方图或线图......并保持相同的水平轴 bins

<小时/>

更新

<小时/>

这是我到目前为止使用的代码。下载数据HERE

import numpy as np
import matplotlib.pyplot as plt

# ignore this, it is so it makes it easier to iterate over later.
filNM = 'S_MOS152_cut'
filID = filNM + '.txt'
nbins = 30

# extract the data from file
stars = np.genfromtxt(filID, delimiter=' ')
imag = stars[:,18]

# let's start the histogram dance
n,bins,patches = plt.hist(imag, bins=nbins)

# now apply the edge filter (manually for lack of a better way)
nnew=[0 for j in xrange(nbins)]

for i in range(0,len(n)):
if i==0:
    nnew[i]=2*n[i+1]
elif i==len(n)-1:
    nnew[i]=-2*n[i-1]
else:
    nnew=-2*n[i-1]+2*n[i+1]

np.array(nnew)
# I do this because it now generates the same form 
# output as if you just say >>> print plt.hist(imag, bins=nbins)
filt = nnew,bins,patches 
print filt

如果我尝试绘制 filt 现在我无处可去它给了我错误

最佳答案

我会说,使用np.histogaram制作直方图,应用Sobel过滤器,然后plt.hist它。

>>> n, bins=histogram(q, bins=30)
>>> bin_updated=[item for item, jtem in zip(bins, n) if do_Sobel_stuff_on(jtem)]
>>> plt.hist(data, bins=bin_updated)

好的,基本上,你想使用 .set_height() 方法:

>>> a=range(1000)
>>> n,b,p=plt.hist(a)
>>> p[0]
<matplotlib.patches.Rectangle object at 0x026E1070>
>>> p[0].get_height()
100
>>> p[0].set_height(19)
>>> plt.show()
>>> n_adjusted #your new n
>>> for i1, i2 in zip(p, n_adjusted):
        i1.set_height(i2)

enter image description here

关于python - 直方图的边缘检测 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18809392/

相关文章:

python - 如何从字符串中提取 float

python - 为什么 fileinput.input 对象在超出范围时不会丢失?

python - 有没有更好的方法从 Python 中的文件中读取元素?

python - Pandas 注释数据框历史

python - matplotlib 仅绘制日期而不是给定的日期时间变量

python - 在一个函数中调用2个函数

python - 如果值为 true 但为 null 则不起作用

python - 如何使用 matplotlib 为 3D 散点图创建标记大小图例?

python - 颜色条勾号格式不起作用

python - 更改 DataFrame 中的子图颜色?