统计的图像中像素灰度值
在计算机领域中,灰度(Gray scale)数字图像是每个像素只有一个采样颜色的图像。
代码展示:
import cv2
from matplotlib import pyplot as pyplot
img = cv2.imread('data/data131367/微信图片_20220308140220.jpg', 1)
#收集到的数据只考虑灰度值所以为1
#采用直方图的形式输出
pyplot.hist(img.reshape([-1]), 256, [0, 256])
pyplot.show()
#采用折线图的形式输出
import cv2
from matplotlib import pyplot as pyplot
img = cv2.imread('data/data131367/微信图片_20220308140220.jpg',1)
#收集到的数据只考虑灰度值所以为1
color = ('r', 'g', 'b')
for i, col in enumerate(color):
histr = cv2.calcHist([img], [i], None, [256], [0,256])
pyplot.plot(histr, col)
pyplot.xlim([0,256])
pyplot.show()
cv2.calcHist解释:
def calcHist(images: Any,
channels: Any,
mask: Any,
histSize: Any,
ranges: Any,
hist: Any = None,
accumulate: Any = None) -> Non
enumerate python官网解释:
enumerate
(iterable, start=0)Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.
>>>
应用实例:
>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')] >>> list(enumerate(seasons, start=1)) [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]