|
汽车零部件采购、销售通信录 填写你的培训需求,我们帮你找 招募汽车专业培训老师
clear all
%z增加视频的录入并转化为可用的bmp图片文件
figure(1)
%读懂程序改为能够统计自己采集视频中车辆的数目
top_x=[8,97,174,246,280];
top_y=[134,141,144,147,150];
rect.lx=0;
rect.ly=59;
rect.width = 280;
rect.height=103;
host_num=0;
host_regions=zeros(1,13);
global v_order;
v_order=1;
esm_num=0;
esm_regions=zeros(1,6);
%c_match=zeros(1,4);
%c_center=zeros(1,12);
dt=0.12;
BWG=imread('1.bmp');
figure(2);
imshow(BWG);
j=0;
for i=1:1:120
open_filename='';
o=num2str(i);
open_filename=strcat(open_filename,o,'.bmp');
CBW=imread(open_filename);
figure(3);
subplot(221);
imshow(CBW);hold on;
% size(CBW) 120 160 3
SBW=abs(double(CBW)-double(BWG));
SBW=uint8(SBW);
BW2=im2bw(SBW,0.15);
MBW=medfilt2(BW2);
subplot(222);
imshow(MBW);hold on;
%%%%jsx增加一个小区域
%%%%根据自己车道的位置来调整
%修改点1
lefttopx=96;
lefttopy=160;
rightbottomx=120;
rightbottomy=96;
smallarea=MBW(lefttopx:rightbottomx,lefttopy:rightbottomy);
subplot(223);
imshow(smallarea); hold on;
%j=(i-997)/3;
j=i;
smallareavalue(j,:,:)=smallarea; %上述数据可以用来判别是否有车辆通过
subplot(222)
%正好与画像素图的x和y相反
A=[lefttopy,lefttopy,rightbottomy,rightbottomy,lefttopy;lefttopx,rightbottomx,rightbottomx,lefttopx,lefttopx];
plot(A(1,:),A(2,:),'m');
%axis ([0 14 0 14])
%%%%jsx end
BWG=(0.01*double(CBW)+0.99*double(BWG)).*double(MBW)+(0.1*double(CBW)+0.9*double(BWG)).*double(~MBW);
BWG=uint8(BWG);
subplot(224)
imshow(BWG);
% %{
delete=zeros(host_num,1);
count=0;
for k=1:host_num
if host_regions(k,7)<20
delete(k)=1;
count=count+1;
end
end
temp_host=zeros(1,13);
index=1;
for k=1:host_num
if delete(k)==0
temp_host(index,:)=host_regions(k,:);
index=index+1;
end
end
host_regions=temp_host;
host_num=host_num-count;
% c_match=zeros(1,4);
% c_num=0;
if host_num==0
init_num=0;
init_center=zeros(1,2);
% c_num=0;c_match=zeros(1,4);
[c_num,c_match]=Zcluster(init_num,init_center,MBW,rect);
else
for k=1:host_num
init_center(k,1)=host_regions(k,7);
init_center(k,2)=host_regions(k,8);
end
init_num=host_num;
% c_num=0;c_match=zeros(1,4);
[c_num,c_match]=Zcluster(init_num,init_center,MBW,rect);
end
%{
for j=1:c_num
hold on
figure(3);
plot(c_match(j,3),c_match(j,4),'g.')
end
%}
point=j;
measured=zeros(c_num,1);
if c_num~=0
if c_num>host_num
for k=1:c_num
if c_match(k,1)==0&&c_match(k,2)==0
% if c_match(k,3)<20
% measured(k)=1;
% else
if abs(c_match(k,3)-270)<30
% if esm_num==0
esm_regions(1,1)=c_match(k,3);
esm_regions(1,2)=c_match(k,4);
esm_regions(1,3)=0;
esm_regions(1,4)=0;
esm_regions(1,5)=0;
esm_regions(1,6)=0;
% esm_num=esm_num+1;
measured(k)=1;
%{
else if esm_num==1
esm_regions(1,3)=(c_match(k,3)-esm_regions(1,1))/dt;
esm_regions(1,4)=(c_match(k,4)-esm_regions(1,2))/dt;
esm_regions(1,1)=c_match(k,3);
esm_regions(1,2)=c_match(k,4);
esm_regions(1,5)=0;
esm_regions(1,6)=0;
esm_num=esm_num+1;
measured(k)=1;
else
temp_vx=esm_regions(1,3);
temp_vy= esm_regions(1,4);
esm_regions(1,3)=(c_match(k,3)-esm_regions(1,1))/dt;
esm_regions(1,4)=(c_match(k,4)-esm_regions(1,2))/dt;
esm_regions(1,5)=(esm_regions(1,3)-temp_vx)/dt;
esm_regions(1,6)=(esm_regions(1,4)-temp_vy)/dt;
esm_regions(1,1)=c_match(k,3);
esm_regions(1,2)=c_match(k,4);
measured(k)=1;
end
end
%}
end
rem_host=kkghr(esm_regions,v_order);
host_num=host_num+1;
host_regions(host_num,:)=rem_host;
v_order=v_order+1;
% hold on
% plot(host_regions(host_num,1),host_regions(host_num,2),'g.')
end
esm_regions=zeros(1,6);
esm_num=0;
end
end
end
%{
for j=1:c_num
hold on
plot(host_regions(j,1),host_regions(j,2),'g.')
end
for j=1:c_num
hold on
plot(c_match(j,3),c_match(j,4),'g.')
end
%}
host_regions=kkghr2(c_match,measured,host_regions);
% hold on
% plot(host_regions(1,1),host_regions(1,2),'g.')
%{
for j=1:c_num
hold on
plot(c_match(j,3),c_match(j,4),'g.')
end
%}
for j=1:c_num
hold on;
plot(host_regions(j,1),host_regions(j,2),'g.') ;
end
for j=1:c_num
plot(host_regions(j,7),host_regions(j,8),'r.');
end
pause(0.01);
end
%%%%jsx add 实现车辆计数
for l=1:size(smallareavalue,1)
pxelnum=0;
for m=1:size(smallareavalue,2)
for n=1:size(smallareavalue,3)
if smallareavalue(l,m,n)==1
pxelnum=pxelnum+1;
end
end
end
flag(l)=pxelnum;
end
figure(4);
plot( flag,'-o');
title('这个值可以用来确定车辆数目;车型等;标定后可以求车速...');
carnum=0;
for i=1:size(flag,2)-1
if flag(1,i)==0 && flag(1,i+1)>0
carnum=carnum+1;
end
end
carnum
%%%%end |
|