图像直方图均衡化

**

1.灰度图像直方图均衡化

**

1)读入图像

​ 读入的图像矩阵命名为A

2)统计图像像素灰度

​ 设置一个可以保存256个灰度级别的一维数组,初始化为0

Sumpixel=zeros(1,256)

​ 根据图像的大小,统计每个灰度级别各有几个像素点

 [heigh,width]=size(A);
 for i=1:heigh
        for j=1:width    			      
        Sumpixel(A(i,j)+1)=Sumpixel(A(i,j)+1)+1;%对应灰度加一
        end
    end
3)统计灰度分布密度

​ 设置一个一维数组,保存每个灰度的密度

  Ppixel=zeros(1,256);
    for i=1:256
        Ppixel(i)=Sumpixel(i)/(heigh*width*1.0);
    end
4)求积分,得到概率累计
Cpixel=zeros(1,256);
    for i=1:256
         if i==1
             Cpixel(i)=Ppixel(i);
         else
             Cpixel(i)=Cpixel(i-1)+Ppixel(i);
         end
    end
5)映射到【0,255】
 Cpixel=uint8(255.*Cpixel+0.5);
6)完成每一个像素点的映射
for i=1:heigh
        for j=1:width
            B(i,j)=Cpixel(A(i,j)+1);
        end
    end   
    output2=B;
  
7)显示结果

在这里插入图片描述

**

2.彩色图像直方图均衡化(一)

**

1)读取彩色图像
RGB=input_image;
2)均衡化

​ 将RGB图像的 R G B三个通道分离,对每个通道进行均衡化

 [R,C,K]=size(RGB);
    for k=1:K
       RGB(:,:,k)=hist_equal(RGB(:,:,k));
    end
3) 显示结果

在这里插入图片描述

处理效果明显

3.RGB图像直方图均衡化(二)

1)读取图像
RGB=input_image;
2)将RGB图像灰度图像
 RGB_GRAY=rgb2gray(RGB);
3)将灰度执行均衡化处理
 RGB_GRAY=hist_equal(RGB_GRAY);
4)用调色板将灰度图染成彩色

首先函数将灰度图像扩展到[三通道],然后将两张图像转为yCbCr空间。最后,通过一个二重循环来逐像素生成结果图。

color_Sum=gray2rgb(RGB_GRAY,'tiaose.JPG');
5)显示效果:

在这里插入图片描述

原图像

在这里插入图片描述
变为灰度图像,执行均衡化之后,再染色

在这里插入图片描述

看直方图,处理有效果,但是看图片,发现与上面方法相比,效果不太好。

贴上代码:
直方图均衡化代码

function [output] = Histogram_equalization(input_image)
%first test the image is a RGB or gray image
if numel(size(input_image)) == 3
    %this is a RGB image
    %here is just one method, if you have other ways to do the
    %equalization, you can change the following code
    %r=input_image(:,:,1);
    %v=input_image(:,:,2);
    %b=input_image(:,:,3);
    %r1 = hist_equal(r);
    %v1 = hist_equal(v);
    %b1 = hist_equal(b);
    %output = cat(3,r1,v1,b1)
    
    
    %方法一:多通道处理
    RGB=input_image;
    subplot(3,3,1),imshow(RGB);title('原图像');
    subplot(3,3,4),imhist( RGB(:,:,1));title('原图像R通道直方图');
    subplot(3,3,5),imhist( RGB(:,:,2));title('原图像G通道直方图');
    subplot(3,3,6),imhist( RGB(:,:,3));title('原图像B通道直方图');
    [R,C,K]=size(RGB);
    for k=1:K
       RGB(:,:,k)=hist_equal(RGB(:,:,k));
    end
    subplot(3,3,2),imshow(RGB);title('新图像');
    subplot(3,3,7),imhist( RGB(:,:,1));title('均衡化之后图像R通道直方图');
    subplot(3,3,8),imhist( RGB(:,:,1));title('均衡化之后图像G通道直方图');
    subplot(3,3,9),imhist( RGB(:,:,1));title('均衡化之后图像B通道直方图');
    output=RGB;   
    
    
    
    %方法二:转化为灰度图像进行直方图均衡化
    %检测时请解开注释
     %RGB=input_image;
     %subplot(3,3,1),imshow(RGB);title('原图像');
     %subplot(3,3,4),imhist( RGB);title('原图像直方图');
     %RGB_GRAY=rgb2gray(RGB);
     %subplot(3,3,2),imshow(RGB_GRAY);title('灰度图');
     %subplot(3,3,5),imhist( RGB);title('灰度图直方图');
     %RGB_GRAY=hist_equal(RGB_GRAY);
     %subplot(3,3,3),imshow(RGB_GRAY);title('灰度图均衡化后');
     %subplot(3,3,6),imhist(RGB_GRAY);title('灰度图均衡化后直方图');
     %color_Sum=gray2rgb(RGB_GRAY,'tiaose.JPG');
     %subplot(3,3,7),imshow(color_Sum);title('合成后图像');
     %subplot(3,3,8),imhist(color_Sum);title('合成后直方图');
     
     %output=color_Sum;   
     
    
    
