简单图像中 监测图像物品位置

 

 


function [] = test() clc; clear; % Path="C:\Users\Administrator\Desktop\塑料瓶\0.jpg";%图片路径 % Path="C:\Users\Administrator\Desktop\塑料瓶\纯透明塑料瓶.jpg";%图片路径 % Path="C:\Users\Administrator\Desktop\塑料瓶\深色塑料瓶.jpg";%图片路径 % Path="C:\Users\Administrator\Desktop\塑料瓶\透明蓝色塑料瓶.jpg";%图片路径 Path="C:\Users\Administrator\Desktop\塑料瓶\透明绿色塑料瓶.jpg";%图片路径 Source_img=imread(Path);%获取图片信息 % figure(6);imshow(Source_img); [r,lie]=size(Source_img(:,:,2));%获取行列信息 bottle_bottom_XY=[-1,-1]; bottle_top=-1; bottle_bottom=-1; bottle_left=-1; bottle_right=-1; num=9; !算出指定位点与标志行列的差别 判断该点是否在瓶子图片内 X_D=round(r/num);%round四舍五入取整函数 算出行取值的等差 Y_D=round(lie/num);%round四舍五入取整函数 算出列取值的等差 X_label=(1:X_D:r); Y_label=(1:Y_D:lie); !RGB->LAB S_lab=zeros(r,lie,3); cform = makecform('srgb2lab'); lab = applycform(Source_img, cform); l=lab(:,:,1); a=lab(:,:,2); b=lab(:,:,3); % figure(1),imshow(l); % figure(1),imshow(a); % figure(1),imshow(b); for i=X_label for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; % ii=lab(i,j,1)*lab(i,j,1); % jj=lab(i,j,2)*lab(i,j,2); % kk=lab(i,j,3)*lab(i,j,3); % if(ii+jj+kk>100) if(lab(i,j,1)>100) % S_lab(i,j,1)=lab(i,j,1); % S_lab(i,j,2)=lab(i,j,2) % S_lab(i,j,3)=lab(i,j,3); [i,j] lab(i,j,1) end end end % figure(5);imshow(S_lab); A_R=zeros(num,num); A_G=zeros(num,num); A_B=zeros(num,num); for i=X_label for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; A_R(ti,tj)=Source_img(i,j,1); A_G(ti,tj)=Source_img(i,j,2); A_B(ti,tj)=Source_img(i,j,3); end end S_R=zeros(num,num); S_G=zeros(num,num); S_B=zeros(num,num); for i=X_label for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; S_R(ti,tj)=(A_R(1,tj)-A_R(ti,tj))*(A_R(1,tj)-A_R(ti,tj)) + (A_R(ti,1)-A_R(ti,tj))*(A_R(ti,1)-A_R(ti,tj)); S_G(ti,tj)=(A_G(1,tj)-A_G(ti,tj))*(A_G(1,tj)-A_G(ti,tj)) + (A_G(ti,1)-A_G(ti,tj))*(A_G(ti,1)-A_G(ti,tj)); S_B(ti,tj)=(A_B(1,tj)-A_B(ti,tj))*(A_B(1,tj)-A_B(ti,tj)) + (A_B(ti,1)-A_B(ti,tj))*(A_B(ti,1)-A_B(ti,tj)); end end SS=zeros(num,num); for i=X_label for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; SS(ti,tj)=S_R(ti,tj)+S_G(ti,tj)+S_B(ti,tj); end end S_end=zeros(num,num); for i=X_label for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; if( SS(ti,tj)>250) S_end(ti,tj)=SS(ti,tj); else S_end(ti,tj)=0; end end end !判断瓶子顶部 flag=0; for i=X_label if(flag==1) break; end for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; if(S_end(ti,tj)>0) flag=1; bottle_top=ti; break; end end end % length(X_label) % X_label(end:-1:1) !判断瓶子底部 flag=0; for i=X_label(end:-1:1) if(flag==1) break; end for j=Y_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; if(S_end(ti,tj)>0) flag=1; % ti % S_end(ti-1,tj) % S_end(ti+1,tj) bottle_bottom=length(X_label)-ti+1; bottle_color=[Source_img(i,j,1),Source_img(i,j,2),Source_img(i,j,3)]; break; end end end !判断瓶子左边界部 flag=0; for j=Y_label if(flag==1) break; end for i=X_label tj=round(j/Y_D)+1; ti=round(i/X_D)+1; if(S_end(ti,tj)>0) flag=1; bottle_left=tj; break; end end end !判断瓶子右边界部 flag=0; for j=Y_label(end:-1:1) if(flag==1) break; end for i=X_label ti=round(i/X_D)+1; tj=round(j/Y_D)+1; if(S_end(ti,tj)>0) flag=1; bottle_right=tj; break; end end end % S_end % bottle_color; % bottle_top%11 % bottle_bottom%11 % bottle_left%41 % bottle_right%62

  

posted @ 2022-05-23 18:07  古路无行客  阅读(56)  评论(0)    收藏  举报