报告论文:直方图均衡化和规定化 matlab实现

直方图均衡化代码:

第一种:
%对一幅图像进行直方图均衡化
A=imread('d:\Gem.bmp');
[m,n]=size(A);
B=zeros(size(A));
l=m*n;
r=zeros(1,256);
y=zeros(1,256);
A=double(A);
for i=1:m
for j=1:n
r(A(i,j)+1)=r(A(i,j)+1)+1;
end
end
r=r./l;%得到原图像各灰度级所占的比例
for k=1:256
for j=1:k
y(k)=y(k)+r(j);
end
end %直方图累加
for k=1:256
y(k)=floor(255*y(k)+0.5); %确定映射关系
end
for i=1:m
for j=1:n
B(i,j)=y(A(i,j)+1);
end
end
A=uint8(A);
B=uint8(B);
subplot(2,2,1)
imshow(A);
title('原图像')
subplot(2,2,2)
imhist(A);
title('原图像的直方图')
subplot(2,2,3)
imshow(B);
title('经过均衡处理的图像')
subplot(2,2,4)
imhist(B);
title('经过均衡处理后该图像的直方图')


第二种:
>> A=imread('d:\gem.bmp');                  %读入JPG彩色图像文件
>> I=rgb2gray(A);                          %灰度化后的数据存入数组
>> subplot(2,2,1),imshow(I);title('原始图像 Gem');

>> %二,绘制直方图
>> [m,n]=size(I);                             %测量图像尺寸参数
>> GP=zeros(1,256);                           %预创建存放灰度出现概率的向量
>> for k=0:255
GP(k+1)=length(find(I==k))/(m*n);     %计算每级灰度出现的概率,将其存入GP中相应位置
end
>> subplot(2,2,2),bar(0:255,GP,'g');title('原始图像的直方图')
>> xlabel('灰度值')
>> ylabel('出现概率')                           %绘制直方图

>> %三,直方图均衡化
>> S1=zeros(1,256);
>> for i=1:256
for j=1:i
S1(i)=GP(j)+S1(i);                              %计算Sk 累计直方图概率
end
end

>> S2=round(S1*256);                         %将Sk归到相近级的灰度
>> for i=1:256
GPeq(i)=sum(GP(find(S2==i)));          %计算现有每个灰度级出现的概率
end


>> subplot(2,2,3),bar(0:255,GPeq,'b');title('均衡化后的直方图')
>> xlabel('灰度值')
>> ylabel('出现概率')                         %显示均衡化后的直方图

>> %四,图像均衡化
>> PA=I;
>> for i=0:255
PA(find(I==i))=S2(i+1);                %将各个像素归一化后的灰度值赋给这个像素
end
>> subplot(2,2,4),imshow(PA);title('均衡化后图像')   %显示均衡化后的图像


>> figure(2),plot(0:255,S2,'r');legend('灰度变化曲线')  %显示灰度变化曲线
>> xlabel('原图像灰度级')
>> ylabel('均衡化后灰度级')
>>


第三种:
%%%%%%%%%%%%灰度图象的直方图数据
clc
clear
fn='rice.tif';
I=imread (fn);
J=I;
%计算灰度图象的直方图数据
L=256; %灰度级
Ps = zeros(L,1); %统计直方图结果数据
nk=zeros(L,1);
[row,col]=size(I);
n=row*col; %总像素个数
for i = 1:row
for j = 1:col
num = double(I(i,j))+1; %获取像素点灰度级
nk(num) = nk(num)+1; %统计nk
end
end
%计算直方图概率估计
for i=1:L
Ps(i)=nk(i)/n;
end
figure;
subplot(3,1,1);imshow(J),title('原图');
subplot(3,1,2),plot(nk),title('直方图(nk)');
subplot(3,1,3),plot(Ps),title('直方图(Ps)');

%figure(4),imhist(hist);
%p=imhist(hist); %生成直方图函数,返回灰度图象直方图数据
%figure(5),plot(p);

 

直方图规定化代码:

第一种:matlab实现的
   请看work 文件夹
   后面的两种都是VC++实现的,

  由于字数有限,我会把代码和报告文件上传到下面的地址(由于服务器暂时不能上传,请等待)

   报告和代码完整版下载:

     报告下载:  http://ctfysj.gbaopan.com/files/d4773fe340724aed93995c28400e018b.gbp

      代码下载: http://ctfysj.gbaopan.com/files/908f9f52959e4a538f84c75ca4c6948f.gbp

     或者下载页面: http://tel4.800disk.com/ContentPane.aspx?down=ok&filename 

第二种:
       double scrMin[256][256];
        for(y=0 ;y<256 ;y++)
        for(x=0;x<256;x++)
            scrMin[x][y]=fabs(GP[y]-GPeq[x]);
