opencv 运动目标检测
生活随笔
收集整理的這篇文章主要介紹了
opencv 运动目标检测
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
<span style="font-size:18px;color:#ff0000;">請注意你要安裝opencv,復制代碼后要配置opencv環(huán)境,然后找個視頻把代碼中路徑改為你視頻路徑。</span>
#include "cv.h"
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h> // various tracking parameters (in seconds) //跟蹤的參數(shù)(單位為秒)
const double MHI_DURATION = 1;//0.5s為運動跟蹤的最大持續(xù)時間
//const double MAX_TIME_DELTA = 1; //最大時間增量為0.5s
//const double MIN_TIME_DELTA = 0.05; //最小時間增量0.05s
const int N = 3;
//
const int CONTOUR_MAX_AERA = 900; // 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;// 參數(shù):
// 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;
//width和height通過&運算變?yōu)閕mg的一半
IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 );CvMemStorage *stor;
CvSeq *cont; /*先進行數(shù)據(jù)的初始化*/
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, 30, 255, CV_THRESH_BINARY ); //threshold it,二值化 第三個參數(shù)閾值會影響效果//去掉超時的影像以更新運動歷史圖像
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI
cvCvtScale(mhi,dst,255./MHI_DURATION,(MHI_DURATION-timestamp)*255./MHI_DURATION);
cvCvtScale(mhi,dst,255./MHI_DURATION,0);
//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 ); // 做膨脹操作,消除目標的不連續(xù)空洞 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));
cvFindContours( dst, stor, &cont, sizeof(CvContour),
1, 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)
{
//保存視頻文件
//!將保存視頻文件的名字設置成"FVideoSave.avi"
char szVideoSaveName[] = "VideoSave.avi"; CvVideoWriter * pVideoWriter = 0; //用于保存視頻文件
//IplImage * pFrame = NULL;
//IplImage * pImage = NULL;
IplImage* motion = 0;
CvCapture* capture = 0; capture = cvCaptureFromAVI("F:\\OPENCV實驗室\\test.avi" );//AVI為視頻來源 if( capture )
{
cvNamedWindow( "Motion", 1 );//建立窗口 int frameH = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT);//liumengint frameW = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH);//liumengint fps = (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);//liumeng
//創(chuàng)建視頻寫入器
//pVideoWriter=cvCreateVideoWriter(/*"VideoSave.avi"*/szVideoSaveName,/*CV_FOURCC('M','J', 'P', 'G')*/-1 ,25,cvSize(640,480),1);
pVideoWriter=cvCreateVideoWriter(szVideoSaveName,CV_FOURCC('M','J', 'P', 'G'),25,cvSize(frameW,frameH),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 ); //創(chuàng)建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;
}
總結
以上是生活随笔為你收集整理的opencv 运动目标检测的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv中在图片上显示文本
- 下一篇: sklearn逻辑回归 极大似然 损失_