对视频中的车辆进行计数,MATLAB仿真
生活随笔
收集整理的這篇文章主要介紹了
对视频中的车辆进行计数,MATLAB仿真
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.仿真預覽
2.部分核心代碼
clc; close all; clear; folderPath = 'TrafficVideo\'; % 文件夾名稱 imageType = '*.jpg'; % 圖像類型 imageFiles = dir([folderPath, imageType]); % 指定路徑下所有指定圖像,以結構體形式保存 %% 通過循環遍歷所有圖像procNum = length(imageFiles); m=5; n=ceil((procNum-1)/4/m); Road = imread([folderPath,imageFiles(procNum).name]); GrayRoad = rgb2gray(Road); figure,imshow(Road); th=zeros(1,(length(imageFiles)-1)/4); se1=[1 1 1]; se2=ones(9); color=[1 0 0;0 1 0;0 0 1;1 0 1;0 1 1;1 1 0;1 1 1]; a=0; q=0; for z=1:(length(imageFiles)-1)/4:length(imageFiles)-1figure,%%%%%%%%%%%%%%%%%%%%%%%%%%求取閾值%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%for k = z:(length(imageFiles)-1)/4+z-1fileName = imageFiles(k).name; % 當前文件名,注意這里不含路徑信息I = rgb2gray(imread( [folderPath, fileName] ))- GrayRoad; % 將圖像數據讀入矩陣th(k-z+1)=graythresh(I);endth1=sum(th)/length(th)-0.045;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%for k = z:(length(imageFiles)-1)/4+z-1fileName = imageFiles(k).name; % 當前文件名,注意這里不含路徑信息I0 = imread([folderPath, fileName]);% J=midfilter(I);I = rgb2gray(I0)- GrayRoad; % 將圖像數據讀入矩陣% [mu,mask]=kmeans(J,2);% th=graythresh(I);% I=midfilter(I); %中值濾波I=im2bw(I,th1); %分割J=midfilter(I);I=imclose(J,se2); %閉運算I=imopen(I,se1); %開運算[Car,num,Rect] = CarProc(I); %標記、計數、求坐標subplot(m,n,k-z+1);imshow(I0,[]); title(num2str(k))midCar=Car;if k~=1for v=1:num[r,c]=size(Car);Car1=Car;for x=1:rfor y=1:cif Car1(x,y)~=vCar1(x,y)=0;endendendJ=Car1.*Car0;if J==0[r1,c1]=find(Car1~=0);a=a+1;for k1=1:length(r1)midCar(r1(k1),c1(k1))=a;endq=a;if (q>7)&&(q<=14)q=q-7;endif q>14q=q-14;endrectangle('Position',Rect(v,:),'EdgeColor',color(q,:),'LineWidth',1);text(Rect(v,1),Rect(v,2),num2str(a),'color','r');else[r1,c1]=find(Car1~=0);[c2,d2]=find(Car1.*Car0~=0);for k1=1:length(r1)midCar(r1(k1),c1(k1))=Car0(c2(1),d2(1));end g=Car0(c2(1),d2(1));if (Car0(c2(1),d2(1))>7)&&(Car0(c2(1),d2(1))<=14)g=Car0(c2(1),d2(1))-7;endif Car0(c2(1),d2(1))>14g=Car0(c2(1),d2(1))-14;endrectangle('Position',Rect(v,:),'EdgeColor',color(g,:),'LineWidth',1);text(Rect(v,1),Rect(v,2),num2str(Car0(c2(1),d2(1))),'color','r');endendelsefor v=1:numif num>0a=a+1;text(Rect(v,1),Rect(v,2),num2str(a),'color','r');rectangle('Position',Rect(v,:),'EdgeColor',color(q,:),'LineWidth',1);endendendCar0=midCar;end end % search(1); %圖像檢索C70
總結
以上是生活随笔為你收集整理的对视频中的车辆进行计数,MATLAB仿真的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python第三方库pygame的使用
- 下一篇: MATLAB如何在原图插入一个局部放大图