您好,欢迎来到华佗小知识。
搜索
您的当前位置:首页二值图像的轮廓跟踪

二值图像的轮廓跟踪

来源:华佗小知识


function C = contour_following(BW) 定义函数

% CONTOUR_FOLLOWING takes a binary array and returns the sorted row and

% column coordinates of contour pixels. CONTOUR_FOLLOWING一个二进制的数组,并返回排序的行和轮廓像素的列坐标

% C = CONTOUR_FOLLOWING(BW) takes BW as an input. BW is a binary array

% containing the image of an object ('1': foreground, '0': background). It

% returns a circular list (N x 2, C(1,:)=C(end,:)) of the

% (row,column)-coordinates of the object's contour, in the order of

% appearence (This function was inspired from the freeman contour coding

% algorithm).C = CONTOUR_FOLLOWING(BW)作为输入体重。BW是一个二进制的数组,包含一个对象的图像('1':前景,'0':背景)。它返回一个循环链表(为N×2,C级(1,:) =(年底,:))在appearence秩序,对象的轮廓(行,列)坐标(此功能的启发弗里曼轮廓编码算法)

% Note:

% - if the object is less than 3 pixels, CONTOUR_FOLLOWING sends back [0 0]. 如果对象是小于3个像素,CONTOUR_FOLLOWING发回[0]。

% - the algorithm is quite robust: the object can have holes, and can also

% be only one pixel thick in some parts (in this case, some coordinates

% pair will appear two times: they are counted \"way and back\").

算法是相当稳健的对象可以有洞,也可以是只有一个像素厚(在这种情况下,在一些地区,一些坐标对将出现两次:他们都算“回溯”)

[m,n]=size(BW); % getting the image height and width

获取图像的高度和宽度

Itemp=zeros(m+2,n+2); % we create a '0' frame around the image to avoid border problems

创建0为图像帧,以避免边界问题

Itemp(2:(m+1),2:(n+1))=BW;

BW=Itemp;

BW = BW - imerode(BW,[0 1 0 ; 1 1 1 ; 0 1 0]); % gets the contour by substracting the erosion to the image

获得减去侵蚀图像轮廓

BW bwmorph(BW,'thin',Inf); sure to have strictly 8-connected contour

一定要有严格的八连轮廓

if

(sum(sum(BW))<3), we consider that less than 3 pixels cannot make a contour

我们认为不到三个像素不算一个轮廓

C=[0 0];

return;

end;

=

% to be %

[row,col]=find(BW,1); % takes the first encountered '1' pixel as the starting point of the contour

以第一次遇到1像素为轮廓起点

MAJ=[6 6 0 0 2 2 4 4]; variable initialization 变量初始化

C=[0 0 ; 0 0];

k=0;

ended=0;

direction=4;

while(ended==0),

k=k+1;

found_next=0;

while(found_next==0),

%

switch mod(direction,8),

case 0,

if (BW(row, col+1)==1),

row=row;

col=col+1;

C(k,:)=[row col];

found_next=1;

end;

case 1;

if (BW(row+1, col+1)==1),

row=row+1;

col=col+1;

C(k,:)=[row col];

found_next=1;

end;

case 2;

if (BW(row+1, col)==1),

row=row+1;

col=col;

C(k,:)=[row col];

found_next=1;

end;

case 3;

if (BW(row+1, col-1)==1),

row=row+1;

col=col-1;

C(k,:)=[row col];

found_next=1;

end;

case 4;

if (BW(row, col-1)==1),

row=row;

col=col-1;

C(k,:)=[row col];

found_next=1;

end;

case 5;

if (BW(row-1, col-1)==1),

row=row-1;

col=col-1;

C(k,:)=[row col];

found_next=1;

end;

case 6;

if (BW(row-1, col)==1),

row=row-1;

col=col;

C(k,:)=[row col];

found_next=1;

end;

case 7;

if (BW(row-1, col+1)==1),

row=row-1;

col=col+1;

C(k,:)=[row col];

found_next=1;

end;

end

if (found_next==0), direction=direction+1; end;

end

if(and((length(C)>3),(([C(1,:) C(2,:)]==[C((end-1),:) C(end,:)])))),

ended=1;

end;

direction = MAJ((mod(direction,8)+1));

end

C=C(1:(end-1),:); the first and last points in the list are the same (circular list)

列表中第一个点和最后一个点是相同的

C=C-1; % to go back to the original coordinates (without the '0' frame)

返回到原来自坐标(没有0)

%

% TEST SCRIPT OF THE FUNCTION \"CONTOUR_FOLLOWING\"

测试脚本函数的轮廓开放测试函数后

%test images opening

I1=rgb2gray(imread('test1.bmp'));

I2=rgb2gray(imread('test2.bmp'));

I3=rgb2gray(imread('test3.bmp'));

I4=rgb2gray(imread('test4.bmp'));

I5=rgb2gray(imread('test5.bmp'));

%function use函数调用

C1 = contour_following(I1);

C2 = contour_following(I2);

C3 = contour_following(I3);

C4 = contour_following(I4);

C5 = contour_following(I5);

%plotting the found contours画出轮廓

figure, imshow(I1);

figure, plot(C1(:,1),C1(:,2),'*');

for j=1:size(C1,1),

text(C1(j,1),C1(j,2),[' ' num2str(j)]);

end

figure, imshow(I2);

figure, plot(C2(:,1),C2(:,2),'*');

for j=1:size(C2,1),

text(C2(j,1),C2(j,2),[' ' num2str(j)]);

end

figure, imshow(I3);

figure, plot(C3(:,1),C3(:,2),'*');

for j=1:size(C3,1),

text(C3(j,1),C3(j,2),[' ' num2str(j)]);

end

figure, imshow(I4);

figure, plot(C4(:,1),C4(:,2),'*');

for j=1:size(C4,1),

text(C4(j,1),C4(j,2),[' ' num2str(j)]);

end

figure, imshow(I5);

figure, plot(C5(:,1),C5(:,2),'*');

for j=1:size(C5,1),

text(C5(j,1),C5(j,2),[' ' num2str(j)]);

end

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo0.cn 版权所有 湘ICP备2023017654号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务