我有一个仅包含红色和绿色通道的png图像。为了计算目的,我从图像中删除了蓝色通道。我需要计算这些像素的估计联合概率分布。我遇到了这个功能: numpy.random.multivariate_normal(mean,cov [,size]) 但是这个计算已知的分布。我需要计算估计的分布。有什么建议? 非常感谢。 Areej
答案 0 :(得分:4)
很容易将数据分成一组直方图
#2d histogram gives you the counts, in each cell
(H,redEdges,greedEdges) = numpy.histogram2d(
red.ravel(),green.ravel(),
bins=nbins
)
#divide by the total to get the probability of
#each cell -> the joint distribution
Prg = H/H.sum()
#sum over the `green` axis to get the `red` marginal (nx1)
Pr = H2d.sum(1)[:,numpy.newaxis]
#sum over the `red` axis to get the `green` marginal (1xn)
Pg = H2d.sum(0)[numpy.newaxis,:]
从那里互相信息很容易:
#calculate information contribution of each bin
dIrg = Prg*numpy.log(Prg/(Pr*Pg))
#filter nans and sum
Irg = dIrg[~numpy.isnan(dIrg)].mean()
答案 1 :(得分:0)
使用scipy,您可以使用许多适合数据的发行版。以下是如何执行此操作的示例,假设您从.png或.jpg或相关文件加载图像:
from PIL import Image
import numpy
import scipy.stats as ss
im = numpy.array(Image.open("myfile.png")
red = im[:,:,0]
green = im[:,:,1]
from matplotlib import pyplot
pyplot.hist(red.ravel())
pyplot.hist(green.ravel())
# if your data follow a normal distribution
print "red mean: %g sigma: %g" % ss.norm.fit(red.ravel())
print "green mean: %g sigma: %g" % ss.norm.fit(green.ravel())
如果您想要不同的发布,请将上面的norm
替换为其中之一:http://docs.scipy.org/doc/scipy/reference/stats.html