【图像增强】基于蝙蝠算法实现直方图增强附matlab代码
1 简介
图像增强是数字图像的预处理,对图像整体或局部特征能有效地改善.我们讨论了基于蝙蝠算法优化直方图增强的参数;同时,以一个灰度图像为例,用MATLAB语言实现了蝙蝠算法优化直方图均衡化和规定化增强处理。
2 部分代码
%In this code, the two controlling parameters are lamda and gamma,%increasing lamda increases contrast whereas increasing gamma,%slowly(usually between 1000-100000) retains the overbrightness of white %pixels.%in original paper, the scholar hasn't used lamda in cdf function,%hence thereby, restricting himself to increase lamda%line 53 and 54 contains lamda and gamma respectively% clcclear allfor p=1:255 for q=1:256 if p==q D(p,q) = -1; elseif q==p+1 D(p,q) = 1; else D(p,q) = 0; end end end clci = imread('ImgOrig.png');r = i(:,:,1);g = i(:,:,2);b = i(:,:,3);figure,imshow(i)title('Original Image')%for n=1:256 % histogram(1,n) = 0;%for l = 1:size_of_image(1) % for b = 1:size_of_image(2) % if grays(l,b)==n % histogram(1,n) = histogram(1,n)+1; % end %end%end%end%x = 1:1:256;%figure, plot(x,histogram)[freqr xr] = imhist(r);[freqg xg] = imhist(g);[freqb xb] = imhist(b);size_of_image = size(r);number_of_pixels = size_of_image(1)*size_of_image(2);lamda = 5;%variable to determine the amount of contrastgamma = 50000 ;% smoothing_factor = inv(((1+lamda).*eye(256) + gamma.*transpose(D)*D));% for n = 0:1:255 % nfreqr(n+1,1) = (freqr(n+1,1) + lamda*n);% nfreqg(n+1,1) = (freqg(n+1,1) + lamda*n);% nfreqb(n+1,1) = (freqb(n+1,1) + lamda*n);% end% % freqr = smoothing_factor*nfreqr;% freqg = smoothing_factor*nfreqg;% freqb = smoothing_factor*nfreqb;hir = freqr; %for R[freqr,fminr] = bat_algorithm_image(20,1000, 0.5 ,0.5,hir,lamda,gamma,D,xr);freqr = transpose(freqr);hig = freqg; %for G[freqg,fming] = bat_algorithm_image(20 ,1000, 0.5 ,0.5,hig,lamda,gamma,D,xg);freqg = transpose(freqg);hib = freqb; %for B[freqb,fminb] = bat_algorithm_image(20 ,1000, 0.5 ,0.5,hib,lamda,gamma,D,xb);freqb = transpose(freqb);for n = 0:1:255 %probablity Density Function p(n+1,1) = freqr(n+1,1)/number_of_pixels;endcr = zeros(256,1); %cumulative density function cg = zeros(256,1); cb = zeros(256,1); cr(1,1) = freqr(1,1);cg(1,1) = freqg(1,1); cb(1,1) = freqb(1,1);for n = 1:1:255 cr(n+1,1) = cr(n,1) + freqr(n+1,1); cg(n+1,1) = cg(n,1) + freqg(n+1,1); cb(n+1,1) = cb(n,1) + freqb(n+1,1);endfor n = 0:1:255 cr(n+1,1) = cr(n+1,1)/number_of_pixels; cg(n+1,1) = cg(n+1,1)/number_of_pixels; cb(n+1,1) = cb(n+1,1)/number_of_pixels;endcdfr = (lamda+1)*(255.*cr + 0.5);cdfg = (lamda+1)*(255.*cg + 0.5);cdfb = (lamda+1)*(255.*cb + 0.5);cdfr = round(cdfr);cdfg = round(cdfg);cdfb = round(cdfb);%main Imagemain_image = uint8(zeros(size_of_image(1),size_of_image(2),3));c = 1; for l = 1:size_of_image(1) for w = 1:size_of_image(2) main_image(l,w,c) = cdfr(r(l,w)+1,1); main_image(l,w,c+1) = cdfg(g(l,w)+1,1); main_image(l,w,c+2) = cdfb(b(l,w)+1,1); end endfigure, imshow(main_image),title('Histogram Modified')figure, hist(main_image(:,:,1),xr);hold onhist(main_image(:,:,2),xg);hist(main_image(:,:,3),xb);entropy_of_original_image = entropy(i)entropy_of_image = entropy(main_image)mean_Optimized = mean2(main_image);var_optimzed = std2(main_image);D = abs(uint8(main_image) - uint8(i)).^2;mse = sum(D(:))/numel(main_image);psnr = 10*log10(255*255/mse);%mae = meanAbsoluteError(main_image,i)%E = eme(main_image,size_of_image(1),5)
3 仿真结果


4 参考文献
[1]汪志云, 黄梦为, 胡钋,等. 基于直方图的图像增强及其MATLAB实现[J]. 计算机工程与科学, 2006(2):54-56.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。


