图像处理------直方图均衡化

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>基本思想</strong>:</span></p><p style="color: rgb(51, 51, 51); font-fam

基本思想:

直方图图均衡化是图像处理中的常用图像增强手段,直方图均衡化的主要优点是

可以降低图像噪声,提升图像的局部显示。对于常见的RGB图像,直方图均衡化

可以分别在三个颜色通道上处理,基本的直方图均衡化的公式为:

其中nj表示灰度级为Rk的像素的个数,L为图像中灰度总数,对于RGB来说L的

取值范围为[0~255]总灰度级为256个。而R表示输入图像的直方图数据。根据输

出的灰度值Sk计算出输出像素的每个像素值,完成直方图均衡化之后的像素处理

程序效果:


源代码:

[java]  view plain copy
  1. package com.gloomyfish.filter.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4.   
  5. public class HistogramEFilter extends AbstractBufferedImageOp{  
  6.   
  7.     @Override  
  8.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  9.         int width = src.getWidth();  
  10.         int height = src.getHeight();  
  11.   
  12.         if ( dest == null )  
  13.             dest = createCompatibleDestImage( src, null );  
  14.   
  15.         int[] inPixels = new int[width*height];  
  16.         int[] outPixels = new int[width*height];  
  17.         getRGB( src, 0, 0, width, height, inPixels );  
  18.         int[][] rgbhis = new int[3][256]; // RGB  
  19.         int[][] newrgbhis = new int[3][256]; // after HE  
  20.         for(int i=0; i<3; i++) {  
  21.             for(int j=0; j<256; j++) {  
  22.                 rgbhis[i][j] = 0;  
  23.                 newrgbhis[i][j] = 0;  
  24.             }  
  25.         }  
  26.         int index = 0;  
  27.         int totalPixelNumber = height * width;  
  28.         for(int row=0; row<height; row++) {  
  29.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  30.             for(int col=0; col<width; col++) {  
  31.                 index = row * width + col;  
  32.                 ta = (inPixels[index] >> 24) & 0xff;  
  33.                 tr = (inPixels[index] >> 16) & 0xff;  
  34.                 tg = (inPixels[index] >> 8) & 0xff;  
  35.                 tb = inPixels[index] & 0xff;  
  36.   
  37.                 // generate original source image RGB histogram  
  38.                 rgbhis[0][tr]++;  
  39.                 rgbhis[1][tg]++;  
  40.                 rgbhis[2][tb]++;  
  41.             }  
  42.         }  
  43.           
  44.         // generate original source image RGB histogram  
  45.         generateHEData(newrgbhis, rgbhis, totalPixelNumber, 256);  
  46.         for(int row=0; row<height; row++) {  
  47.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  48.             for(int col=0; col<width; col++) {  
  49.                 index = row * width + col;  
  50.                 ta = (inPixels[index] >> 24) & 0xff;  
  51.                 tr = (inPixels[index] >> 16) & 0xff;  
  52.                 tg = (inPixels[index] >> 8) & 0xff;  
  53.                 tb = inPixels[index] & 0xff;  
  54.   
  55.                 // get output pixel now...  
  56.                 tr = newrgbhis[0][tr];  
  57.                 tg = newrgbhis[1][tg];  
  58.                 tb = newrgbhis[2][tb];  
  59.                   
  60.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  61.             }  
  62.         }  
  63.         setRGB( dest, 0, 0, width, height, outPixels );  
  64.         return dest;  
  65.     }  
  66.     /** 
  67.      *  
  68.      * @param newrgbhis 
  69.      * @param rgbhis 
  70.      * @param totalPixelNumber 
  71.      * @param grayLevel [0 ~ 255] 
  72.      */  
  73.     private void generateHEData(int[][] newrgbhis, int[][] rgbhis, int totalPixelNumber, int grayLevel) {  
  74.         for(int i=0; i<grayLevel; i++) {  
  75.             newrgbhis[0][i] = getNewintensityRate(rgbhis[0], totalPixelNumber, i);  
  76.             newrgbhis[1][i] = getNewintensityRate(rgbhis[1], totalPixelNumber, i);  
  77.             newrgbhis[2][i] = getNewintensityRate(rgbhis[2], totalPixelNumber, i);  
  78.         }  
  79.     }  
  80.       
  81.     private int getNewintensityRate(int[] grayHis, double totalPixelNumber, int index) {  
  82.         double sum = 0;  
  83.         for(int i=0; i<=index; i++) {  
  84.             sum += ((double)grayHis[i])/totalPixelNumber;  
  85.         }  
  86.         return (int)(sum * 255.0);  
  87.     }  
  88.   
  89. }  
相关文章
|
资源调度 计算机视觉 算法
图像处理------高斯一阶及二阶导数计算
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">图像的一阶与二阶导数计算在图像特征提取与边缘提取中十分重要。一阶与二阶导数的</span></p> <p style="color: rgb(51, 5
5655 0
|
资源调度 计算机视觉 Java
图像处理------图像加噪
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">图像噪声源于现实世界中数字信号总会受到各种各样的干扰,最终接受的图像和源于的数字信号之间总</p> <p style="color: rgb(51, 51, 51); font-family: Arial; fon
1998 0
|
Perl 流计算 内存技术
AXI总线简介
<p style="margin-top: 14px; margin-bottom: 14px; padding-top: 0px; padding-bottom: 0px; color: rgb(46, 46, 46); font-family: 微软雅黑, 'Microsoft Yahei'; font-size: 14px; line-height: 28px; border: 0px;
5583 0
|
XML Android开发 数据格式
|
资源调度 算法 Java
图像处理------Canny边缘检测
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>一:历史</strong></span></p> <p style="color: rgb(51, 51, 51); font-fami
2117 0
|
算法 计算机视觉 Java
图像处理------相似图片识别(直方图应用篇)
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong>算法概述:</strong></p> <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-heig
2942 0
|
计算机视觉
图像处理------调整亮度与饱和度
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;"><strong>什么是亮度:</strong></span></p> <p style="color: rgb(51, 51, 51); font-fa
3081 0
|
计算机视觉 Python Java
图像处理------光源退化效果
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><strong><span style="font-size: 18px;">基本思想:</span></strong></p> <p style="color: rgb(51, 51, 51); font-fam
1728 0