[英]How to get 2D histogram map of an image?
更具體地說,這是確切的要求。 我不確定如何說出這個問題。 我有一張圖片,大(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)
hist2d[0].shape
(256, 256)
因為它對應於每對256x256顏色。 精細
主要要求是,在一個單獨的圖像中,稱為與原始圖像具有相同形狀的result
,即(500, 500)
,我想用r
和g
通道的2d直方圖的值填充result
的每個元素
例如,如果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]]
但對於大圖像,這需要很長的時間來計算。 有這樣一種笨拙的方式嗎?
謝謝
只需使用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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.