# 利用MATLAB进行车辆的速度测量

V=L(n1n2)×t$V=\frac{L}{(n_{1}-n_{2})\times t}$

function [jiludian,rect,speed]=gengzhong(shizheng,rect)
number=shizheng;
name=strcat('sy',num2str(number),'.jpg');
number=number+1;
name=strcat('sy',num2str(number),'.jpg');
jiludian=zeros(100,2);
k=1;
zhic1=src2-src1;
zhic1=im2bw(zhic1,0.1);

function [y,x]=zhixin1(zhic1,rect)
u=0;
v=0;
zuobiaox=0;
zuobiaoy=0;
number=0;
jilux=0;
jiluy=0;
zhongxinx=rect(1)+rect(3)/2;
zhongxiny=rect(2)+rect(4)/2;
for i=zhongxiny-rect(4)/2:zhongxiny+rect(4)/2
for j=zhongxinx-rect(3)/2:zhongxinx+rect(3)/2
if(j<1920)
if(zhic1(i,j)==1)
number=number+1;
u=(int64(i)+int64(u));
v=(int64(j)+int64(v));
zuobiaox=zuobiaox+double(zhic1(i,j));
zuobiaoy=double(zhic1(i,j))+zuobiaoy;
jilux=int64(jilux)+int64(j);
jiluy=int64(jiluy)+int64(i);
end
end
end
end
y=int64(u/zuobiaoy);
x=int64(v/zuobiaox);

number=number+1;
name=strcat('sy',num2str(number),'.jpg');
src1=src2;
zhic2=src2-src1;
zhic2=im2bw(zhic2,0.1);
imshow(zhic2);
[y2,x2]=zhixin2(zhic2,rect);
jiludian(2,1)=y2;
jiludian(2,2)=x2;

function [y,x]=zhixin2(zhic1,rect)
u=0;
v=0;
zuobiaox=0;
zuobiaoy=0;
number=0;
jilux=0;
jiluy=0;
zhongxinx=rect(1)+rect(3)/2;
zhongxiny=rect(2)+rect(4)/2;
for i=zhongxiny-rect(4)/2:zhongxiny+rect(4)/2+40
for j=zhongxinx-rect(3)/2:zhongxinx+rect(3)/2+30
if(j<1920)
if(zhic1(i,j)==1)
number=number+1;
u=(int64(i)+int64(u));
v=(int64(j)+int64(v));
zuobiaox=zuobiaox+double(zhic1(i,j));
zuobiaoy=double(zhic1(i,j))+zuobiaoy;
jilux=int64(jilux)+int64(j);
jiluy=int64(jiluy)+int64(i);
end
end
end
end
y=int64(u/zuobiaoy);
x=int64(v/zuobiaox);

for b=number+1:number+40
yuandian(1,1)=y1;
yuandian(1,2)=x1;
yuandian(2,1)=y2;
yuandian(2,2)=x2;
x1=x2;
y1=y2;
name=strcat('sy',num2str(b),'.jpg');
src1=src2;
src3=src2;
zhic2=src2-src1;
zhic2=im2bw(zhic2,0.2);

houx=2*yuandian(2,2)-yuandian(1,2);
houy=2*yuandian(2,1)-yuandian(1,1);
[y2,x2]=zhixin3(zhic2,rect,houy,houx);
hold on;
[state,result]=draw_rect(src3,[x2-rect(3)/2,y2-rect(4)/2],[rect(3),rect(4)],0);
plot(x2,y2,'LineWidth',2,'Color','b');
imshow(result);
jiludian(k,1)=y2;
jiludian(k,2)=x2;
k=k+1;
if(y2>1016)
break;
end
pause(0.01)；

end
function [y,x]=zhixin3(zhic1,rect,houy,houx)
u=0;
v=0;
zuobiaox=0;
zuobiaoy=0;
number=0;
jilux=0;
jiluy=0;

for i=houy-rect(4)/2:houy+rect(4)/2
for j=houx-rect(3)/2:houx+rect(3)/2
if(j<1920&&i<1072&&j>0&&i>0)
if(zhic1(i,j)==1)
number=number+1;
u=(int64(i)+int64(u));
v=(int64(j)+int64(v));
zuobiaox=zuobiaox+double(zhic1(i,j));
zuobiaoy=double(zhic1(i,j))+zuobiaoy;
jilux=int64(jilux)+int64(j);
jiluy=int64(jiluy)+int64(i);
end
end
end
end
y=int64(u/zuobiaoy);
x=int64(v/zuobiaox)

zhengshu=b;
[kaishi,kaiwei]=zhidao_nearest(jiludian,760);
[jieshu,jiewei]=zhidao_nearest(jiludian,460);
speed=((jiewei-kaiwei)/270*10)/((jieshu-kaishi)*0.1);
function [x,y]=zhidao_nearest(A,b)
[Asort,index]=sort(abs(A(:)-b));
y=A(index(1));
x=find(A==y);

V=y1y2n×t×10270$V=\frac{y_{1}-y_{2}}{n_{差}\times{t}}\times {\frac{10}{270}}$