車輛的檢測 跟蹤和計數

2021-08-06 04:39:03 字數 4108 閱讀 1525

#include

#include

#include

using

namespace cv;

#include

#include

using

namespace

std;

/*----定義滑鼠事件--畫矩形區域:作用當兩個車燈----*/

//第一步:全域性變數

bool drawing_box = false;

bool gotbox = false;

rect box;

point downpoint;

/*void mouserecthandler(int event, int x, int y, int flags, void *param)

break;

case cv_event_lbuttondown:

drawing_box = true;

box = rect(x, y, 0, 0);

break;

case cv_event_lbuttonup:

drawing_box = false;

gotbox = true;

if (box.width < 0)

if( box.height < 0 )

break;

default:

break;}}

*/void mouserecthandler(int event, int x, int y, int flags, void *param)

//滑鼠的移動到downpoint的右上角

if( x >= downpoint.x && y <= downpoint.y)

//滑鼠的移動到downpoint的左上角

if( x <= downpoint.x && y <= downpoint.y)

//滑鼠的移動到downpoint的左下角

if( x <= downpoint.x && y >= downpoint.y)

} break;

case cv_event_lbuttondown:

//按下滑鼠,代表可以可以開始畫矩形

drawing_box = true;

//記錄起點

downpoint = point(x,y);

break;

case cv_event_lbuttonup:

//鬆開滑鼠,代表結束畫矩形

drawing_box = false;

gotbox = true;

break;

default:

break;

} }

int main(int argc,char*argv)

//remove callback

setmousecallback("video",null,null);

for(;;)

return

0;

}

#include

using

namespace

std;

#include

#include

#include

#include

using

namespace cv;

//trackbar控制的變數及該值的最大值

int thresh = 200;

const

int max_thresh = 255;

//trackbar控制的函式

void thresh_callback(int,void*);

int w_h = 25;

const

int max_w_h = 100;

int vehiclefrequency = 2;//這個值不能過於大,否則拍到的車輛裡,沒有車牌

const

int max_vehiclefrequency = 15;

//全域性標量

mat grayframe;//顏色空間轉化

mat binaryframe;//二值化

mat kernel;//進行形態學處理的核函式

rect box_vehicle;

bool vehiclebool;//判斷是否有車輛

//儲存邊緣

vector

> contours;

vector

hierarchy;

rect vehiclerect;//通過前燈檢測,定位出來的車輛

/*----定義滑鼠事件--畫矩形區域:作用當兩個車燈----*/

//第一步:全域性變數

bool drawing_box = false;

bool gotbox = false;

rect box;

point downpoint;

void mouserecthandler(int event, int x, int y, int flags, void *param)

//滑鼠的移動到downpoint的右上角

if( x >= downpoint.x && y <= downpoint.y)

//滑鼠的移動到downpoint的左上角

if( x <= downpoint.x && y <= downpoint.y)

//滑鼠的移動到downpoint的左下角

if( x <= downpoint.x && y >= downpoint.y)

}break;

case cv_event_lbuttondown:

//按下滑鼠,代表可以可以開始畫矩形

drawing_box = true;

//記錄起點

downpoint = point(x,y);

break;

case cv_event_lbuttonup:

//鬆開滑鼠,代表結束畫矩形

drawing_box = false;

gotbox = true;

break;

default:

break;

}}int main()

rectangle(frame,box,scalar(255,0,0),2);//畫出感興趣區域

imshow("video",frame);

if(waitkey(33) == 'q')

break;

}//用於跟蹤演算法和計數

bool currentvehiclebool = false;

bool previousvehiclebool = false;

int numbervehicle = 0;

//用於記錄每一輛車的的幀數,避免是一閃而過的,可能不是車輛

int numbervehicleframe = 1;

int currentvehiclenumber = 0;

int previousvehiclenumber = 0;

//以上畫出感興趣區域,以下可以判斷是否為車輛

for(;;)

//用於計數,該輛車出現的幀數

if( currentvehiclenumber == previousvehiclenumber )}}

else

//記錄上一幀的是否有車輛

previousvehiclebool = currentvehiclebool;

//記錄上一幀,車輛的標號(number)

previousvehiclenumber = currentvehiclenumber;

//顯示

imshow("video",frame);

cout

break;

}return0;}

void thresh_callback(int,void*)//得到新的vehiclerect

//得到整個汽車前燈輪廓

for(int index = 0;index < boundrect.size() ;index++)

}

目標檢測和跟蹤小結

目標檢測即為從序列影象中將變化區域從背景影象中提取出來。運動目標檢測的演算法依照目標與攝像機之間的關係可以分為靜態背景下運動檢測和動態背景下運動檢測。1.靜態背景 2.動態背景 需要進行影象的全域性運動估計與補償 運動目標跟蹤就是在一段序列影象中的每幅影象中實時地找到所感興趣的運動目標 包括位置 速...

opencv人臉檢測和跟蹤

一 我的分類訓練器的xml檔案路徑如下 d opencv sources data haarcascades haarcascade frontalface alt.xml 每個人的安裝位置不同,訓練器的xml也不同,我的是opencv3.0的。二 先檢測一張如里的人臉 程式 include inc...

基於OpenCV的車輛檢測與追蹤的實現

一 安裝opencv和搭建環境 這裡就貼出幾個教程 安裝教程 關於其他opencv版本的安裝教程網路上也有很多,這裡就只貼出這兩個。二 關於opencv的介紹 我看還是把網路上關於這方面好的教程給大家貼出來吧,我怕講不好的話招笑話 首先是淺墨大神的系列部落格,我基本上就是從大神的部落格中學習到的,還...