How to plot 3D histogram of an image in OpenCV

馋奶兔 提交于 2019-12-20 04:30:28

问题


[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:


This is my result:


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()


来源:https://stackoverflow.com/questions/47899253/how-to-plot-3d-histogram-of-an-image-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!