資源簡介
matlab 基于視頻的車流量統計源碼附帶需要vision 工具箱

代碼片段和文件信息
%?創建系統對象,用于讀入待處理視頻?
filename?=?‘viptraffic.avi‘;
hvfr?=?vision.VideoFileReader(filename?‘ImageColorSpace‘?‘RGB‘);
%?創建系統對象,用于色彩空間轉換
hcsc?=?vision.ColorSpaceConverter(‘Conversion‘?‘RGB?to?intensity‘);
%?創建系統對象,用于用高斯混合模型檢測背景
hfdet?=?vision.ForegroundDetector(...
????????‘NumTrainingframes‘?5?...?????%?取連續五幀進行檢測背景
????????‘InitialVariance‘?(30/255)^2);???%?初始標準差為?30/255
%?創建系統對象,用于檢測出包含汽車運動的圖像塊
hblob?=?vision.BlobAnalysis(?...
????????????????????‘CentroidOutputPort‘?false?...
????????????????????‘AreaOutputPort‘?true?...
????????????????????‘BoundingBoxOutputPort‘?true?...
????????????????????‘OutputDataType‘?‘single‘?...
????????????????????‘MinimumBlobArea‘?250?...
????????????????????‘MaximumBlobArea‘?3600?...
????????????????????‘MaximumCount‘?80);
%?創建系統對象,用于對檢測出的運動車輛進行框畫
hshapeins?=?vision.ShapeInserter(?...
????????????‘BorderColor‘?‘Custom‘?...
????????????‘CustomBorderColor‘?[0?255?0]);
%?創建系統對象,用于標注檢測到車輛的個數
htextins?=?vision.TextInserter(?...
????????‘Text‘?‘%4d‘?...
????????‘Location‘??[1?1]?...
????????‘Color‘?[255?255?255]?...
????????‘FontSize‘?12);
%?創建系統對象,用于顯示結果
sz?=?get(0‘ScreenSize‘);
pos?=?[20?sz(4)-300?200?200];
hVideoOrig?=?vision.VideoPlayer(‘Name‘?‘Original‘?‘Position‘?pos);
pos(1)?=?pos(1)+220;??%在右側建立下一個視窗
hVideoFg?=?vision.VideoPlayer(‘Name‘?‘Foreground‘?‘Position‘?pos);
pos(1)?=?pos(1)+220;
hVideoRes?=?vision.VideoPlayer(‘Name‘?‘Results‘?‘Position‘?pos);
line_row?=?23;?%?定義感興趣區域(ROI)
%?以下的程序段為對輸入的視頻圖像進行處理
while?~isDone(hvfr)
????image?=?step(hvfr);??????%?讀入視頻的每一幀
????y?=?step(hcsc?image);????%?將彩色圖像轉換成灰度圖像
%?采用自動白平衡算法去除灰度突變
y?=?y-mean(y(:));
????fg_image?=?step(hfdet?y);?%?檢驗背景
????%?采用數學形態學,在前景圖像中檢測變化的聯通圖像塊區域的面積
????[area?bbox]?=?step(hblob?fg_image);
????image_out?=?image;
????image_out(22:23::)?=?255;??%?僅對經過白線后的車輛進行計數
????image_out(1:151:30:)?=?0;??%?將背景置為黑色
????Idx?=?bbox(:2)?>?line_row;????%選擇感興趣區域.
???%?當在感興趣區域中,聯通圖像塊的比例占整個變化區域的40%以上時,便認為是車輛
????ratio?=?zeros(length(Idx)1);
????ratio(Idx)?=?single(area(Idx1))./single(bbox(Idx3).*bbox(Idx4));
????ratiob?=?ratio?>?0.4;
????count?=?int32(sum(ratiob));????%?車輛的數量
????bbox(~ratiob:)?=?int32(-1);
????%?將檢測出的車輛圈畫出來
????image_out?=?step(hshapeins?image_out?bbox);
????%?顯示檢測汽車的數量
????image_out?=?step(htextins?image_out?count);
????step(hVideoOrig?image);??????????%?原始視頻
????step(hVideoFg???fg_image);???????%?前景
????step(hVideoRes??image_out);??????%?用方框勾畫檢測出的車輛
end
%關閉視頻文件
release(hvfr);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-03-17?11:11??基于視頻的車流量統計\
?????文件????????2914??2018-05-14?23:00??基于視頻的車流量統計\example6.m
?????文件??????208896??2005-04-04?13:17??基于視頻的車流量統計\viptraffic.avi
?????文件??????????35??2015-03-17?11:12??基于視頻的車流量統計\代碼說明.txt
評論
共有 條評論