如何在 OpenCV 中绘制图像的 3D 直方图 [英] How to plot 3D histogram of an image in OpenCV

查看:33
本文介绍了如何在 OpenCV 中绘制图像的 3D 直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新] 我找到了更多例子,我现在就可以做到

<小时>

代码:

#!/usr/bin/python3# 2017.12.20 14:00:12 CST# 2017.12.20 14:26:08 CST从 mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 plt将 numpy 导入为 np导入 cv2img = 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, 投影='3d')对于 zip([h,s,v], ['r', 'g', 'b'], [30, 20, 10]) 中的 x, c, z: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()

[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

解决方案

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

这篇关于如何在 OpenCV 中绘制图像的 3D 直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