zoukankan      html  css  js  c++  java
  • 直方图正规化

    概念和原理

    直方图正规化是一种自动选取 a 和 b 的值的线性变换方法

    假设输入图像为 I,高为 H、 宽为 W, I (r, c) 代表 I 的第 r 行第 c 列的灰度值, 将I中出现的最小灰度级记为Imin, 最大灰度级记为Imax,即 I (r, c) ∈[ I min, Imax], 为使输出图像 O 的灰度级范围为[ Omin, Omax], I (r, c) 和 O( r, c) 做以下映射关系:
    (O(r,c) = frac{O_{max}-O_{min}}{I_{max}-I_{min}}(I(r, c) - I_{min}) + O_{min})
    其中,
    (a=frac{O_{max}-O_{min}}{I_{max}-I_{min}}), (b=O_{min}-frac{O_{max}-O_{min}}{I_{max}-I_{min}}*I_{min})

    代码

    1、若输入是 8 位图 ,一般设置 O_min = 0,O_max = 255
    2、若输入的是归一化的图像,一般设置 O_min = 0,O_max = 1

    # !/usr/bin/env python
    # -*-encoding: utf-8-*-
    # author:LiYanwei
    # version:0.1
    
    import numpy as np
    import cv2
    import matplotlib.pyplot as plt
    
    
    def histNormalized(InputImage,O_min = 0,O_max = 255):
        I_min = np.min(InputImage)
        I_max = np.max(InputImage)
        rows,cols = InputImage.shape
        #输出图像
        OutputImage = np.zeros(InputImage.shape,np.float32)
        #输出图像的映射
        cofficient = float(O_max - O_min)/float(I_max - I_min)
        for r in range(rows):
            for c in range(cols):
                OutputImage[r][c] = cofficient*( InputImage[r][c] - I_min) + O_min
        return OutputImage
    
    
    if __name__ =="__main__":
        image = cv2.imread('img3.jpg', cv2.IMREAD_GRAYSCALE)
        #显示原图
        cv2.imshow("image", image)
        #直方图正规化
        histNormResult = histNormalized(image)
        #数据类型转换,灰度级显示
        histNormResult = np.round(histNormResult)
        histNormResult = histNormResult.astype(np.uint8)
        #显示直方图正规化的图片
        cv2.imshow("histNormlized", histNormResult)
        cv2.imwrite("histNormResult.jpg", histNormResult)
    
  • 相关阅读:
    this is test,,this is only test!!!
    js设置鼠标悬停改变背景色
    免费数据恢复软件
    ORA-00600: 内部错误代码
    js控制只能输入数字和控制小数的位数
    Eclipse换背景色
    记JavaScript的入门学习(二)
    html+css基础篇
    记JavaScript的入门学习(一)
    前端之Photoshop切片
  • 原文地址:https://www.cnblogs.com/Py00/p/8927363.html
Copyright © 2011-2022 走看看