-
Notifications
You must be signed in to change notification settings - Fork 2
/
random_walker_example.m
67 lines (59 loc) · 1.37 KB
/
random_walker_example.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%Script gives a sample usage of the random walker function for image
%segmentation
%
%
%10/31/05 - Leo Grady
clear
close all
%Read image
img=im2double(imread('axial_CT_slice.bmp'));
[X Y]=size(img);
%Set two seeds
s1x=130; s1y=150; %Note that seed location is not central to object
s2x=200; s2y=200;
s3x=210; s3y=210;
%Apply the random walker algorithms
[mask,probabilities] = random_walker(img,[sub2ind([X Y],s1y,s1x),sub2ind([X Y],s2y,s2x),sub2ind([X Y],s3y,s3x)],[1,2,3]);
%Display results
figure
imagesc(img);
colormap('gray')
axis equal
axis tight
axis off
hold on
plot(s1x,s1y,'g.','MarkerSize',24)
plot(s2x,s2y,'b.','MarkerSize',24)
title('Image with foreground (green) and background (blue) seeds')
figure
imagesc(mask)
colormap('gray')
axis equal
axis tight
axis off
hold on
plot(s1x,s1y,'g.','MarkerSize',24)
plot(s2x,s2y,'b.','MarkerSize',24)
title('Output mask');
figure
[imgMasks,segOutline,imgMarkup]=segoutput(img,mask);
imagesc(imgMarkup);
colormap('gray')
axis equal
axis tight
axis off
hold on
plot(s1x,s1y,'g.','MarkerSize',24)
plot(s2x,s2y,'b.','MarkerSize',24)
title('Outlined mask')
figure
imagesc(probabilities(:,:,1))
colormap('gray')
axis equal
axis tight
axis off
hold on
plot(s1x,s1y,'g.','MarkerSize',24)
plot(s2x,s2y,'b.','MarkerSize',24)
title(strcat('Probability at each pixel that a random walker released ', ...
'from that pixel reaches the foreground seed'));