检测注射器的活塞或推柄是否丢失 1丢失推柄 丢失活塞 正常 matlab代码 % onezeros@yahoo.cn May 15,2011 % usage :detection 'imgname.jpg' function detection(imgname) img=imread(imgname); % detection of piston gray=rgb2gray(img); bw=im2bw(gray,60/255); bw=1-bw; figure;imshow(bw); bw1=bwareaopen(bw,600,8); figure;imshow(bw1); acc=sum(sum(bw1)); if acc >600 disp 'detection result of piston : normal' else disp 'detection result of piston : lost' end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % detection of handle % method: polygon analysis % step 1: prewitt edge detection on blue channel % step 2: erase small edge in order to erase all background points % step 3: center line % step 4: farthest two points from the center line to locate tube wing % step 5: calculate distance between wing and corresponding end to judge whether the handle exists % step 1: prewitt edge detection on blue channel blue=img(:,:,3); prewitt_blue=edge(blue,'prewitt'); figure;imshow(prewitt_blue) % step 2: erase small edge in order to erase all background points bw=bwareaopen(prewitt_blue,10,8);%assume this can erase all background points figure;imshow(bw) % step 3: center line % get positions of edge points [height,width]=size(bw); ptn=0; for h=1:height for w=1:width if bw(h,w)>0 ptn=ptn+1; x(ptn)=w; y(ptn)=h; end end end % fit center line p=polyfit(x,y,1); % draw the center line end1=[0,p(2)]; end2=[width,p(1)*width+p(2)]; line([end1(1),end2(1)],[end1(2),end2(2)],'Color',[1,153/255,0],'LineWidth',4) % step 4: farthest two points from the center line to locate tube wing A=p(1); B=-1; C=p(2); deno=sqrt(A^2+B^2); distance=(A*x+B*y+C)/deno; [dist_far,index_far]=max(distance); % step 5: calculate distance between wing and corresponding end to judge whether the handle exists % project points on the center line x_proj=(A*y+x-A*C)/(A^2+1); y_proj=A*x_proj+C; hold on plot(x(index_far),y(index_far),'ob','LineWidth',3,'MarkerSize',10); plot(x_proj(index_far),y_proj(index_far),'ob','LineWidth',3,'MarkerSize',10); % find farthest point on either side to wing [end1_x,index_end1]=min(x_proj); [end2_x,index_end2]=max(x_proj); % judgement for existance of handle d1=sqrt((x_proj(index_end1)-x_proj(index_far))^2+(y_proj(index_end1)-y_proj(index_far))^2); d2=sqrt((x_proj(index_end2)-x_proj(index_far))^2+(y_proj(index_end2)-y_proj(index_far))^2); % the nearer one is possible to be handle if d1<d2 handle=d1; ends_index=[index_end1,index_end2]; else handle=d2; ends_index=[index_end2,index_end1]; end plot(x_proj(ends_index(1)),y_proj(ends_index(1)),'sg','LineWidth',3,'MarkerSize',8);% possible handle end plot(x_proj(ends_index(2)),y_proj(ends_index(2)),'sy','LineWidth',3,'MarkerSize',8); % if possible handle distance(d1/d2) is bigger than wing distance (dist_far) if handle>dist_far% you can adjust this to get bigger accuracy disp 'detection result of handle : normal' % verify handle end by mark a red square plot(x_proj(ends_index(1)),y_proj(ends_index(1)),'sr','LineWidth',3,'MarkerSize',15); else disp 'detection result of handle : lost' end hold off clear