Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何创建二值图像的直方图?_Python_Numpy_Matplotlib_Image Processing_Histogram - Fatal编程技术网

Python 如何创建二值图像的直方图?

Python 如何创建二值图像的直方图?,python,numpy,matplotlib,image-processing,histogram,Python,Numpy,Matplotlib,Image Processing,Histogram,我有二值图像(只有两种颜色,黑色和白色) 我想创建图像的直方图。我已尝试使用以下代码: 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]

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

我想创建图像的直方图。我已尝试使用以下代码:

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。

因为这里有黑白图像,所以它应该只有一个通道。你不需要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()

因为这里有黑白图像,所以它应该只有一个频道。你不需要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()

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