[Image segmentation] Image segmentation based on matlab random walk algorithm [including Matlab source code 084]

1. Introduction

  1. About global optimization Solving
      global optimization is a very complex problem, and there is no universal method to solve the global optimal value for any complex function. The previous article explained a method for solving local minimums-gradient descent method. This method is practical for situations where the accuracy of the solution is not high, and the local minimum can be used to approximate the global minimum. But when it is required to accurately solve the global minimum, the gradient descent method is not applicable, and other methods are needed to solve it. Common methods to solve the global optimization include Lagrangian method, linear programming method, and some artificial intelligence algorithms such as genetic algorithm, particle swarm algorithm, simulated annealing algorithm, etc. (see my previous blog). But today I’m going to talk about a method that is simple to operate but not easy to fall into a local minimum: the random walk algorithm.
  2. Operation steps
    of random walk algorithm Let f(x)f(x) be a multivariate function with nn variables, x=(x1,x2,...,xn)x=(x1,x2,...,xn) is nn dimensions vector.
    Given the initial iteration point xx, the first walking step λλ, the control accuracy ϵϵ (ϵϵ is a very small positive number, used to control the end of the algorithm).
    Given the number of iteration control NN, kk is the current iteration number, set k=1k=1.
    When k<Nk<N, randomly generate a nn-dimensional vector between (−1,1)(−1,1) u=(u1,u2,...,un),(−1<ui<1,i =1,2,...,n)u=(u1,u2,...,un),(−1<ui<1,i=1,2,...,n), and standardize it to get u′=u∑ ni=1u2i√u′=u∑i=1nui2. Let x1=x+λu′x1=x+λu′ to complete the first step of walking.
    Calculate the value of the function, if f(x1)<f(x)f(x1)<f(x), that is, find a better point than the initial value, then reset kk to 1, change x1x1 to xx, and return Step 2; otherwise k=k+1k=k+1, go back to step 3.
    If no better value can be found for consecutive NN times, it is considered that the optimal solution is in the NN sphere with the current optimal solution as the center and the current step size as the radius (if it is three-dimensional, it just happens to be in space Sphere). At this point, if λ<ϵλ<ϵ, the algorithm ends; otherwise, let λ=λ2λ=λ2, go back to step 1, and start a new round of travel.

Second, the source code

clear ;
close all;
 
addpath 'algorithms'
out = ['results\'];
if ~exist(out)
    mkdir(out);
end
 
%% parameters            
only_name= '41004';
img_name = ['./imgs/' only_name '.jpg'];
ref_name = ['./scribbles/' only_name '.bmp'];
 
 
nei = 1;            % 0: 4-neighbors, 1: 8-neighbors
c = 0.0004;%1e-3;%         % restarting probability of RWR
sigma_c = 60;       % color variance
scale = 1.0;        % image resize
lambda = 2e-10;     % parameter for unitary
isKeepConnect = 0;  % 1: only consinder the connected regions with seeds;  0: otherwise.
reset(RandStream.getGlobalStream); % fix the random seed for initalization of GMM
 
saveProb = 0; % 1: save the probability image
%% main routine
img = imread(img_name); img = imresize(img,scale);
[K, labels, idx] = seed_generation(ref_name,scale);
 
%% RWR with prior
% run('vlfeat-0.9.13/toolbox/vl_setup');
st=clock;
[posteriors label_img] = do_RWR_prior(img,idx,labels,c,lambda,nei,sigma_c,isKeepConnect);
fprintf('subRW took %.2f second\n',etime(clock,st));
 
% display
[imgMasks,segOutline,imgMarkup]=segoutput(im2double(img),label_img); %clear imgMasks segOutline;
 
outPath = [out,'ours\'];
if ~exist(outPath)
    mkdir(outPath);
end
 
figure; clf;set(gcf,'Position',[100,500,size(img,2)*(K+1),size(img,1)]);
for k=1:K 
    prob_img = sc(posteriors(:,:,k),'prob_jet');
    if saveProb == 1
        imwrite(prob_img,[outPath,only_name,'_prob',num2str(k),'.png']);
    end
    subplot(1,K+1,k); imshow(prob_img); clear prob_img;
end;
subplot(1,K+1,K+1); imshow(imgMarkup);
figure,imshow((K-imgMasks)/(K-1));
 
imwrite(imgMarkup,[outPath,only_name,'_bound.png']);
imwrite((K-imgMasks)/(K-1),[outPath,only_name,'_binary.png']);

Three, running results

Insert picture description here
Insert picture description here

Four, remarks

Complete code or writing add QQ2449341593 past review
>>>>>>
[Matlab 024] [Image processing 1] Image compression of Matlab image processing tutorial series
[Matlab 025] [Image processing 2] Matlab image processing tutorial series Image segmentation (1)
[Matlab 026 issue] [Image processing 3] Image segmentation of Matlab image processing tutorial series (2)
[Matlab 029] [Image processing 4] Matlab fingerprint recognition
[Matlab 030] [Image processing 5] Bank Card number recognition matlab source code
[Matlab 074] [Image processing 6] [Image clustering] Based on FCM and improved FCM brain CT image clustering processing
[Matlab 075] [Image processing 7] [Image evaluation] Based on CCF algorithm Image quality evaluation
[Matlab 076] [Image processing 8] [Image enhancement] CLAHE algorithm based on local contrast enhancement-histogram enhancement
[Matlab 077] [Image processing 9] [Image fusion] Image fusion based on Frequency Partition
[ Matlab Issue 078] [Image Processing 10] [Image Evaluation] Image quality evaluation based on svm without reference
[Image Edge Detection] Matlab source code of ellipse edge detection based on least square method [Matlab Issue 079] [Image Processing 11]
[Image Encryption] Image encryption and decryption based on chaotic system matlab source code with GUI [Matlab 080 period] [Image processing 12]
[Image processing] Based on DWT+DCT+PBFO to improve image watermark hiding and extraction matlab source code with GUI [Matlab 081 period] [Image processing 13]
[Image registration] Image registration matlab source code based on sift algorithm [Matlab 082] [Image processing 14]
[Image fusion] Image fusion matlab source code based on CBF algorithm [Matlab 083] [Image processing 15]
[Image filtering] Image two-dimensional bilateral Gaussian filtering [Matlab 085] [Image processing 17]
[Image denoising] Image denoising based on adaptive morphology [Matlab 086] [Image processing 18]
[Image enhancement] Water based on DEHAZENET and HWD Down-scattering image enhancement [Matlab 087] [Image processing 19]
[Image enhancement] PSO optimization ACE image enhancement matlab source code [Matlab 088] [Image processing 20]
[Image enhancement] Based on region similarity transformation function and dragonfly algorithm Gray image enhancement [Matlab 089] [Image processing 21]

Guess you like

Origin blog.csdn.net/TIQCmatlab/article/details/112986678