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

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

内容简介:手动实现一个灰度直方图算法,过程很简单,主要有以下几步: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灰度图像直方图算法实现》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

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

C++ How to Program (5th Edition) (How to Program)

C++ How to Program (5th Edition) (How to Program)

Harvey & Paul) Deitel & Associates / Prentice Hall / 2005-01-05 / USD 98.00

With over 250,000 sold, Harvey and Paul Deitel's C++ How to Program is the world's best-selling introduction to C++ programming. Now, this classic has been thoroughly updated! The Deitels' groundbreak......一起来看看 《C++ How to Program (5th Edition) (How to Program)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码