function TYPE=shibie(str)
TYPE=0;
im=imread(str);
im=rgb2gray(im);
f=im(20:550,300:700);
f=double(f);
h=ones(3)/9;
g=double(f);
for i=1:15
g=conv2(g,h,'same');
end
G=edge(g,'canny',0.14);
%imshow(g,[]);
%imshow(G,[]);
[M,N]=size(G);
G=G(10:M-10,10:N-10);
%imshow(G,[]);
%judge if it's type NO. 1
[H,theta,rho] = hough(G,'RhoResolution',0.5,'ThetaResolution',0.5);
P = houghpeaks(H,2,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(G,theta,rho,P,'FillGap',55,'MinLength',50);
figure, imshow(g,[]), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
end
hold off
if abs(abs(lines(1).theta)-90)<20 && abs(abs(lines(2).theta)-90)<20 && abs(lines(1).point1(2)-lines(2).point1(2))>100
TYPE=1;
else
%judge if it's type NO.2
f=im(20:550,20:400);
f=double(f);
h=ones(3)/9;
g=double(f);
for i=1:15
g=conv2(g,h,'same');
end
G=edge(g,'canny',0.13);
%imshow(g,[]);
%imshow(G,[]);
%[M,N]=size(G);
G=G(20:500,10:300);
g=g(20:500,10:300);
%clear figure
%%%%%%%%%%%%%
%ellipses = houghellipse( G,1,[3,3] );
f=G;numpeaks=1;nhood=[3,3];
[M,N]=size(f);
fcent=zeros(M,N);
ellipses=[];
[x,y]=find(f);
%x=x-1;y=y-1;
for i=1:length(x)-1
for k=(i+1):length(x)
rcenter=round((x(k)+x(i))/2);
ccenter=round((y(k)+y(i))/2);
fcent(rcenter,ccenter)=fcent(rcenter,ccenter)+1;
end
end
done=false;
r=[];c=[];%r、c分别表示可能的椭圆中心的行列指标
kpeaks=0;
fcnew=zeros(M,N);
threshold=0.5*max(fcent(:));%threshold can be modified
while ~done
[p,q]=find(fcent==max(fcent(:)));
p=p(1);q=q(1);
if fcent(p,q)>=threshold
r(end+1)=p;c(end+1)=q;
fcnew(p,q)=fcent(p,q);
kpeaks=kpeaks+1;
p1=max(1,p-(nhood(1)-1)/2);p2=min(size(fcent,1),p+(nhood(1)-1)/2);
q1=max(1,q-(nhood(2)-1)/2);q2=min(size(fcent,2),q+(nhood(2)-1)/2);
fcent(p1:p2,q1:q2)=0;
else
done=true;
end
if kpeaks==numpeaks
done=true;
end
end
%下面应根据找到的可能椭圆中心进一步找出图中的椭圆(弧)
alpha=linspace(-10*pi/180,10*pi/180,11);
D=0.5*sqrt(M^2+N^2);
a=linspace(30,D,100);
b=linspace(30,D,100);
for i=1:length(r)
X=zeros(11,100,100);
x1=x-r(i);y1=y-c(i);
for j=i:length(alpha)
x2=x1*cos(alpha(j))+y1*sin(alpha(j));
y2=y1*cos(alpha(j))-x1*sin(alpha(j));
for k=1:length(a)
for l=1:length(b)
temp=abs((x2/a(k)).^2+(y2/b(l)).^2-1);
X(j,k,l)=X(j,k,l)+length(find(temp<0.025));
end
end
end
% kpeaks=0;
% numellipse=1;%number of ellipses wanted
threshold1=max(50,0.5*max(X(:)));%low limit that can be recognised as an ellipse
% Alpha=[];A=[];B=[];
% while ~done
[m,ind]=max(X(1:end));;
al=mod(ind,11);
if al==0
al=11;
end
lo=ceil(mod(ind,1100)/11);
if lo==0
lo=100;
end
sh=ceil(ind/1100);
if sh==0
sh==100;
end
% if X(al,lo,sh)>=threshold1
% Alpha(end+1)=al;A(end+1)=lo;B(end+1)=sh;
% kpeaks=kpeaks+1;
% al1=max(1,al-(nhood(1)-1)/2);al2=min(size(X,1),al+(nhood(1)-1)/2);
% lo1=max(1,lo-(nhood(2)-1)/2);lo2=min(size(X,2),lo+(nhood(2)-1)/2);
% sh1=max(1,sh-(nhood(2)-1)/2);sh2=min(size(X,3),sh+(nhood(2)-1)/2);
% fcent(al1:al2,lo1:lo2,sh1:sh2)=0;
% else
% done=true;
% end
% if kpeaks==numellipse
% done=true;
% end
% end
if X(al,lo,sh)>=threshold1
ellipses(end+1,:)=[r(i),c(i),alpha(al),a(lo),b(sh)];
end
end
%%%%%%%%%%%%%%%%
if ~isempty(ellipses)
if ellipses(1,4)>100 && ellipses(1,5)>90
TYPE=2;
imshow(g,[]);
hold on
plot(ellipses(1,2),ellipses(1,1),'r+');
theta=linspace(0,pi,181);
plot(ellipses(1,5)*sin(theta)+ellipses(1,2),ellipses(1,4)*cos(theta)+ellipses(1,1),...
'LineWidth',2,'color','r');
hold off
end
end
end
end