% Example 3 - Isolate cancer cell
% -------------------------------
% start with clean slate
close all, clear all, clc
% Read image
I = imread('cell.tif');
figure, imshow(I), title('original image');
set(1,'pos',[6 77 246 224])
pause %detect cells
% Detect cells
BWs = edge(I, 'sobel', (graythresh(I) * .1));
figure, imshow(BWs), title('binary gradient mask');
set(2,'pos',[262 78 246 222])
pause %connect edges
% Connect edges
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BWs, [se90 se0]);
figure, imshow(BWsdil), title('dilated gradient mask');
set(3,'pos',[518 78 246 222])
pause %fill holes
% Fill interior gaps
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('holes filled');
set(4,'pos',[773 78 246 222])
pause %clear borders
% Remove connected objects on border
BWnobord = imclearborder(BWdfill, 4);
figure(3), imshow(BWnobord,'n'), title('cleared borders');
pause %too fat (dilation)
% Reduce/smooth outline
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure(2), imshow(BWfinal,'n'), title('segmented image');
pause %outline original
% Indicate outline over original image
BWoutline = bwperim(BWfinal);
Segout = imadd(I, immultiply(BWoutline, 255));
figure(1), imshow(Segout,'n'), title('detect cell outlined');
% Copyright 2003-2010 RBemis The MathWorks, Inc.