How to plot 3D histogram of an image in OpenCV

2019-03-01 07:06发布

[Update] I find more example and i can do it now Can I plot several histograms in 3d?

I know this question is already ask and i try this How to calculate 3D histogram in python using open CV but it doesn't work

I want something like this 3D histogram

this is what i have now 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])

can i use just plt.hist() to plot 3d bar or I need something more?

i have been searching for 3d histogram of an image tutorial but i can't find any

1条回答
欢心
2楼-- · 2019-03-01 08:08

This is my result: enter image description here


Code:

#!/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()
查看更多
登录 后发表回答