|
| 1 | +'''%MORPHDEMO Demonstrate morphology using animation |
| 2 | +% |
| 3 | +% MORPHDEMO(IM, SE, OPTIONS) displays an animation to show the principles |
| 4 | +% of the mathematical morphology operations dilation or erosion. Two |
| 5 | +% windows are displayed side by side, input binary image on the left and |
| 6 | +% output image on the right. The structuring element moves over the input |
| 7 | +% image and is colored red if the result is zero, else blue. Pixels in |
| 8 | +% the output image are initially all grey but change to black or white |
| 9 | +% as the structuring element moves. |
| 10 | +% |
| 11 | +% OUT = MORPHDEMO(IM, SE, OPTIONS) as above but returns the output image. |
| 12 | +% |
| 13 | +% Options:: |
| 14 | +% 'dilate' Perform morphological dilation |
| 15 | +% 'erode' Perform morphological erosion |
| 16 | +% 'delay' Time between animation frames (default 0.5s) |
| 17 | +% 'scale',S Scale factor for output image (default 64) |
| 18 | +% 'movie',M Write image frames to the folder M |
| 19 | +% |
| 20 | +% Notes:: |
| 21 | +% - This is meant for small images, say 10x10 pixels. |
| 22 | +% |
| 23 | +% See also IMORPH, IDILATE, IERODE. |
| 24 | +''' |
| 25 | + |
| 26 | +def morphdemo(input, se, op='erode', delay=0.1, scale=64): |
| 27 | + |
| 28 | + print("hello from morphdemo") |
| 29 | + |
| 30 | + # # movie option |
| 31 | + |
| 32 | + # input = idouble(input>0); |
| 33 | + |
| 34 | + # clf |
| 35 | + |
| 36 | + # %se = [0 1 0; 1 1 1; 0 1 0]; |
| 37 | + |
| 38 | + # white = [1 1 1] * 0.5; |
| 39 | + # red = [1 0 0] * 0.8; |
| 40 | + # blue = [0 0 1] * 0.8; |
| 41 | + |
| 42 | + # result = ones(size(input)) * 0.5; |
| 43 | + |
| 44 | + # subplot(121); |
| 45 | + # h1 = gca; |
| 46 | + # im = icolor(input); |
| 47 | + # h1 = image(im); |
| 48 | + # set(h1, 'CDataMapping', 'direct'); |
| 49 | + # axis equal |
| 50 | + |
| 51 | + # subplot(122); |
| 52 | + # h2 = image(result); |
| 53 | + # colormap(gray); |
| 54 | + # set(h2, 'CDataMapping', 'scaled'); |
| 55 | + # set(gca, 'CLim', [0 1]); |
| 56 | + # set(gca, 'CLimMode', 'manual'); |
| 57 | + # axis equal |
| 58 | + |
| 59 | + # nr_se = (numrows(se)-1)/2; |
| 60 | + # nc_se = (numcols(se)-1)/2; |
| 61 | + |
| 62 | + # for r=nr_se+1:numrows(input)-nr_se |
| 63 | + # for c=nc_se+1:numcols(input)-nc_se |
| 64 | + # im = icolor(input*0.7); |
| 65 | + |
| 66 | + # win = input(r-nr_se:r+nr_se, c-nc_se:c+nc_se); |
| 67 | + |
| 68 | + # rr = win .* se; |
| 69 | + # switch opt.op |
| 70 | + # case {'erode', 'min'} |
| 71 | + # if all(rr(find(se))) |
| 72 | + # color = blue; |
| 73 | + # result(r,c) = 1; |
| 74 | + # else |
| 75 | + # color = red; |
| 76 | + # result(r,c) = 0; |
| 77 | + # end |
| 78 | + # case {'dilate', 'max'} |
| 79 | + # if any(rr(find(se))) |
| 80 | + # color = blue; |
| 81 | + # result(r,c) = 1; |
| 82 | + # else |
| 83 | + # color = red; |
| 84 | + # result(r,c) = 0; |
| 85 | + |
| 86 | + |
| 87 | + # for i=-nr_se:nr_se |
| 88 | + # for j=-nc_se:nc_se |
| 89 | + # if se(i+nr_se+1,j+nc_se+1) > 0 |
| 90 | + # im(r+i,c+j,:) = min(1, im(r+i,c+j,:) + reshape(color, [1 1 3])); |
| 91 | + |
| 92 | + |
| 93 | + # set(h1, 'CData', im); |
| 94 | + |
| 95 | + # set(h2, 'CData', result); |
| 96 | + |
| 97 | + # if isempty(opt.movie) |
| 98 | + # pause(opt.delay); |
| 99 | + # else |
| 100 | + # frame1 = ireplicate(im, opt.scale); |
| 101 | + # frame2 = ireplicate(icolor(result), opt.scale); |
| 102 | + # frame = cat(2, frame1, frame2); |
| 103 | + # imwrite(frame, sprintf('%s/%04d.png', opt.movie, framenum)); |
| 104 | + # framenum = framenum+1; |
| 105 | + |
| 106 | + |
| 107 | + # if nargout > 0 |
| 108 | + # out = result > 0.6; |
| 109 | + # end |
0 commit comments