【问题标题】:Drawing a histogram in opencv python在opencv python中绘制直方图
【发布时间】:2018-01-06 10:14:15
【问题描述】:

我想通过处理前置摄像头视频来检测车辆的车道偏离。为此,我想水平选择每一帧的一条像素线并分析这些像素颜色如何变化。为此,我想绘制 R、G、B 通道的直方图,这些通道在 x 轴上具有从左到右的像素数,在 y 轴上具有相应的像素值。由于我是这个领域的新手,我想知道如何使用 opencv 和 python 为视频中的每一帧绘制直方图?谢谢。

【问题讨论】:

  • 如何选择那个像素?是手动的吗?您能否详细说明到目前为止您所做的尝试?
  • 一个像素线?你的意思是线条的宽度是一个像素?
  • 是的。我想选择代表道路区域颜色变化的图像的一行。手动选择的那条线总是穿过图像的道路区域。

标签: python opencv image-processing


【解决方案1】:

这是图像的直方图(opencv+matplotlib):

#!/usr/bin/python3
# 2017.12.20 14:00:12 CST
# 2018.01.06 19:07:57 CST
import matplotlib.pyplot as plt
import numpy as np
import cv2

img = cv2.imread("test.png")
b,g,r = cv2.split(img)
fig = plt.figure(figsize=(8,4))

ax = fig.add_subplot(121)
ax.imshow(img[...,::-1])

ax = fig.add_subplot(122)
for x, c in zip([b,g,r], ["b", "g", "r"]):
    xs = np.arange(256)
    ys = cv2.calcHist([x], [0], None, [256], [0,256])
    ax.plot(xs, ys.ravel(), color=c)

ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()


这里有两个关于绘制 3D 直方图的例子,你可以参考一下。

(1) How to plot 3D histogram of an image in OpenCV

(2)3d plot of list of (hist, bin_edges) where histogram bar chart or lines are in the z-y plane

【讨论】:

  • 感谢您的帮助。有没有办法将图像的一行中的像素获取到 x 轴并将颜色值获取到 y 轴,而不是获取每个颜色值的像素总数?
猜你喜欢
  • 2012-03-12
  • 2015-05-29
  • 2015-11-11
  • 2011-08-21
  • 1970-01-01
  • 2023-03-03
  • 2018-05-23
  • 1970-01-01
  • 2018-06-02
相关资源
最近更新 更多