基本上我有一些数据,我已经根据这些数据制作了直方图。没有太大困难,只需使用 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)
关于python - 直方图的边缘检测 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18809392/