我试图获取某些图像的2D直方图值,但我真的迷失了它。如果我没错,我可以使用np.histogram2d()
中的numpy
来做到这一点。
我正在尝试的是
def hist2d(img, bins):
b_channel, g_channel, r_channel = img[:, :, 0], img[:, :, 1], img[:, :, 2]
channels = [b_channel, g_channel, r_channel]
c1 = np.histogram2d(b_channel,g_channel, bins=bins_per_hist)
c2 = np.histogram2d(g_channel,r_channel, bins=bins_per_hist)
c3 = np.histogram2d(r_channel,b_channel, bins=bins_per_hist)
# Finally concatenate results
# np.concatenate()
return result
我的想法是
您认为这个主意正确吗?我该如何使用np.histogram2d()
函数?我不明白如何传递垃圾箱值。 documentation说出2个值的列表,但是什么值呢?我只有一个。
注意:我正在用numpy
来做,但是也许另一个选择是openCV
。
非常感谢!我敢肯定它很简单,但我也想学习!
答案 0 :(得分:0)
在Python / OpenCV中更容易。
输入:
import cv2
# read image
img = cv2.imread('mandril3.jpg')
# calculate 2D histograms for pairs of channels: BG, GR, RB
histBG = cv2.calcHist([img], [0, 1], None, [256, 256], [0, 256, 0, 256])
histGR = cv2.calcHist([img], [1, 2], None, [256, 256], [0, 256, 0, 256])
histRB = cv2.calcHist([img], [2, 0], None, [256, 256], [0, 256, 0, 256])
# merge 3 single channel historgrams into one color histogram
hist = cv2.merge([histBG,histGR,histRB])
# view results
cv2.imshow("hist", hist)
cv2.waitKey(0)
# save result
# result is float and counts need to be scale to range 0 to 255
hist_scaled = cv2.normalize(hist, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
cv2.imwrite('mandril3_histogram.png', hist_scaled)
彩色直方图: