【问题标题】:How to create histogram of binary image?如何创建二值图像的直方图?
【发布时间】:2018-11-17 14:54:03
【问题描述】:

我有二值图像(只有两种颜色,黑白),

我想创建图像的直方图。我试过这些代码:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('bin_003.png')
color = ('b','g','r')
for i,col in enumerate(color):
    histr = cv2.calcHist([img],[i],None,[256],[0,256])
    plt.plot(histr,color = col)
    plt.xlim([0,256])
plt.show()

但代码显示 而不是在 x 轴上只显示 0 和 1。

【问题讨论】:

  • 白色是 (255,255,255) 所以我不知道你为什么感到惊讶。
  • 其实我并不意外,我的意思是,我想让轴变成只有“0”和“1”。例如,“0”代表 0,“1”代表 255 @MarkRansom

标签: python numpy matplotlib image-processing histogram


【解决方案1】:

由于这里有一张黑白图像,它应该只有一个通道。您不需要 RGB 通道。您可以使用plt.hist 创建单个直方图。

from matplotlib import pyplot as plt

img = plt.imread('https://i.stack.imgur.com/y19dr.png')

plt.hist(img.flatten(), bins=[-.5,.5,1.5], ec="k")
plt.xticks((0,1))
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2017-07-27
    • 2018-04-16
    • 2011-10-30
    • 1970-01-01
    • 2013-05-02
    • 2011-01-09
    相关资源
    最近更新 更多