内容简介:手动实现一个灰度直方图算法,过程很简单,主要有以下几步:1. 统计每一个像素灰度值2. 计算每个灰度值出现的概率
手动实现一个灰度直方图算法,过程很简单,主要有以下几步:
1. 统计每一个像素灰度值
2. 计算每个灰度值出现的概率
3. 横坐标 0-255
4. 纵坐标为概率P
直方图效果如下:
# 本质: 统计每一个像素灰度 出现的概率 横坐标 0-255 纵坐标 概率P
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('wwww.linuxidc.com.jpg', 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
count = np.zeros(256, np.float) # 因为是概率, 有可能是浮点数
# 统计像素个数并计算概率
for i in range(height):
for j in range(width):
pixel = gray[i, j]
index = int(pixel)
count[index] = count[index] + 1
total = height * width # 总像素个数
count = count / total # 计算概率
# 画图
x = np.linspace(0, 255, 256)
y = count
plt.bar(x, y, 0.9, alpha = 1, color = "b")
plt.show()
效果如下:
更多 Python 相关信息见 Python 专题页面 https://www.linuxidc.com/topicnews.aspx?tid=17
Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址: https://www.linuxidc.com/Linux/2019-05/158629.htm
以上所述就是小编给大家介绍的《OpenCV灰度图像直方图算法实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Real-Time Collision Detection
Christer Ericson / CRC Press / 2004-12-22 / USD 98.95
Written by an expert in the game industry, Christer Ericson's new book is a comprehensive guide to the components of efficient real-time collision detection systems. The book provides the tools and kn......一起来看看 《Real-Time Collision Detection》 这本书的介绍吧!