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

简介: <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, 00, 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, 00, 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. }  
相关文章
|
计算机视觉
【计算机视觉】图像增强----直方图均衡化
主要介绍图像增强中直方图均衡化的原理以及相关实操及分析。
649 0
【计算机视觉】图像增强----直方图均衡化
|
数据可视化 算法 计算机视觉
【计算机视觉】图像增强----图像的傅立叶变换
【计算机视觉】图像增强----图像的傅立叶变换
534 0
【计算机视觉】图像增强----图像的傅立叶变换
|
编解码 算法 计算机视觉
小波分析法图像处理----图像增强
小波分析法图像处理----图像增强
652 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
1592 0
|
资源调度 算法 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
1737 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; font-size: 14px; line-height: 26px;">
2115 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-height
1354 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; font-siz
1204 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
2599 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; font-size: 1
1588 0