直方图均衡化 Histogram Equalization

   直方图原理及代码实现(MATLAB、java)


1.直方图均衡化步骤:(直接看一个简单的例子)有一个64*64像素的图片,分成8个灰度级,分布图如下:


rk是归一化完的(分成8个灰度级),0.19 = 790/64*64;

根据这个概率分布求概率的累加:



近似灰度值:



只剩下5个灰度等级:



把整个步骤和结果整合起来为:




2.程序实现部分:

(一)MATLAB:

%直方图均衡化  
I = imread('C:\Users\lenovo\Desktop\exam\matlab\1.jpg');  
[height,width] = size(I);  
figure  
subplot(221)  
imshow(I)%显示原始图像   
%进行像素灰度统计;  
NumPixel = zeros(1,256);%统计各灰度数目,共256个灰度级  
for i = 1:height  
    for j = 1: width  
        NumPixel(I(i,j) + 1) = NumPixel(I(i,j) + 1) + 1;%对应灰度值像素点数量增加一  
    end  
end  
%计算灰度分布密度  
ProbPixel = zeros(1,256);  
for i = 1:256  
    ProbPixel(i) = NumPixel(i) / (height * width * 1.0);  
end 
subplot(2,2,2);%显示原图像直方图  
bar(0:255,ProbPixel);  
title('原图像直方图');  
xlabel('灰度值');  
ylabel('出现概率');  
CumuPixel=zeros(1,256); %计算新的各灰度出现的概率  
S1=zeros(1,256);  
S2=zeros(1,256);  
tmp=0;  
for i=1:256  
    tmp=tmp+ProbPixel(i);  
    S1(i)=tmp;  
    S2(i)=round(S1(i)*256);  
end  
for i=1:256  
    CumuPixel(i)=sum(ProbPixel(find(S2==i)));  
end
newGrayPic=I; %填充各像素点新的灰度值  
for i=1:256  
    newGrayPic(find(I==(i-1)))=S2(i);  
end
subplot(223)  
imshow(newGrayPic)%显示均匀化后原始图像  
subplot(224) 
subplot(2,2,4);%显示均衡化后的直方图  
bar(0:255,CumuPixel);  
title('均衡化后的直方图');  
xlabel('灰度值');  
ylabel('出现概率'); 


(二)java:

/**
 * Created by lenovo on 2016/4/1.
 */
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class TestImage1 {


        public void binaryImage() throws IOException{
            File file = new File(System.getProperty("user.dir")+"/src/1.jpg");
            BufferedImage image = ImageIO.read(file);

            int width = image.getWidth();
            int height = image.getHeight();

            BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_BINARY
            for(int i= 0 ; i < width ; i++){
                for(int j = 0 ; j < height; j++){
                    int rgb = image.getRGB(i, j);
                    grayImage.setRGB(i, j, rgb);
                }
            }

            File newFile = new File(System.getProperty("user.dir")+"/src/2.jpg");
            ImageIO.write(grayImage, "jpg", newFile);
        }

        public void grayImage() throws IOException{
            File file = new File(System.getProperty("user.dir")+"/src/1.jpg");
            BufferedImage image = ImageIO.read(file);

            int width = image.getWidth();
            int height = image.getHeight();

            BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);//重点,技巧在这个参数BufferedImage.TYPE_BYTE_GRAY
            for(int i= 0 ; i < width ; i++){
                for(int j = 0 ; j < height; j++){
                    int rgb = image.getRGB(i, j);
                    grayImage.setRGB(i, j, rgb);
                }
            }

            File newFile = new File(System.getProperty("user.dir")+"/src/3.jpg");
            ImageIO.write(grayImage, "jpg", newFile);
        }

        public static void main(String[] args) throws IOException {
            TestImage1 demo = new TestImage1();
            demo.binaryImage();
            demo.grayImage();
        }

}
转载自:http://hello-wangfeng.iteye.com/blog/1717150

新手。。。不喜勿喷!





  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值