图像增强基于matlab GSA灰度图像增强含Matlab源码 1172期
Posted 紫极神光
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像增强基于matlab GSA灰度图像增强含Matlab源码 1172期相关的知识,希望对你有一定的参考价值。
一、简介
基于matlab GSA灰度图像增强:引力搜索算法(GSA)是一种基于引力和质量相互作用定律的优化算法。该算法基于牛顿引力:“宇宙中的每个粒子都以与力乘积成正比的力吸引每个其他粒子。它们的质量与它们之间的距离的平方成反比”。
二、源代码
% Using the Regional Similarity Transformation Function and Dragonfly Algorithm.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%THIS IS A DEMO FOR NOVEL IMAGE ENHANCEMENT USING GRAV脻TAT脻ONAL SEARCH ALGORITHM
clc;
clear all;
close all;
%READ IMAGE
i=imread('test5.bmp');
i=imresize(i,1);
%--------------------------------------------------------------------------
%GLOBAL MEAN
frekans=zeros(256,1);
% It records by counting how many times each pixel value is.
for k=1:size(i,1)
for l=1:size(i,2)
value=i(k,l);
frekans(value+1)=frekans(value+1)+1;
end
end
deger=max(frekans);
for k=1:256
if deger==frekans(k)
D=k;
end
end
D=double(D/255);
image = im2double(i);
global_mean = D;
%--------------------------------------------------------------------------
% LOCAL MEAN
Bmean = mean_n(image);
%--------------------------------------------------------------------------
%SIMILARITY
[S]=similarity(i);
B=double(S);
%--------------------------
C = std_n(image,Bmean);
im_size = size(image);
%-------------------------------------------------------------------------
%OPTIMIZATION PARAMETERS
N=15; % Agent number;
Max_iteration=15;
%-------------------------------------------------------------------------
%CALL FUNCTION
[parameters Fbest BestChart MeanChart] = GSA_enhancement2(image,global_mean,B,C,im_size, N, Max_iteration);
enh = trans(i,image, B, C, global_mean, parameters(1),parameters(2),parameters(3),parameters(4) );
function [S]= similarity(x)
[xlen ylen] =size(x);
S=zeros(xlen, ylen);
for i=2:1:xlen-1
for j=2:1:ylen-1
%--------------------------------------------------------
%Getting the values in the mask
maskGray=zeros(1,9) ;
resim=zeros(1,9) ;
for b=1:9;
[m]=ikomsu(b,i);
[n]=jkomsu(b,j);
[res]=resimdemi(m,n,xlen,ylen);
resim(b)=res;
if(resim(b)==1)
gray= x(m,n);
maskGray (b) =gray;
else
maskGray (b) =0;
end
end
% Distance and similarity are calculated between 9 pixels in the mask and the result is determined.
DN=150;
sim=0;
sum=0;
for b=1:9
sim (b)=similarity1(maskGray(b),maskGray(9),DN);
sum=sim(b)+sum;
end
%---------------------------------------------------------
S(i,j)=sum/9;
end
end
end
%------------------------------------------------------------------------
%FUNCTIONS
%-----------------------------------------------------------------------
function [ res ] = resimdemi(x,y,w,h )
if(x<1 || x>w-1 || y<1 || y>h-1)
res=0;
else
res=1;
end
end
function [x] = ikomsu( komsuno, i)
if(komsuno ==1)
x=i-1;
elseif(komsuno ==2)
x= i;
elseif(komsuno ==3)
x= i+1;
elseif(komsuno ==4)
x= i-1;
elseif(komsuno ==5)
x= i+1;
elseif(komsuno ==6)
x= i-1;
elseif(komsuno ==7)
x= i;
elseif(komsuno ==8)
x= i+1;
elseif(komsuno ==9)
x=i;
else
x=0;
end
end
function [y] = jkomsu( komsuno, j )
if(komsuno ==1)
y= j-1;
elseif(komsuno ==2)
y= j-1;
elseif(komsuno ==3)
y= j-1;
elseif(komsuno ==4)
y= j;
elseif(komsuno ==5)
y= j;
elseif(komsuno ==6)
y= j+1;
elseif(komsuno ==7)
y= j+1;
elseif(komsuno ==8)
y= j+1;
elseif(komsuno ==9)
y= j;
else
y=0;
end
end
function [sim] = similarity1( k, o, DN)
a=0;b=0;c=0;
a=abs(k-o);
% b=(abs(l-p))^2;
% c=(abs(m-r))^2;
% //Distance calculation based on the Euclidean relation
dist=a;
% % //Calculation of similarity based on linear function
arahes=dist/DN;
sim=1 - arahes;
% sim=exp(-dist/DN);
end
三、运行结果
四、备注
版本:2014a
以上是关于图像增强基于matlab GSA灰度图像增强含Matlab源码 1172期的主要内容,如果未能解决你的问题,请参考以下文章
图像增强基于matlab直方图均衡化图像增强含Matlab源码 960期
图像增强基于matlab Frangi滤波器血管图像增强含Matlab源码 2108期