else
    %this is a gray image
    I=input_image;
    subplot(2,2,1),imshow(I);title('原图像');
    subplot(2,2,2),imhist(I);title('原图像直方图');
    I= hist_equal(I);
    subplot(2,2,3),imshow(I);title('新图像');
    subplot(2,2,4),imhist(I);title('新图像直方图');
    [output]=I;
    
end

    function [output2] = hist_equal(input_channel)
    %you should complete this sub-function
    A=input_channel;
    
    [heigh,width]=size(A);
    %统计像素灰度
    Sumpixel=zeros(1,256);%统计每个灰度的数目,一共256个灰度级别(用数组保存)
    for i=1:heigh
        for j=1:width
            Sumpixel(A(i,j)+1)=Sumpixel(A(i,j)+1)+1;%对应灰度加一
        end
    end
    
    %统计灰度分布密度
    Ppixel=zeros(1,256);
    for i=1:256
        Ppixel(i)=Sumpixel(i)/(heigh*width*1.0);
    end
    
    %求积分
    Cpixel=zeros(1,256);
    for i=1:256
         if i==1
             Cpixel(i)=Ppixel(i);
         else
             Cpixel(i)=Cpixel(i-1)+Ppixel(i);
         end
    end
    
    %累计分布
    Cpixel=uint8(255.*Cpixel+0.5);
    
    %均衡化
    for i=1:heigh
        for j=1:width
            B(i,j)=Cpixel(A(i,j)+1);
        end
    end   
    output2=B;
    end
end





方法(二)的灰度图转变为彩色

function R=gray2rgb(img1,img2)
 clc;
 warning off;
 imt=img1;
 ims=imread(img2);
 [sx sy sz]=size(imt);
 [tx ty tz]=size(ims);
 if sz~=1
     imt=rgb2gray(imt);
 end
 if tz~=3
     disp ('img2 must be a color image (not indexed)');
 else
     imt(:,:,2)=imt(:,:,1);
     imt(:,:,3)=imt(:,:,1);

 % Converting to ycbcr color space
     nspace1=rgb2ycbcr(ims);
     nspace2= rgb2ycbcr(imt);

    ms=double(nspace1(:,:,1));
     mt=double(nspace2(:,:,1));
     m1=max(max(ms));
     m2=min(min(ms));
     m3=max(max(mt));
     m4=min(min(mt));
     d1=m1-m2;
     d2=m3-m4;
 % Normalization
     dx1=ms;
     dx2=mt;
     dx1=(dx1*255)/(255-d1);
     dx2=(dx2*255)/(255-d2);
     [mx,my,mz]=size(dx2);
 %Luminance Comparison
     disp('Please wait..................');
     for i=1:mx
         for j=1:my
              iy=dx2(i,j);
              tmp=abs(dx1-iy);
              ck=min(min(tmp));
              [r,c] = find(tmp==ck);
              ck=isempty(r);
              if (ck~=1)            
                  nimage(i,j,2)=nspace1(r(1),c(1),2);
                  nimage(i,j,3)=nspace1(r(1),c(1),3);
                  nimage(i,j,1)=nspace2(i,j,1);           
             end
          end
      end
     rslt=ycbcr2rgb(nimage)
     R=uint8(rslt);
     toc
 end  

测试代码

%test histeq
I = imread('color.jpg');
[J] = Histogram_equalization(I);

figure, imshow(I)
figure, imshow(J)
%figure,imshow(color_Sum);


test调用其他代码运行,灰度图直方图均衡部分已经被注释,解开注释可以运行

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值