基本上我有一些数据,我已经制作了直方图。没有很大的困难,只需使用matplotlib.pyplot.hist(data,bins=num)
但是,我想做一种Sobel边缘检测,基本上ith
直方图条/ bin(无论行话是什么)变为-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
上执行我的操作以获取我的sobel过滤的内容,(旁注:我只是在数组上迭代地执行此操作,是否有更简单的方式使用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
它会给我错误答案 0 :(得分:1)
我要说,使用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)