Code Monkey home page Code Monkey logo

histogram_equqlization's Introduction

Histogram equalization

直方图均衡化的python实现(结果展示)

应用背景

  • 许多图片对比度不够,导致视觉效果不佳(不够清晰、难以分辨不同部分)。
  • 人类视觉系统都难以分辨的图片,再要求计算机去识别过于苛刻

test1

test2

之所以会出现“看不清,难以分辨……”这些问题,和图片的很多属性有关。
比如说分辨率、锐度、对比度等等。而对比度对图像视觉效果影响极为关键。
高对比度的图像会十分醒目,色彩鲜艳;低对比度的图像则会十分模糊,显得灰蒙蒙。


基础概念

在了解如何使用直方图均衡化的方法提高图片清晰度之前,需要了解一些基础概念

1. 对比度(Contrast)

对比度是一个反应图像亮度差异的值,对于人的视觉系统来说,如果视网膜接受的光的亮度之间差异较大,人会感到色彩绚丽,图像清晰。 通常计算公式为

2. 直方图(Histogram)

直方图是一个统计概念,用于统计一个图像中各个强度的像素点出现的个数。从而可以得到一个像素点的强度的概率分布。 图像 x 中单个像素点出现灰度 I 的概率为:

这其实是将0~255的灰度归一化到[0,1]。
test1 test2
(左边为原始图片,右边为它的直方图)

3. 均衡化(Equalization)

直方图的均衡化目的是使直方图尽可能平坦,从而避免整幅图像的强度集中在某一个很小的区域之内而导致分辨不清。
为了使灰度尽可能平坦分布,需要构造一个映射c(I),该映射可以考虑到整幅图像的强度变化,从而使灰度分布尽可能广泛。
对于全局均衡化,可以采用累计分布函数(cumulative distribution function)作为该映射,因为累计分布函数会综合像素灰度值 I 之前所有灰度值出现的情况。
累计分布函数的计算如下:

得到的分布函数图:
test2

4. 局部补偿(Partial compensation)

局部补偿可以改善得到的直方图,使其在保留较多原始灰度图像分布的同时达到更有吸引力的平衡。譬如如下简单的线性混合:


代码展示

以下类中定义了所有方法,并且有详细操作注释,详见代码文件

class Histogram_equalization(object):
    def __init__(self,ImgFile):
        self.OriginalImg = cv2.imread(ImgFile)
        self.Histogram = np.zeros((256,3),dtype=np.int)
        self.NewHistogram = np.zeros((256,3),dtype=np.int)

实验结果

test2

test2

test2

test2

test2

test2

test2

test2

test2

test2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.