matlab morph,morphdemo.m · zigzag2015/machinevision-toolbox-matlab - Gitee.com

本文介绍了一个名为MORPHDEMO的函数,通过动画展示二值图像的膨胀和腐蚀操作原理。使用彩色结构元素在输入图像上移动,显示操作结果。该教程适用于小规模图像,并提供了输出图像、延迟选项和可选的动画保存功能。
摘要由CSDN通过智能技术生成

%MORPHDEMO Demonstrate morphology using animation

%

% MORPHDEMO(IM, SE, OPTIONS) displays an animation to show the principles

% of the mathematical morphology operations dilation or erosion. Two

% windows are displayed side by side, input binary image on the left and

% output image on the right. The structuring element moves over the input

% image and is colored red if the result is zero, else blue. Pixels in

% the output image are initially all grey but change to black or white

% as the structuring element moves.

%

% OUT = MORPHDEMO(IM, SE, OPTIONS) as above but returns the output image.

%

% Options::

% 'dilate' Perform morphological dilation

% 'erode' Perform morphological erosion

% 'delay' Time between animation frames (default 0.5s)

% 'scale',S Scale factor for output image (default 64)

% 'movie',M Write image frames to the folder M

%

% Notes::

% - This is meant for small images, say 10x10 pixels.

%

% See also IMORPH, IDILATE, IERODE.

function out = morphdemo(input, se, varargin)

opt.op = {[], 'erode', 'dilate', 'min', 'max'};

opt.delay = 0.1;

opt.movie = [];

opt.scale = 64;

opt = tb_optparse(opt, varargin);

if ~isempty(opt.movie)

mkdir(opt.movie);

framenum = 1;

end

input = idouble(input>0);

clf

%se = [0 1 0; 1 1 1; 0 1 0];

white = [1 1 1] * 0.5;

red = [1 0 0] * 0.8;

blue = [0 0 1] * 0.8;

result = ones(size(input)) * 0.5;

subplot(121);

h1 = gca;

im = icolor(input);

h1 = image(im);

set(h1, 'CDataMapping', 'direct');

axis equal

subplot(122);

h2 = image(result);

colormap(gray);

set(h2, 'CDataMapping', 'scaled');

set(gca, 'CLim', [0 1]);

set(gca, 'CLimMode', 'manual');

axis equal

nr_se = (numrows(se)-1)/2;

nc_se = (numcols(se)-1)/2;

for r=nr_se+1:numrows(input)-nr_se

for c=nc_se+1:numcols(input)-nc_se

im = icolor(input*0.7);

win = input(r-nr_se:r+nr_se, c-nc_se:c+nc_se);

rr = win .* se;

switch opt.op

case {'erode', 'min'}

if all(rr(find(se)))

color = blue;

result(r,c) = 1;

else

color = red;

result(r,c) = 0;

end

case {'dilate', 'max'}

if any(rr(find(se)))

color = blue;

result(r,c) = 1;

else

color = red;

result(r,c) = 0;

end

end

for i=-nr_se:nr_se

for j=-nc_se:nc_se

if se(i+nr_se+1,j+nc_se+1) > 0

im(r+i,c+j,:) = min(1, im(r+i,c+j,:) + reshape(color, [1 1 3]));

end

end

end

set(h1, 'CData', im);

set(h2, 'CData', result);

if isempty(opt.movie)

pause(opt.delay);

else

frame1 = ireplicate(im, opt.scale);

frame2 = ireplicate(icolor(result), opt.scale);

frame = cat(2, frame1, frame2);

imwrite(frame, sprintf('%s/%04d.png', opt.movie, framenum));

framenum = framenum+1;

end

end

end

if nargout > 0

out = result > 0.6;

end

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值