OpenCV灰度图像直方图算法实现

栏目: 编程工具 · 发布时间: 5年前

内容简介:手动实现一个灰度直方图算法,过程很简单,主要有以下几步: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()

效果如下:

OpenCV灰度图像直方图算法实现

更多 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灰度图像直方图算法实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Dream Machine

The Dream Machine

M. Mitchell Waldrop / Penguin Books / 2002-8 / USD 16.00

While most people may not be familiar with the name J. C. R. Licklider, he was the guiding spirit behind the greatest revolution of the modern era. At a time when most computers were big, ponderous ma......一起来看看 《The Dream Machine》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具