matlab帮助的文件不能运行,matlab中的M文件运行不了

R2009b版本

function B = boundaries(BW,conn,dir)

%BOUNDARIES Trace object boundaries功能:将图像二值后,边缘点的连接,4联通,8联通都可。非常实用!

if nargin < 3       %函数输入项小于3

dir = 'cw';     %顺时针方向

end

if nargin < 2       %函数输入项小于2

conn = 8;

end

L = bwlabel(BW,conn);  %标记目标和背景,背景用0,目标用1,多一个目标用2标记

numObjects = max(L(:));%目标数就是L矩阵中的最大者

if numObjects > 0

B = {zeros(0,2)};

B = repmat(B,numObjects,1);

else

B ={};

end

Lp = padarray(L,[1 1],0,'both');

M = size(Lp,1);

if conn == 8

offsets = [-1,M - 1,M,M + 1,1,-M + 1,-M,-M-1];

else

offsets = [-1,M,1,-M];

end

if conn == 8

next_search_direction_lut = [8 8 2 2 4 4 6 6];

else

next_search_direction_lut = [4 1 2 3];

end

if conn == 8

next_direction_lut = [2 3 4 5 6 7 8 1];

else

next_direction_lut = [2 3 4 1];

end

START = -1;

BOUNDARY = -2;

scratch = zeros(100,1);

[rr,cc] = find((Lp(2:end-1,:) > 0) & (Lp(1:end-2 ,:) == 0));

rr = rr + 1;

for k = 1:length(rr)

r = rr(k);

c = cc(k);

if (Lp(r,c) > 0) & (Lp(r - 1,c) == 0) & isempty(B{Lp(r,c)})

idx = (c-1)*size(Lp,1) + r;

which = Lp(idx);

scratch(1) = idx;

Lp(idx) = START;

numPixels = 1;

currentPixel = idx;

initial_departure_direction = [];

done = 0;

next_search_direction = 2;

while ~done

direction = next_search_direction;

found_next_pixel = 0;

for k = 1:length(offsets)

neighbor = currentPixel + offsets(direction);

if Lp(neighbor) ~= 0

if (Lp(currentPixel) == START) & isempty(initial_departure_direction)

initial_departure_direction = direction;

elseif (Lp(currentPixel) == START) & (initial_departure_direction == direction)

done = 1;

found_next_pixel = 1;

break;

end

next_search_direction = next_search_direction_lut(direction);

found_next_pixel = 1;

numPixels = numPixels +1;

if numPixels > size(scratch,1)

scratch(2*size(scratch,1)) = 0;

end

scratch(numPixels) = neighbor;

if Lp(neighbor) ~= START

Lp(neighbor) = BOUNDARY;

end

currentPixel = neighbor;

break;

end

direction = next_direction_lut(direction);

end

if ~found_next_pixel

numPixels = 2;

scratch(2) = scratch(1);

done = 1;

end

end

[row,col] = ind2sub(size(Lp),scratch(1:numPixels));

B{which} = [row - 1,col - 1];

end

end

if strcmp(dir,'ccw')

for k = 1:length(B)

B{k} = B{k}(end:-1:1,:);

end

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值