python批量直方图均衡化_【图像处理】直方图均衡化基于python实现

本文介绍如何使用Python和OpenCV库进行直方图均衡化的图像处理。通过详细步骤展示从读取图像到创建直方图,再到进行直方图均衡化和显示处理后图像的完整过程。
摘要由CSDN通过智能技术生成

读图像、显示图像采用opencv接口,算法主体用python编写

算法步骤在代码中详细体现。

代码如下:

#!/usr/bin/python3

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

'直方图均衡化函数,0::28 2019/06/20'

__author__ = 'mint'

import numpy as np

import cv2 as cv

#创建空列表(数组)

def createEmptyList(size):

newList = []

for eachNum in range(0, size):

newList.append(0)

return newList

#创建空图像矩阵

def createEmptyImage(rows, cols,type):

img = np.zeros((rows, cols), dtype=type)

return img

#直方图均衡化

def histequaLize(src, dst):

#step 1 校验参数#

assert(type(src)==np.ndarray)

assert(src.dtype == np.uint8)

assert (type(dst) == np.ndarray)

assert (dst.dtype == np.uint8)

# step 2 直方图统计#

hist = createEmptyList(256)

rows, cols = src.shape

for r in range(rows):

for c in range(cols):

hist[src[r,c]] += 1

# step 3 直方图归一化#

for each in range(256):

hist[each] /= rows*cols

# step 4 直方图累加#

for each in range(1,256):

hist[each] = hist[each-1] + hist[each]

# step 4 均衡#

for each in range(256):

hist[each] = (np.uint8)(255*hist[each] + 0.5)

for r in range(rows):

for c in range(cols):

dst[r,c] = hist[src[r,c]]

def histMain():

img = cv.imread('lena.jpg')

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

dst = createEmptyImage(img.shape[0], img.shape[1], np.uint8)

histequaLize(gray, dst)

cv.imshow('histEnhance.jpg', dst)

cv.waitKey(0)

if __name__ == '__main__':

histMain()

输出如下:

5a27cfb5a1715be0b83a27414ab8db85.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值