A brief introduction of particle swarm optimization adaptive multi-threshold image segmentation

Based on MATLAB image processing image segmentation particle swarm optimization multi-threshold image self-segmentation algorithm

Two, some source code

clc; clear; close all; %% input image; Imag = imread('24063.jpg'); %296059Imag=rgb2gray(Imag); Image_OSTU=Imag; %% start population and other basic definition N =500; % Initial population d =2; % number of thresholds (see above function expression) ger =300; % Maximum number of iterations plimit = [1.256];
          
vlimit = [2.5.2.5;2.5.2.5]; % Set speed limit w =0.8; % inertia weight, the impact of individual historical achievements on the present0.5~1There are adaptive weight, random weight, etc. (different weight Settings affect performance, select as needed) c1 =0.5; % Self-learning factor c2 =0.5; % group learning factor x = zeros(N,2);  
for i = 1:N % for each individual x(I,1) = floor(plimit(1) + (plimit(2) - plimit(1)) * rand); % Initial population position x(I,2) = floor(x(i,1) + (plimit(2) - x(i,1)) * rand); End v = rand(N, d); % initial population speed,500line2The columns are xm = x in two dimensions; % Historical best location for each individual ym = zeros(1, d); % historical best location of population, two dimensions, set to0
fxm = zeros(N, 1); % Historical best fitness for each individual, set to0fym = -inf; % population history best fitness, the maximum value is set to negative infinite iter=1; % initial number of iterations because of usewhileSet this to a times =1; 
record = zeros(ger, 1); The % logger %% iteration update beginswhileiter <= ger fx = f(x); % is substituted into the two-dimensional data in X, and the individual's current fitness is calculated500line1Columns of datafor i = 1:N % Judge each individualifFXM (I) < FX (I) % if the historical best fitness of each individual is less than the current fitness of the individual FXM (I) = FX (I); Xm (I,:) = x(I,:); % Update individual history best location end endifFym < Max (FXM) % Optimal fitness in population history was less than the maximum fitness in individuals [FYM, nmax] = Max (FXM); End v = v * w + c1 * rand *(xm-x) + c2 * rand *(repmat(ym, N,1) - x); % speed update formula,repmat function expands YM matrix to N rows1Column %% boundary speed processingfor j=1:N
        if  v(j,i)>vlimit(i,2If the speed is greater than the boundary speed, pull the speed back to the boundary v(j, I)=vlimit(I,2);
        end
        if  v(j,i) < vlimit(i,1)% If the speed is less than the boundary speed, the speed is pulled back to the boundaryv(j,i)=vlimit(i,1);
        end
        end
 end      
   
 x = floor(x + v); % location updatefor j=1:N
        if  x(j,1)> plimit(2)
            x(j,1)=plimit(2);
        end
        if  x(j,1) < plimit(1)
            x(j,1)=plimit(1);
        end
        if  x(j,2)> plimit(2)
            x(j,2)=plimit(2);
        end
        if  x(j,2) < x(j,1)
            x(j,2)=x(j,1); End end %% figure(3); plot(record); % draw the process of the maximum value title('Convergence process')

threshold1 = ym(1);
threshold2 = ym(2);
[height,length]=size(Image_OSTU);
for i=1:length
    for j=1:height
        if Image_OSTU(j,i)>=threshold2
            Image_OSTU(j,i)=255;
        elseif Image_OSTU(j,i)<=threshold1
            Image_OSTU(j,i)=0;
        else  
            Image_OSTU(j,i)=(threshold1+threshold2)/2;
        end
    end
end
figure(4);
imshow(Image_OSTU);
xlabel(['Maximum class difference threshold',num2str(ym)]); Function fx = f(x) Imag = imread('24063.jpg'); %296059   
Imag=rgb2gray(Imag);

[height,length]=size(Imag);
totalNum=height*length;

pixelCount=zeros(1.256); % Count the number of values of each pixelfor i=1:length
    for j=1:height
        number=Imag(j,i)+1;
        pixelCount(number)=pixelCount(number)+1;
    end
end


a=1:256;

fx = zeros(1.500); 
for i=1:500
    m=x(i,1);
    n=x(i,2);
    w0=sum(pi(1:m));
    w1=sum(pi(m+1:n));
    w2=sum(pi(n+1:256));
    mean0=sum(pi(1:m).*a(1:m))/w0;
    mean1=sum(pi(m+1:n).*a(m+1:n))/w1;
    mean2=sum(pi(n+1:256).*a(n+1:256))/w2;
    fx(i)=w0*w1*(mean0-mean1)^2+w0*w2*(mean0-mean2)^2+w1*w2*(mean1-mean2)^2;
end
end

Copy the code

3. Operation results

Matlab version and references

1 matlab version 2014A

2 Reference [1] CAI Limei. MATLAB Image Processing — Theory, Algorithm and Case Analysis [M]. Tsinghua University Press, 2020. [2] Yang Dan, ZHAO Haibin, LONG Zhe. Examples of MATLAB Image Processing In detail [M]. Tsinghua University Press, 2013. [3] Zhou Pin. MATLAB Image Processing and Graphical User Interface Design [M]. Tsinghua University Press, 2013. [4] LIU Chenglong. Proficient in MATLAB Image Processing [M]. Tsinghua University Press, 2015.