/*SML映射
            for(y=0 ;y<256 ;y++)
            {
            int minX=0;
            double minValue=scrMin[0][y];
            for(x=1;x<256;x++)
            {
            if(minValue>scrMin[x][y])
               {
                minValue=scrMin[x][y];
                minX=x;
                }    
             }
            HistogramSpeciMapping[y]=minX;//建立映射关系
            }

/*GML映射
           short lastStartY=0,lastEndY=0,startY=0,endY=0;
           for(x=0;x<256;x++)
           {
             double minValue=scrMin[x][0];
             for (y=0 ;y<256 ; y++ )
             {
             if (minValue>scrMin[x][y])
               {
                  endY=y;
                  minValue=scrMin[x][y];
                }
             }
            if(startY!=lastStartY || endY! =lastEndY)
            {
            for(i=startY;i<=endY;i++)
                  HistogramSpeciMapping[i]=x;//建立映射关系
            lastStartY=startY;
            lastEndY=endY;
            startY=lastEndY+1;
             }
           }

第三种:
void main()
{
 BMPFILE bmpfile;                                                                
 
 bmpfile.LoadBMPFILE ("实验图象1.bmp");                                                                   
 if(!bmpfile.bIsOk)
 {
  printf("Error loading image.\n");
  return;
 }
 // do other processing with the imagedata
 int H[256]; //原始图象直方图                                          
 memset(H,0,sizeof(H));
 int S[256]; //变换映射-均衡直方图S
 double psnr; //峰值信噪比

int i;                                                       
 int f;                                                       
 for(i=0;i<bmpfile.imageh*bmpfile.imagew;i++)                 
  H[*(bmpfile.imagedata+i)]++;                        
 S[0]=H[0];                                                   
 for(i=1;i<256;i++)                                         
  S[i]=S[i-1]+H[i];                                     
 for(i=0;i<256;i++)                                              
  S[i]=S[i]*255/(bmpfile.imageh*bmpfile.imagew);       
 for(i=0;i<bmpfile.imageh*bmpfile.imagew;i++)                     
  *(bmpfile.imagedata+i)=S[*(bmpfile.imagedata+i)];      
 

       //  r(A(i,j)+1)=r(A(i,j)+1)+1;  B(i,j)=y(A(i,j)+1);    
       
       //原图象的直方图H                                            
 printf("H: ");                                                 
 for(i=1;i<256;i++)                                           
                                                                        
  printf("%d ",H[i]);                                   
 printf("\n");                                                     
 //原图象(直方图H)的均衡直方图
 printf("S: ");
 for(i=1;i<256;i++)
  printf("%d ",S[i]);
 printf("\n");

 //均衡图象
 bmpfile.SaveBMPFILE ("实验图象1-均衡图象.bmp");                                      
 //PSNR值
 psnr=0;                                                                        
 for(i=0;i<255;i++)                                                                 
  psnr+=(S[i]-i)*(S[i]-i)*H[i];      
                                     
 psnr=10*log10(double(bmpfile.imageh)*bmpfile.imagew*255*255/psnr);

 printf("PSNR: %f\n",psnr);

         //均衡图象 =psnr
 //目标直方图G的均衡直方图                                     
 int G[256]; //规定直方图
 int GS[256]; //规定直方图的均衡直方图
 for(i=0;i<=127;i++)
  G[i]=i;
 for(i=128;i<=255;i++)
  G[i]=(255-i);
 GS[0]=G[0];
 for(i=1;i<256;i++)
  GS[i]=GS[i-1]+G[i];
 f=GS[255]/255+1;
 for(i=0;i<256;i++)
  GS[i]/=f;

 printf("GS: ");                   
 for(i=1;i<256;i++)                    
  printf("%d ",GS[i]);        
 printf("\n");
 //最终增强图象
 int i2;
 int minNumber;
 int SG[256]; //从原图象的均衡图象到最终增强图象的映射

 memset(SG,0,sizeof(SG));
 for(i=0;i<256;i++)
 {
  minNumber=255;
  for(i2=0;i2<256;i2++)
   if(abs(S[i]-GS[i2])<minNumber)
   {
    minNumber=abs(S[i]-GS[i2]);
    SG[S[i]]=i2;
   }
 }

 for(i=0;i<bmpfile.imageh*bmpfile.imagew;i++)
  *(bmpfile.imagedata+i)=SG[*(bmpfile.imagedata+i)];
 bmpfile.SaveBMPFILE ("实验图象1-最终增强图象.bmp");

 //最终增强图象的直方图GR R=real
 int GR[256];
 memset(GR,0,sizeof(GR));   //  SG清零   将指定内存地址设为某值'
 for(i=1;i<256;i++)
  GR[SG[S[i]]]+=H[i];        


 printf("GR: ");                                
 for(i=1;i<256;i++)
  printf("%d ",GR[i]);                    
 
          printf("\n");
 //最终增强图象的PSNR值

 psnr=0;
 for(i=0;i<255;i++)
  psnr+=(SG[S[i]]-i)*(SG[S[i]]-i)*GR[i];
 psnr=10*log10(double(bmpfile.imageh)*bmpfile.imagew*255*255/psnr);

 printf("PSNR of the destination image: %f\n",psnr);

}


 

 

posted @ 2013-06-13 14:24  信息无障碍  阅读(912)  评论(0编辑  收藏  举报