彩色直方图均衡化(源码实现)

原理:计算每个通道像素级别(0-255)的累加概率,然后用累加概率乘以255代替原来的像素

import cv2
import numpy as np
img = cv2.imread('D:/pythonob/imageinpaint/img/flower.jpg',1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
cv2.imshow('src',img)
count_b = np.zeros(256,np.float)
count_g = np.zeros(256,np.float)
count_r = np.zeros(256,np.float)
for i in range(0,height):
for j in range(0,width):
(b,g,r) = img[i,j]
index_b = int(b)
index_g = int(g)
index_r = int(r)
count_b[index_b] = count_b[index_b]+1
count_g[index_g] = count_g[index_g] + 1
count_r[index_r] = count_r[index_r] + 1
for i in range(0,256):
count_b[i] = count_b[i] / (height * width)
count_g[i] = count_g[i] / (height * width)
count_r[i] = count_r[i] / (height * width)
#计算累计概率
sum_b = float(0)
sum_g = float(0)
sum_r = float(0)
for i in range(0,256):
sum_b = sum_b + count_b[i]
sum_g = sum_g + count_g[i]
sum_r = sum_r + count_r[i]
count_b[i] = sum_b
count_g[i] = sum_g
count_r[i] = sum_r
#计算映射表
map_b = np.zeros(256,np.uint16)
map_g = np.zeros(256,np.uint16)
map_r = np.zeros(256,np.uint16)
for i in range(0,256):
map_b[i] = count_b[i] * 255
map_g[i] = count_g[i] * 255
map_r[i] = count_r[i] * 255
for i in range(0,height):
for j in range(0,width):
(b,g,r) = img[i,j]
b = map_b[b]
g = map_b[g]
r = map_b[r]
img[i,j] = (b,g,r)
cv2.imshow('aftergImg',img)
cv2.waitKey(0)

效果图:

 

转载于:https://www.cnblogs.com/cxxBoo/p/11480940.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值