活动轮廓模型——CV模型

活动轮廓模型——CV模型

原理:见论文http://www.sfu.ca/~rpyke/research/vese_chan.pdf

博客:https://blog.csdn.net/u011460059/article/details/59538983

效果图



matlab部分代码:

clc
clear all;
close all;
addpath('other methods\');
%% path of saving
outDir1 = 'test';
mkdir(outDir1);
outDir2 = 'results\ChanVese';
mkdir(outDir2);
outDir3 = 'results\中间过程';
mkdir(outDir3);
%% input an image
[filename, filepath] = uigetfile({'*.jpg;*.ppm; jpeg *.;*.bmp;*.png;*.gif'},'Choose Input Image');
if isequal(filename,0) || isequal(filepath,0)
    disp('User pressed cancel')
    return
else
    fullfp = fullfile(filepath, filename);
end
Img = imread(fullfp);
imwrite(Img,strcat(outDir1,'\',filename));%save the original image

%% RGB image-->Gray image
if length(size(Img)) > 2
    Img = rgb2gray(Img);
end
figure(1),imshow(Img,'InitialMagnification',200);

%% set parameters and draw initial contour
img_magnify = 200;
mask_type   = 'rect';  %% mask_type = 'rect' or  'ellipse' or 'multiball'
num_iter    = 400;  %% may be revised! 
color       = 'g';     %% the color of the final contour
thresh      = 0;
length_reg  = 0.4;
display_interval = 50;
acObj = GeomAC(im2double(Img),img_magnify,num_iter,color,thresh,mask_type);
acObj.initRegions();  %% Initialize the level set
acObj.computeSDF();   %% Compute the signed distance function

%% Initial level set function
figure(2);
mesh(acObj.phi0);   % for a better view, the LSF is displayed upside down
hold on; 
title('Initial level set function');
view([-75 60]);
box on
saveas(gcf,strcat(outDir2,'\',strtok(filename,'.'),'_initial contour.bmp'));
hold off

%% compare experiments
% load('ImgMask3.mat');
% phi0 = 1-u;

u =1 - acObj.init_reg;
str = strcat(strtok(filename,'.'),'_mask');
save ImgMask u;

%% compare with other level set methods
tic;
intermed_phi = acObj.runChanVese(length_reg,display_interval);     % regularization-factor,display-interval
toc;

%% Final level set function
figure(4);
[aa,bb,cc] = size(intermed_phi);
mesh(intermed_phi(:,:,cc)'); % for a better view, the LSF is displayed upside down
hold on;  contour(intermed_phi(:,:,cc)', [0,0], 'g','LineWidth',2);
title('Final level set function');
view([60 45]);
box on
saveas(gcf,strcat(outDir2,'\',strtok(filename,'.'),'_final contour.bmp'));

%% Show Intermediate results
[~,~,l] = size(intermed_phi);
last = l-2;
figure(5);imshow(im2double(Img),'InitialMagnification',200); hold on;
%figure(5);imshow(uint8(Img),'InitialMagnification',200); hold on;
for ii = 1 : last
   if ii == 1       %-- Initial Contour
       GeomAC.showIntermediate(acObj.phi0(:,:,ii),'r',2);
       %GeomAC.showIntermediate(phi0(:,:,ii),'r',2);
   elseif ii == last   %-- Final Contour
       GeomAC.showIntermediate(intermed_phi(:,:,ii),'g',2);
%     else             %-- Intermediate output
%        GeomAC.showIntermediate(intermed_phi(:,:,ii),'c',0.2);
    end
end
hold off;

%%%%%%%%%%% save fig
[m,n] = size(Img);
set(gca,'units','pixels','Visible','off');
q=get(gca,'position');
q(1)=0;%设置左边距离值为零 
q(2)=0;%设置右边距离值为零
set(gca,'position',q);

%frame=getframe(gcf,[0,0,n,m]);%% for medical images
frame=getframe(gcf,[0,0,2*n,2*m]);

im=frame2im(frame);
imwrite(im,strcat(outDir2,'\',strtok(filename,'.'),'_segmentation.bmp'));

%% Test Heaviside-Dirac-gradient relationship
Dirac_global    = @(x,e) ((e/pi)./(e^2.+ x.^2));
Heaviside       = @(y,e) (0.5*(1+(2/pi)*atan(y/e)));

phi = acObj.phi;
eps = 2; 

h = Heaviside(phi,eps);
[hx,hy]=gradient(h);
g_h = sqrt(hx.^2+hy.^2);
delta = Dirac_global(phi,eps);

figure(6); 
subplot(2,2,1); imshow(phi); title('\phi');
subplot(2,2,2); imshow(h); title('H(\phi)');
subplot(2,2,3); imshow(g_h); title('|\nabla H(\phi)|');
subplot(2,2,4); imshow(delta); title('\delta(\phi)');
saveas(gcf,strcat(outDir2,'\',strtok(filename,'.'),'_compare.bmp'));
%% Show Intermediate results
inter_res = intermed_phi;
[~,~,l] = size(inter_res);
last = l-2;
for ii = 1 : last
    figure(4),imshow(im2double(Img),'InitialMagnification',200); hold on;
    if ii == 1       %-- Initial Contour
        showIntermediate(acObj.phi0(:,:,ii),'r',2);
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        set(gca,'units','pixels','Visible','off');
        q=get(gca,'position');
        q(1)=0;
        q(2)=0;
        set(gca,'position',q);
        
        frame=getframe(gcf,[0,0,2*n,2*m]);
        im=frame2im(frame);
        imwrite(im,strcat(outDir3,'\',number2string(ii),'.bmp'));
        
    else          
        showIntermediate(inter_res(:,:,ii),'g',2);
        
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        set(gca,'units','pixels','Visible','off');
        q=get(gca,'position');
        q(1)=0;
        q(2)=0;
        set(gca,'position',q);
        
        frame=getframe(gcf,[0,0,2*n,2*m]);
        im=frame2im(frame);
        imwrite(im,strcat(outDir3,'\',number2string(ii),'.bmp'));
        hold off
    end
end
%%  画矩形框
figure(7);
evolved_u = phi; 
finalResult = double(im2bw(evolved_u));
imshow(uint8(Img));
hold on;
[m,n]=size(finalResult);

L=bwlabel(finalResult);
stats=regionprops(L,'all');

set(gcf,'color','w');
set(gca,'units','pixels','Visible','off');
q=get(gca,'position');
q(1)=0;q(2)=0;
set(gca,'position',q);

for i=1:length(stats)
    title('分割效果');
    hold on;
    rectangle('position',stats(i).BoundingBox,'edgecolor','y','linewidth',2);
    temp = stats(i).Centroid;
    drawnow;
end

frame=getframe(gcf,[0,0,n,m]);
im=frame2im(frame);
%imwrite(im,strcat(outDir,'\',strtok(filename,'.'),'_seg.bmp')); % 保存分割效果图
imwrite(im,'segResult.bmp'); % 保存分割效果图
%% delete acObj
% delete(acObj);
% close all;



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页