A brief introduction of genetic algorithm adaptive multi-threshold image segmentation
Matlab image processing image segmentation based on genetic algorithm adaptive optimal threshold image segmentation technology
Two, some source code
function main(a)
clear; clc; % Population size popsize=100; % Binary encoding length chromlength=10; % crossover probability PC =0.6; % variation probability PM =0.001; % initial population pop = initpop(popsize,chromlength);for i = 1:100% Calculate fitness value(function value) objValue = cal_objvalue(pop); fitvalue = objvalue; % select newPOP = Selection (pop, fitValue); % newpop = crossover(newpop, PC); % newPOP = mutation(newPOP, PM); % update population pop = newPOP; % [bestIndividual,bestfit] = best(pop, fitValue); x2 = binary2decimal(bestindividual); x1 = binary2decimal(newpop); y1 = cal_objvalue(newpop);if mod(i,50) = =0figure; %Matlab figure command, can create a window object used to display graph output fplot(@(x)x+10.*sin(5.*x)+7.*cos(4.*x),[5.20]);
%fplot('x+10*sin(5*x)+7*cos(4*x)'[5 20]); % adaptive drawing, the first parameter is a function, the second parameter is the value range of coordinate axis hold on; % hold on function is to keep the original picture and accept the new curve drawn after, overlay drawing; Plot: the plot command allows you to plot multiple curves simultaneously (x1,y1,The '*'); % Best solution to mark title([) with *'Number of iterations n='num2str(i)]); The %num2str function converts a numeric value to a string %plot(x1,y1,The '*');
end
end
fprintf('The best X is -->>%5.2f\n',x2); % Specifies that the data is output in decimal formatfprintf('The best Y is -->>%5.2f\n',bestfit);
end
function pop=initpop(popsize,chromlength)
pop = round(rand(popsize,chromlength));
end
function pop2 = binary2decimal(pop)
lb=5; The lower bound of %x is ub=20; %x upper bound [px,py]=size(pop); %px is the number of rows of the matrix, py is the number of columns of the matrix, where px is the number of populations, py is the length of chromosomes %[r,c]=size(A). When there are two output parameters, size returns the number of rows of the matrix to the first output variable R, and the number of columns of the matrix to the second output variable C.for i = 1:py
pop1(:,i) = 2.^(py-i).*pop(:,i); %py-i is equivalent to saying the order from the first column to the last column is reversed end %sum(.,2Sum the columns temp = sum(pop1,2);
pop2 = 5+(temp*(ub-lb)/1023); % is based on the value range of X2Into the system to10Base conversion, (UB - LB) corresponds to a line segment, temp/1023This is the ratio of binary to the interval, which is then multiplied by (UB-lb) to get its position on the line segment, plus the lower bound, which is converted to decimal on the interval. end function [objvalue] = cal_objvalue(pop) x = binary2decimal(pop); % converts the binary number to the value of x variable variable objValue = x+10*sin(5*x)+7*cos(4*x); End % How to select the new individual % input variables: POP binary population, FITValue: fitness value % Output variables: Function [newPOP] = Selection (pop, fitValue) % totalfit = sum(fitvalue); P_fitvalue = fitValue /totalfit; % The proportion of each fitness value in the wheel, that is, the relative fitness size, is the probability of each individual being inherited to the next generation population p_FITValue = cumsum(p_FitValue); Cumsum () = sum ms = sort(rand(px,1)); % from smallest to largest to generate population size0- 1According to which random number appears in the above probability area to determine the number of times each individual is selected in % fitin =1;
newin = 1; The % line selects the scalar % to select the new populationwhileNewin <=px % the initial row vector is1Start with the first lineif(ms(newin))< p_FITValue (fitin)% Heritable when the resulting random number < the probability of an individual being inherited into the next generation of populationelse
fitin=fitin+1; If not heritable, examine the next larger areaend
end
end
if(rand<pc)%rand generates a random cpoint between 0 and 1 from the uniform distribution= round(rand*py); Newpop (I,:) = [pop(I,:))1:cpoint),pop(i+1,cpoint+1:py)]; % intercepts the and before the intersection of line I and the I +1Newpop (I +) after row intersections1,:) = [pop(i+1.1:cpoint),pop(i,cpoint+1:py)]; % intercepts the I +1Join the one before the row intersection and the one after the row I intersectionelseNewpop (I,:) = pop(I,:); newpop(I,:); newpop(i+1,:) = pop(i+1, :); end end end function [newpop] = mutation(pop,pm) [px,py] = size(pop); newpop = ones(size(pop));for i = 1:px % loops from the first line to the lastifMpoint = round(rand*py); mpoint = round(rand*py); % then produces the position of the variation pointif mpoint <= 0
mpoint = 1;
end
newpop(i,:) = pop(i,:);
if newpop(i,mpoint) = =0
newpop(i,mpoint) = 1;
else newpop(i,mpoint) == 1
newpop(i,mpoint) = 0; % is the inverse of the mutation point in row Iend
else newpop(i,:) = pop(i,:); end end end function [bestindividual, bestfit] = best(pop,fitvalue) [px,py] = size(pop); % Get the number of rows and columns of the matrix, size () give the number of rows and columns of the matrix to px and py bestIndividual = pop(1, :); % pops all columns in the first row to bestIndividual, and the pop() function removes an element from the list (the last element by default) and returns the value of that element. bestfit = fitvalue(1); The % lines are equivalent to initializing the best individual and best fitness valuesfor i = 2:px %forThe loop starts at the second line,ifFitvalue (I)>bestfit % if the fitvalue is greater than the bestfit, bestindividual = pop(I,:); % take the row as the best individual, bestFit = fitValue (I); % The line adaptation is the best adaptation end end endCopy 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. Master MATLAB Image Processing [M]. Tsinghua University Press, 2015. [5] WU Chang ‘an, YU Nan. Research on Adaptive Optimal Threshold Image Segmentation Based on Genetic Algorithm [J]. Optical Disc Technology, 2007,(01)