資源簡介
基于OpenCV2.4.9的視頻車輛識別與車輛計數,在Visual Studio
2010上開發(fā)。多多指教。
代碼片段和文件信息
#include?“opencv2/opencv.hpp“
#include
using?namespace?std;
using?namespace?cv;
int?CarNum?=?0;//初始化車的數量為0
Mat?MoveDetect(Mat?frame1?Mat?frame2){//移動監(jiān)測
Mat?result?=?frame2.clone();//克隆frame2
Mat?gray1?gray2;
cvtColor(frame1?gray1?CV_BGR2GRAY);//顏色空間轉換將frame1圖像轉換為灰度圖
cvtColor(frame2?gray2?CV_BGR2GRAY);//顏色空間轉換將frame2圖像轉換為灰度圖
Mat?diff;
absdiff(gray1?gray2?diff);//幀差法,計算兩個數組差的絕對值的函數。
imshow(“幀差法顯示“?diff);
//threshold(diff?diff?45?255?CV_THRESH_BINARY);?//簡單二值化
adaptiveThreshold(diff?diff?255?ADAPTIVE_THRESH_GAUSSIAN_C?THRESH_BINARY_INV?15?3);//自適應閾值化
imshow(“二值化顯示“?diff);
//圖像的二值化就是將圖像上的像素點的灰度值設置為0或255,
//這樣將使整個圖像呈現出明顯的黑白效果。在數字圖像處理中,
//二值圖像占有非常重要的地位,圖像的二值化使圖像中數據量大為減少,
//從而能凸顯出目標的輪廓。OpenCV中提供了函數cv::threshold();
?
Mat?element?=?getStructuringElement(MORPH_RECT?Size(3?3));//返回矩形的結構元素,核大小為3*3
Mat?element2?=?getStructuringElement(MORPH_RECT?Size(31?31));//返回矩形的結構元素,核大小為31*31
//進行形態(tài)學操作
morphologyEx(diff?diff?MORPH_OPEN?element);//輸入為diff,輸出為diff,開運算的形態(tài)學操作
medianBlur(diff?diff?9);//中值濾波
dilate(diff?diff?element2);//膨脹操作
imshow(“膨脹窗口“?diff);//圖像膨脹
vector>?contours;??
vector?hierarcy;
findContours(diff?contours?hierarcy?CV_RETR_EXTERNAL?CHAIN_APPROX_NONE);?//查找輪廓
vector>?contours_poly(contours.size());
vector?boundRect(contours.size());?//定義外接矩形集合
//drawContours(img2?contours?-1?Scalar(0?0?255)?1?8);??//繪制輪廓
int?x0=0?y0=0?w0=0?h0=0;
char?Num[10];
for(int?i=0;?i approxPolyDP(Mat(contours[i])?contours_poly[i]?3?true);?//多邊形逼近
boundRect[i]?=?boundingRect((Mat)contours_poly[i]);?//查找每個輪廓的外接矩形
if(boundRect[i].width?>?55?&&?boundRect[i].width?<180?&&?boundRect[i].height?>?55?&&?boundRect[i].height?180){
x0?=?boundRect[i].x;??//獲得第i個外接矩形的左上角的x坐標
y0?=?boundRect[i].y;?//獲得第i個外接矩形的左上角的y坐標
w0?=?boundRect[i].width;?//獲得第i個外接矩形的寬度
h0?=?boundRect[i].height;?//獲得第i個外接矩形的高度
rectangle(result?Point(x0?y0)?Point(x0+w0?y0+h0)?Scalar(0?255?0)?2?8);?//繪制第i個外接矩形
}
if((y0?+?h0/2?+?1)?>=?138?&&?(y0?+?h0/2?-?1)?<=?142){
CarNum++;
}
line(result?Point(0140)?Point(568140)?Scalar(00255)?1?8);
Point?org(035);
sprintf(Num?“CarNum=%d“?CarNum);
putText(result?Num?org?CV_FONT_HERSHEY_SIMPLEX?0.8f?CV_RGB(02550)?2);
}
return?result;
}
void?main(){ ?
VideoCapture?cap(“car.avi“);//打開一個視頻文件
????if(!cap.isOpened())?//檢查打開是否成功
?????????return;
????Mat?frame;
Mat?result;?
Mat?temp;
int?count=0;
????while(1){
????????cap>>frame;
????????if(!frame.empty()){//frame不為空
count++;
if(count==1)
?result?=?MoveDetect(frame?frame);
else
?result?=?MoveDetect(temp?frame);
imshow(“原圖“?frame);//原圖
imshow(“結果“?result);//結果圖
temp?=?frame.clone();
????????????if(waitKey(5)==27)//判斷用戶的操作,若用戶在5ms時間段中按下ESC則跳出循環(huán)
???????????????break;
????????}
????????else
????????????break;
????}
????cap.release();??
waitKey(0);
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-04-10?11:20??vehicle?detection\
?????目錄???????????0??2018-04-10?11:19??vehicle?detection\Debug\
?????文件??????115200??2018-04-09?11:02??vehicle?detection\Debug\visual1111.exe
?????文件?????1202084??2018-04-09?11:02??vehicle?detection\Debug\visual1111.ilk
?????文件?????2026496??2018-04-09?11:02??vehicle?detection\Debug\visual1111.pdb
?????目錄???????????0??2018-04-10?11:19??vehicle?detection\ipch\
?????目錄???????????0??2018-04-10?11:19??vehicle?detection\ipch\visual1111-27eaa1c8\
?????文件????56688640??2018-04-09?16:23??vehicle?detection\ipch\visual1111-27eaa1c8\visual1111-43b96364.ipch
?????目錄???????????0??2018-04-10?11:19??vehicle?detection\visual1111\
?????目錄???????????0??2018-04-10?11:19??vehicle?detection\visual1111\Debug\
?????文件???????37488??2018-04-09?11:02??vehicle?detection\visual1111\Debug\CL.read.1.tlog
?????文件????????1126??2018-04-09?11:02??vehicle?detection\visual1111\Debug\CL.write.1.tlog
?????文件??????336347??2018-04-07?11:18??vehicle?detection\visual1111\Debug\bike.obj
?????文件??????523093??2018-04-09?11:02??vehicle?detection\visual1111\Debug\car.obj
?????文件????????2026??2018-04-09?11:02??vehicle?detection\visual1111\Debug\cl.command.1.tlog
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件???????????2??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件????????2396??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件????????5312??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件?????????844??2018-04-09?11:02??vehicle?detection\visual1111\Debug\li
?????文件??????522537??2018-04-07?11:16??vehicle?detection\visual1111\Debug\main.obj
?????文件?????????416??2018-04-09?11:02??vehicle?detection\visual1111\Debug\mt.command.1.tlog
?????文件?????????358??2018-04-09?11:02??vehicle?detection\visual1111\Debug\mt.read.1.tlog
............此處省略22個文件信息
- 上一篇:LS-DYNA K文件幫助手冊
- 下一篇:深思4加密狗徹底研究
評論
共有 條評論