⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mouthextract.asv

📁 MATLAB 下的snake算法实现 原始实现方法 检测眼睛、嘴巴
💻 ASV
字号:
function mouthextract(pname)
pic=imread(pname);
[m,n]=size(pic);
figure,imshow(pic);
[x,y]=ginput(1);
h = ones(5,5) / 25;
pict = imfilter(pic,h,'replicate');
part1=zeros(40,120);
part=im2uint8(part1);
firstx=y-20;
firsty=x-60;
firstx=floor(firstx);
firsty=floor(firsty);
for i=1:40
    for j=1:120
        part(i,j)=pict(firstx+i-1,firsty+j-1);
    end
end
T=graythresh(part);
x=floor(x);
y=floor(y);
bin1=im2bw(pict,T);
bin=imcomplement(bin1);
% MN=[7,20];
% se = strel('rectangle',MN);
% bin = imclose(bin,se);
pic1=bwlabel(bin);
pic1=imfill(pic1,'holes');
figure,imshow(pic1);
H=regionprops(pic1,'PixelList','FilledImage');
a=length(H);
flag=0;
for i=1:a
    b=H(i).PixelList;
    c=length(b(:,1));
    for j=1:c
        x1=b(j,1);
        y1=b(j,2);
        if im2double(x-x1)==0 & im2double(y-y1)==0
           flag=1;
           count1=i; 
           break;
        end
    end
    if flag==1
        break;
    end 
end
figure,imshow(H(count1).FilledImage);
% % B=H(count1).FilledImage;
% plot(Bnd(:,2), Bnd(:,1));
se=[1,1,1;
    1,1,1;
    1,1,1];
I2=zeros(m,n);
e=H(count1).PixelList;
cnt=length(e);
for i=1:cnt
    I2(e(i,2),e(i,1))=1;
end
I=imfill(I2,'holes');
figure,imshow(I);
re1=zeros(m,n);
ct=0;
for i=2:m-1
    for j=2:n-1
        if(I(i,j)==1 & (I(i-1,j)==0 | I(i+1,j)==0 | I(i,j-1)==0 | I(i,j+1)==0))
            re1(i,j)=1;
            ct=ct+1;
        end
    end
end
figure,imshow(re1);
        
% Ie=imerode(I,se);
% re=I-Ie;
% figure,imshow(re);
%边界跟踪
% [M,ct]=trace(re,m,n);       
flag1=0;
for i=1:m
    for j=1:n
        if re1(i,j)==1
            flag1=1;
            break;
        end
    end
    if flag1==1
        break;
    end
end
M=zeros(ct,2);
ct1=1;
tmpx=i;
tmpy=j;
re1(i,j)=2;
M(1,1)=i;
M(1,2)=j;
while 1    
if re1(tmpx,tmpy+1)==1 | re1(tmpx,tmpy+1)==2
    re1(tmpx,tmpy)=0;
    tmpy=tmpy+1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx+1,tmpy+1)==1 | re1(tmpx+1,tmpy+1)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx+1;
    tmpy=tmpy+1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx+1,tmpy)==1 | re1(tmpx+1,tmpy)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx+1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx+1,tmpy-1)==1 | re1(tmpx+1,tmpy-1)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx+1;
    tmpy=tmpy-1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx,tmpy-1)==1 | re1(tmpx,tmpy-1)==2
    re1(tmpx,tmpy)=0;
    tmpy=tmpy-1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx-1,tmpy-1)==1 | re1(tmpx-1,tmpy-1)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx-1;
    tmpy=tmpy-1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx-1,tmpy)==1 | re1(tmpx-1,tmpy)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx-1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
if re1(tmpx-1,tmpy+1)==1 | re1(tmpx-1,tmpy+1)==2
    re1(tmpx,tmpy)=0;
    tmpx=tmpx-1;
    tmpy=tmpy+1;
    re1(i,j)=2;
    if re1(tmpx,tmpy)==2
        break;
    end
    ct1=ct1+1;
    M(ct1,1)=tmpx;
    M(ct1,2)=tmpy;
    continue;
end
end

mid=ct/20;
mid=floor(mid);
M1=zeros(20,2);
for j=1:20
    M1(j,1)=M((j-1)*mid+1,1);
    M1(j,2)=M((j-1)*mid+1,2);
end
pic1=im2double(pic);
[snake_pnts,e]=snake(M1,50,50,1,1,1,1,pic1);
figure, imshow(pic);
hold on
[m n] = size(snake_pnts);
tmp = zeros(m+1, n);
tmp(1:m, :) = snake_pnts;
tmp(m+1, :) = snake_pnts(1, :);
plot(tmp(:,2), tmp(:,1));
end



    

    
    
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -