#include "cv.h"
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
// various tracking parameters (in seconds) //跟蹤的引數(單位為秒)
const double MHI_DURATION = 0.5;//0.5s為運動跟蹤的最大持續時間
const double MAX_TIME_DELTA = 0.5; //最大時間增量為0.5s
const double MIN_TIME_DELTA = 0.05; //最小時間增量0.05s
const int N = 3;
//
const int CONTOUR_MAX_AERA = 16;
// ring image buffer 圈出影像緩沖
IplImage **buf = 0;//指標的指標
int last = 0;
// temporary images臨時影像
IplImage *mhi = 0; // MHI: motion history image 運動歷史影像
CvConnectedComp *cur_comp, min_comp; //連接部件
CvConnectedComp comp;
CvMemStorage *storage; //記憶體存盤器
CvPoint pt[4]; //二維坐標系下的點,型別為整型 ,通常以0點為原點,有x坐標和y坐標
int nCurFrameIndex = 0;
// 引數:
// img - 輸入視頻幀 // dst - 檢測結果
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/100.; // get current time in seconds 時間戳
CvSize size = cvSize(img->width,img->height); // get current frame size,得到當前幀的尺寸
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 < N; i++ )
{
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
cvZero( buf[i] );// clear Buffer Frame at the beginning
}
cvReleaseImage( &mhi );
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
cvZero( mhi ); // clear MHI at the beginning
} // end of if(mhi)
/*將當前要處理的幀轉化為灰度放到buffer的最后一幀中*/
cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale
/*設定幀的序號*/
idx1 = last;
idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;
// 做幀差
silh = buf[idx2];//差值的指向idx2
cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames
// 對差影像做二值化
cvThreshold( silh, silh, 50, 255, CV_THRESH_BINARY ); //threshold it,二值化
//去掉超時的影像以更新運動歷史影像
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI
cvConvert( mhi, dst );//將mhi轉化為dst,dst=mhi
// 中值濾波,消除小的噪聲
cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 );
cvPyrDown( dst, pyr, CV_GAUSSIAN_5x5 );// 向下采樣,去掉噪聲,影像是原影像的四分之一
cvDilate( pyr, pyr, 0, 1 ); // 做膨脹操作,消除目標的不連續空洞
cvPyrUp( pyr, dst, CV_GAUSSIAN_5x5 );// 向上采樣,恢復影像,影像是原影像的四倍
// 下面的程式段用來找到輪廓
// 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)
{
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);
}
}
// free memory
cvReleaseMemStorage(&stor);
cvReleaseImage( &pyr );
}
int main(int argc, char** argv)
{
//保存視頻檔案
//!將保存視頻檔案的名字設定成"F://VideoSave.avi"
char szVideoSaveName[] = "F://VideoSave.avi";
CvVideoWriter * pVideoWriter = NULL; //用于保存視頻檔案
//IplImage * pFrame = NULL;
//IplImage * pImage = NULL;
IplImage* motion = 0;
CvCapture* capture = 0;
capture = cvCaptureFromAVI("video.avi" );//AVI為視頻來源
if( capture )
{
cvNamedWindow( "Motion", 1 );//建立視窗
//創建視頻寫入器
pVideoWriter=cvCreateVideoWriter(/*"VideoSave.avi"*/szVideoSaveName,/*CV_FOURCC ('M','J', 'P', 'G')*/-1 ,25,cvSize(640,480),1);
for( ; ; )
{
IplImage* image;
if( !cvGrabFrame( capture ))//捕捉一楨
break;
image = cvRetrieveFrame( capture );//取出這個幀
if( image )//若取到則判斷motion是否為空
{
if( !motion )
{
motion = cvCreateImage( cvSize(image->width,image->height), 8, 1 ); //創建motion幀,八位,一通道
cvZero( motion ); //零填充motion
motion->origin = image->origin; //記憶體存盤的順序和取出的幀相同
}
}
update_mhi( image, motion, 60 );//更新歷史影像
cvShowImage( "Motion", image );//顯示處理過的影像
// pVideoWriter = cvCreateVideoWriter(/*"VideoSave.avi"*/szVideoSaveName,-1 ,10,cvSize(640,480),1);//放在里有視頻只會有一幀注意
cvWriteFrame(pVideoWriter,image);
// cvReleaseVideoWriter(&pVideoWriter);
if( cvWaitKey(10) >= 0 )//10ms中按任意鍵退出
break;
}
cvReleaseVideoWriter(&pVideoWriter);//釋放寫入器
cvReleaseCapture( &capture );//釋放設備
cvDestroyWindow( "Motion" );//銷毀視窗
}
return 0;
}
uj5u.com熱心網友回復:
代碼功能歸根結底不是別人幫自己看或講解或注釋出來的;而是被自己靜下心來花足夠長的時間和精力親自動手單步或設斷點或對執行到某步獲得的中間結果顯示或寫到日志檔案中一步一步分析出來的。提醒:再牛×的老師也無法代替學生自己領悟和上廁所!
單步除錯和設斷點除錯(VS IDE中編譯連接通過以后,按F10或F11鍵單步執行,按Shift+F11退出當前函式;在某行按F9設斷點后按F5執行停在該斷點處。)是程式員必須掌握的技能之一。
在不懂的代碼片斷的每一步使用 imshow("wintitle", img);waitKey(0);顯示中間結果img幫助理解。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/120602.html
標籤:圖形處理/算法
上一篇:c獲取變數地址的指標
下一篇:ENVI資料問題 BIP
