# Level Set algorithm not working properly

1 view (last 30 days)
Warid Islam on 12 Oct 2022
Answered: Maneet Kaur Bagga on 15 Sep 2023
I am trying to segment gastric tumor. 'b1.png' is the original image. After applying level set segmentation, I get the result as definded in 'b2.png' which is not correct. As eviden, the red lines in the top left corder of the image is not right. I am trying to start the contour around the middle of the image(where I have the mass). But I am not getting the desired result. Any suggestions would be very much appreciated. Thank you.
Img=double(I(:,:,1));
%% parameter setting
timestep=1; % time step
mu=0.2/timestep; % coefficient of the distance regularization term R(phi)
iter_inner=5;
iter_outer=20;
lambda=5; % coefficient of the weighted length term L(phi)
alfa=-3; % coefficient of the weighted area term A(phi)
epsilon=1.5; % papramater that specifies the width of the DiracDelta function
sigma=.8; % scale parameter in Gaussian kernel
G=fspecial('gaussian',15,sigma); % Caussian kernel
Img_smooth=conv2(Img,G,'same'); % smooth image by Gaussiin convolution
f=Ix.^2+Iy.^2;
g=1./(1+f); % edge indicator function.
% initialize LSF as binary step function
c0=2;
initialLSF = c0*ones(size(Img));
% generate the initial region R0 as two rectangles
initialLSF(25:35,20:25)=-c0;
initialLSF(25:35,40:50)=-c0;
phi=initialLSF;
figure(1);
mesh(-phi); % for a better view, the LSF is displayed upside down
hold on; contour(phi, [0,0], 'r','LineWidth',2);
title('Initial level set function');
view([-80 35]);
figure(2);
imagesc(Img,[0, 255]); axis off; axis equal; colormap(gray); hold on; contour(phi, [0,0], 'r');
title('Initial zero level contour');
pause(0.5);
potential=2;
if potential ==1
potentialFunction = 'single-well'; % use single well potential p1(s)=0.5*(s-1)^2, which is good for region-based model
elseif potential == 2
potentialFunction = 'double-well'; % use double-well potential in Eq. (16), which is good for both edge and region based models
else
potentialFunction = 'double-well'; % default choice of potential function
end
% start level set evolution
for n=1:iter_outer
phi = drlse_edge(phi, g, lambda, mu, alfa, epsilon, timestep, iter_inner, potentialFunction);
if mod(n,2)==0
figure(2);
imagesc(Img,[0, 255]); axis off; axis equal; colormap(gray); hold on; contour(phi, [0,0], 'r');
end
end
% refine the zero level contour by further level set evolution with alfa=0
alfa=0;
iter_refine = 10;
phi = drlse_edge(phi, g, lambda, mu, alfa, epsilon, timestep, iter_inner, potentialFunction);
finalLSF=phi;
figure(2);
imagesc(Img,[0, 255]); axis off; axis equal; colormap(gray); hold on; contour(phi, [0,0], 'r');
hold on; contour(phi, [0,0], 'r');
str=['Final zero level contour, ', num2str(iter_outer*iter_inner+iter_refine), ' iterations'];
title(str);
figure;
mesh(-finalLSF); % for a better view, the LSF is displayed upside down
hold on; contour(phi, [0,0], 'r','LineWidth',2);
view([-80 35]);
str=['Final level set function, ', num2str(iter_outer*iter_inner+iter_refine), ' iterations'];
title(str);
axis on;
[nrow, ncol]=size(Img);
axis([1 ncol 1 nrow -5 5]);
set(gca,'ZTick',[-3:1:3]);
set(gca,'FontSize',14)

Maneet Kaur Bagga on 15 Sep 2023
Hi Warid,
As per my understanding of the code, the areas that could potentially contribute to the undesired segmentation result are:
1. The initial contour is set as two rectangles in the code. This may not accurately represent the shape and location of the gastric tumor. Modify the initialization step to better align with the actual tumor region.
2. The number of iterations in the outer and inner loops ("iter_outer" and "iter_inner") can affect the level set evolution. Consider adjusting these values to find the optimal balance between computational efficiency and segmentation accuracy.
3. The code is currently using a double-well potential function. Modifying the function to single-well potential function might be helpful as they are more suitable for region-based models.
4. After the initial level set evolution, the code is refining the zero level contour with "alfa=0". Please add additional level set evolution steps to further improve the segmentation.
Hope this helps!
Thank You
Maneet Bagga

### Categories

Find more on Active contours in Help Center and File Exchange

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!