如何在OpenCV中绘制图像的3D直方图

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

[更新]我找到更多的例子,我现在可以做到Can I plot several histograms in 3d?

我知道这个问题已经问过了,我试试这个How to calculate 3D histogram in python using open CV但它不起作用

我想要像这样的3D histogram

这就是我现在拥有的My Graph

hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv_image)

fig = plt.figure(figsize=(8,7))
ax = plt.axes(projection='3d'), plt.title("Histogram 3D")
plt.hist(h.ravel(), 256, [0, 256])
plt.hist(s.ravel(), 256, [0, 256])
plt.hist(v.ravel(), 256, [0, 256])

我可以使用plt.hist()绘制3d条形图或者我还需要更多东西吗?

我一直在寻找图像教程的3d直方图,但我找不到任何

python opencv matplotlib 3d histogram
1个回答
3
投票

这是我的结果:enter image description here


码:

#!/usr/bin/python3
# 2017.12.20 14:00:12 CST
# 2017.12.20 14:26:08 CST

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
import cv2

img = cv2.imread("panda.png")
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h,s,v = cv2.split(hsv)
fig = plt.figure()

ax = fig.add_subplot(111, projection='3d')
for x, c, z in zip([h,s,v], ['r', 'g', 'b'], [30, 20, 10]):
    xs = np.arange(256)
    ys = cv2.calcHist([x], [0], None, [256], [0,256])
    cs = [c] * len(xs)
    cs[0] = 'c'
    ax.bar(xs, ys.ravel(), zs=z, zdir='y', color=cs, alpha=0.8)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.