码迷,mamicode.com
首页 > 编程语言 > 详细

直方图均衡化-Python实现

时间:2020-03-12 17:10:29      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:email   for   value   utf-8   pix   outlook   lin   img   计算过程   

需要均衡的图像

将下面的图像进行直方图均衡

1 3 9 9 8
2 1 3 7 3
3 6 0 6 4
6 8 2 0 5
2 9 2 6 0

均衡化计算过程

使用python进行直方图均衡化:

# -*- coding: utf-8 -*-

# @Time    : 2020/3/7 23:30
# @Author  : focksor
# @Email   : focksor@outlook.com

# 原始图像
img = [
    [1, 3, 9, 9, 8],
    [2, 1, 3, 7, 3],
    [3, 6, 0, 6, 4],
    [6, 8, 2, 0, 5],
    [2, 9, 2, 6, 0],
]


counter = {}
for i in range(10):
    counter[i] = 0

# 统计各级灰度频数
for line in img:
    for i in line:
        counter[i] += 1
print("各级频数:", counter)

# 计算各级灰度概率
pixel_num = sum(counter.values())
for k in counter.keys():
    counter[k] /= pixel_num
print("各级概率:", counter)

# 求各级累积概率
sum_probability = {}
for i in range(10):
    sum_probability[i] = 0
    for k in counter.keys():
        if k <= i:
            sum_probability[i] += counter[k]
print("累计概率:", sum_probability)

# 打印灰阶映射表
for i in sum_probability:
    sum_probability[i] = round(sum_probability[i] * 9)
print("映射到灰阶:")
for i in range(10):
    print(i, "->", sum_probability[i])

# 将原图像中的灰阶映射到均衡后的灰阶
for i, line in enumerate(img):
    for j, pixel in enumerate(line):
        img[i][j] = sum_probability[img[i][j]]

print("均衡化后图像:")
for line in img:
    print(line)

直方图均衡化-Python实现

标签:email   for   value   utf-8   pix   outlook   lin   img   计算过程   

原文地址:https://www.cnblogs.com/focksor/p/HistogramEqualization_python.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!