如何获取图像的二维直方图?

问题描述 投票:1回答:1

更具体地说,这是确切的要求。我不确定如何说出这个问题。我有一个像(500,500)大小的图像。我只提取r和g通道

r = image[:, :, 0]
g = image[:, :, 1]

然后,我计算r和g的2D直方图

hist2d = np.histogram2d(r, g, bins=256, range=[(255,255),(255,255)])

现在,hist2d[0].shape(256, 256),因为它对应于每对256x256颜色。精细

主要的要求是,在一个单独的图像中,称为result,其形状与原始图像相同,即(500, 500),我想用resultr通道的2d直方图的值填充g的每个元素

例如,如果r[200,200]是23而g[200, 200]是26,我想放置result[200, 200] = hist2d[0][23, 26]

这样做的天真方法是简单的python循环。

for i in range(r.shape[0]):
    for j in range(r.shape[1]):
        result[i, j] = hist2d[0][r[i, j], g[i, j]]

但对于大图像,这需要很长的时间来计算。有这样一种笨拙的方式吗?

谢谢

python numpy opencv scipy
1个回答
3
投票

只需使用hist2d[0][r, g]

import numpy as np

r, g, b = np.random.randint(0, 256, size=(3, 500, 500)).astype(np.uint8)
hist2d = np.histogram2d(r.ravel(), g.ravel(), bins=256, range=[[0, 256], [0, 256]])
hist2d[0][r, g]
© www.soinside.com 2019 - 2024. All rights reserved.