如何仅计算 png 图像中红色和绿色像素的估计联合概率分布?

发布于 2024-12-29 06:45:35 字数 164 浏览 1 评论 0原文

我有一个仅包含红色和绿色通道的 png 图像。为了计算目的,我从图像中删除了蓝色通道。我需要计算这些像素的估计联合概率分布。我遇到了这个函数: numpy.random.multivariate_normal(平均值, cov[, 大小]) 但这是计算已知的分布。我需要计算估计分布。有什么建议吗? 多谢。 阿雷吉

I have a png image that contains the red and green channels only. I removed the blue channel from the image for calculation purposes. I need to calculate the estimated joint probability distribution for these pixels. I came across this function:
numpy.random.multivariate_normal(mean, cov[, size])
but this one computes the known distribution. I need to calculate the estimated distribution. Any suggestions?
Thanks a lot.
Areej

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2025-01-05 06:45:35

将数据分成一组直方图很容易

#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()

it's easy to bin the data into a set of histograms

#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,:]

From there the mutual information is easy:

#calculate information contribution of each bin
dIrg = Prg*numpy.log(Prg/(Pr*Pg))
#filter nans and sum
Irg = dIrg[~numpy.isnan(dIrg)].mean()
怀中猫帐中妖 2025-01-05 06:45:35

使用 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 替换为以下之一:< a href="http://docs.scipy.org/doc/scipy/reference/stats.html" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/stats.html< /a>

Using scipy, there are a number of distributions you can fit to data. Here's an example of how to do that, assuming you're loading your image from a .png or .jpg or related file:

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())

If you want a different distribution, replace norm above with one of these: http://docs.scipy.org/doc/scipy/reference/stats.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文