資源簡介
基于Opencv的紅外運動目標識別與跟蹤,內附紅外演示與源代碼。
代碼片段和文件信息
#include?“cv.h“
#include?“highgui.h“
#include?
#include?
#include?
#include?
#include?
#include
#include?
//跟蹤參數
const?double?MHI_DURATION?=?0.5;//最大跟蹤時間
const?double?MAX_TIME_DELTA?=?0.5;
const?double?MIN_TIME_DELTA?=?0.05;
const?int?N?=?3;
const?int?CONTOUR_MAX_AERA?=?100;//矩形面積
IplImage?**buf?=?0;
int?last?=?0;
int?flag;
IplImage?*mhi?=?0;?//?MHI:?motion?history?image
CvConnectedComp?*cur_comp?min_comp;
CvConnectedComp?comp;
CvMemStorage?*storage;
CvPoint?pt[4];
//?img?–?輸入視頻幀
//?dst?–?檢測結果
void?update_mhi(IplImage*?img?IplImage*?dst?int?diff_threshold)
{
double?timestamp?=?clock()?/?100.;?//獲取當前時間
CvSize?size?=?cvSize(img->width?img->height);
int?i?idx1?idx2;
IplImage*?silh;
IplImage*?pyr?=?cvCreateImage(cvSize((size.width?&?-2)?/?2?(size.height?&?-2)?/?2)?8?1);
CvMemStorage?*stor;
CvSeq?*cont;
if?(!mhi?||?mhi->width?!=?size.width?||?mhi->height?!=?size.height)
{
if?(buf?==?0)
{
buf?=?(IplImage**)malloc(N*sizeof(buf[0]));//動態內存分配
memset(buf?0?N*sizeof(buf[0]));
}
for?(i?=?0;?i? {
cvReleaseImage(&buf[i]);
buf[i]?=?cvCreateImage(size?IPL_DEPTH_8U?1);
cvZero(buf[i]);
}
cvReleaseImage(&mhi);
mhi?=?cvCreateImage(size?IPL_DEPTH_32F?1);
cvZero(mhi);
}
cvCvtColor(img?buf[last]?CV_BGR2GRAY);?//rgb->gray
idx1?=?last;
idx2?=?(last?+?1)?%?N;
last?=?idx2;
//?做幀差
silh?=?buf[idx2];
cvAbsDiff(buf[idx1]?buf[idx2]?silh);?//兩幀差異
//?對差圖像做二值化
cvThreshold(silh?silh?30?255?CV_THRESH_BINARY);?//src(xy)>threshold?dst(xy)?=?max_value;?否則dst(xy)=0;
cvUpdateMotionHistory(silh?mhi?timestamp?MHI_DURATION);?//更新像素點的運動歷史
cvCvtScale(mhi?dst?255.?/?MHI_DURATION
(MHI_DURATION?-?timestamp)*255.?/?MHI_DURATION);//timestamp是時間戳;MHI_DURATION,獲得的是當前時間
cvCvtScale(mhi?dst?255.?/?MHI_DURATION?0);
//?中值濾波,消除小的噪聲
cvSmooth(dst?dst?CV_MEDIAN?3?0?0?0);
//?向下采樣,去掉噪聲
cvPyrDown(dst?pyr?7);
cvDilate(pyr?pyr?0?1);?//?做膨脹操作,消除目標的不連續空洞
cvPyrUp(pyr?dst?7);
//
//?下面的程序段用來找到輪廓
//
//?Create?dynamic?structure?and?sequence.
stor?=?cvCreateMemStorage(0);
cont?=?cvCreateSeq(CV_SEQ_ELTYPE_POINT?sizeof(CvSeq)?sizeof(CvPoint)?stor);
//?找到所有輪廓
cvFindContours(dst?stor?&cont?sizeof(CvContour)
CV_RETR_LIST?CV_CHAIN_APPROX_SIMPLE?cvPoint(0?0));
//?直接使用CONTOUR中的矩形來畫輪廓
for?(;?cont;?cont?=?cont->h_next)
{
flag?=?0;
CvRect?r?=?((CvContour*)cont)->rect;
if?(r.height?*?r.width?>?CONTOUR_MAX_AERA)
{
cvRectangle(img?cvPoint(r.x?r.y)
cvPoint(r.x?+?r.width?r.y?+?r.height)
CV_RGB(255?0?0)?1?CV_AA?0);
flag?=?1;
}
else
{
flag?=?0;
}
}
cvReleaseMemStorage(&stor);
cvReleaseImage(&pyr);
}
int?main(int?argc?char**?argv)
{
IplImage*?motion?=?0;
CvCapture*?capture?=?0;?//視頻獲取結構?
while?(1){
capture?=?cvCreateFileCapture(“D:\\IR_object\\IR3.avi“);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2015-06-05?10:32??紅外目標識別與跟蹤\
?????目錄???????????0??2015-06-05?10:31??紅外目標識別與跟蹤\IR\
?????目錄???????????0??2015-06-05?10:31??紅外目標識別與跟蹤\IR\Debug\
?????文件???????67584??2015-05-25?17:12??紅外目標識別與跟蹤\IR\Debug\IR.exe
?????文件??????676676??2015-05-25?17:12??紅外目標識別與跟蹤\IR\Debug\IR.ilk
?????文件?????2027520??2015-05-25?17:12??紅外目標識別與跟蹤\IR\Debug\IR.pdb
?????目錄???????????0??2015-06-05?10:31??紅外目標識別與跟蹤\IR\IR\
?????目錄???????????0??2015-06-05?10:31??紅外目標識別與跟蹤\IR\IR\Debug\
?????文件????????1423??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.log
?????文件??????211923??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.obj
?????目錄???????????0??2015-06-05?10:31??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\
?????文件???????35898??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\CL.read.1.tlog
?????文件?????????268??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\CL.write.1.tlog
?????文件?????????149??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\IR.lastbuildstate
?????文件?????????434??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\cl.command.1.tlog
?????文件????????1464??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\li
?????文件????????3984??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\li
?????文件?????????250??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\IR.tlog\li
?????文件?????1108992??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\vc120.idb
?????文件?????1339392??2015-05-25?17:12??紅外目標識別與跟蹤\IR\IR\Debug\vc120.pdb
?????文件????????4331??2015-05-25?17:11??紅外目標識別與跟蹤\IR\IR\IR.cpp
?????文件????????4793??2015-05-25?16:40??紅外目標識別與跟蹤\IR\IR\IR.vcxproj
?????文件?????????943??2015-05-25?16:40??紅外目標識別與跟蹤\IR\IR\IR.vcxproj.filters
?????文件????40173568??2015-06-04?18:23??紅外目標識別與跟蹤\IR\IR.sdf
?????文件?????????952??2015-05-25?16:37??紅外目標識別與跟蹤\IR\IR.sln
?????文件???????15360??2015-06-04?18:23??紅外目標識別與跟蹤\IR\IR.v12.suo
?????文件?????1675958??2012-10-31?11:05??紅外目標識別與跟蹤\IR.avi
?????文件??????337142??2012-10-31?11:05??紅外目標識別與跟蹤\IR3.avi
?????文件???????17408??2015-05-25?16:35??紅外目標識別與跟蹤\ms.v12.suo
評論
共有 條評論