% demoBiasedNcutImage % % demo for BiasedNcutImage % % author : Subhransu Maji, Feb 2011. % % Biased Normalized Cuts, % Subhransu Maji, Nisheeth Vishnoi, Jitendra Malik, CVPR 2011. % % based on the code from : % Timothee Cour, Stella Yu, Jianbo Shi, 2004. disp('BiasedNcut Image Segmentation demo'); %% read image, change color image to brightness image, resize to 160x160 I = imread_ncut('jpg_images/3.jpg',160,160); %% display the image figure(1);clf; imagesc(I);colormap(gray);axis off; disp('This is the input image to segment, press Enter to continue...'); pause; %% compute the edges imageEdges, the similarity matrix W based on %% Intervening Contours, the Ncut eigenvectors and discrete segmentation nbSegments = 20; disp('computing Ncut eigenvectors ...'); tic; [SegLabel,NcutDiscrete,NcutEigenvectors,NcutEigenvalues,W,imageEdges]= NcutImage(I,nbSegments); disp(['The computation took ' num2str(toc) ' seconds on the ' num2str(size(I,1)) 'x' num2str(size(I,2)) ' image']); % the smallest eigenvalue is zero NcutEigenvalues = 1 - NcutEigenvalues; %% precompute the diagonal matrix; [wx, wy] = size(W); x = 1 : wx; S = full(sum(W, 1)); D = sparse(x, x, S, wx, wy); clear x S; %% biased normalized cut demo ROIMODE = 0; % input is ROI or a set of points numinput = 6; % number of input points [nr,nc,nb] = size(I); wts = zeros(length(NcutEigenvalues),1); % display biased ncuts for various values of gamma NUMDISPLAY = 9; m = round(sqrt(NUMDISPLAY)); n = ceil(NUMDISPLAY/m); while(1), figure(1); clf; imshow(I/255); hold on; if(ROIMODE) title('draw a polygon.'); else title(sprintf('click %i points',numinput)); end if(ROIMODE) [bw,xi,yi] = roipoly(I/255); else [cx,cy] = ginput(numinput); bw = zeros(nr,nc); cx = round(cx); cy = round(cy); for i = 1:length(cx), bw(cy(i),cx(i)) = 1; end plot(cx,cy,'b+'); plot(cx,cy,'bo'); end %make the normalized seed vector tic; s = D*bw(:); figure(2); clf; for j = 1:NUMDISPLAY, gamma = (1 - 10^(j-1))*NcutEigenvalues(2); for i = 2:length(NcutEigenvalues) wts(i) = (NcutEigenvectors(:,i)'*s)/(NcutEigenvalues(i) - gamma); end WEigVect = NcutEigenvectors*wts; subplot(m,n,j); imagesc(reshape(WEigVect,nr,nc)); axis image; axis off; title(sprintf('\\gamma=%.2d', gamma)); colormap gray; end toc; fprintf('press: n next, t toggle-mode, <-,-> decrease,increase points\n'); % process keypoint input [x,y,button] = ginput(1); switch(button) case 110 % space - next one continue; case 27 % esc - stop the interactive segmentation break; case 116 % t - toggle ROIMODE = ~ROIMODE; case 28 % <- numinput = max(numinput-1,1); case 29 % -> numinput = numinput+1; end end