#include "cv.h"
#include "highgui.h"
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
// various tracking parameters (in seconds)
const double MHI_DURATION = 0.5;
const double MAX_TIME_DELTA = 0.5;
const double MIN_TIME_DELTA = 0.05;
// 用于运动检测的循环帧数,与机器速度以及FPS设置有关
const int N = 2;
// ring image buffer
IplImage **buf = 0;
int last = 0;
// temporary images
IplImage *mhi = 0; // MHI: motion history image
IplImage *orient = 0; // orientation
IplImage *mask = 0; // valid orientation mask
IplImage *segmask = 0; // motion segmentation map
CvMemStorage* storage = 0; // temporary storage
// parameters:
// img - input video frame
// dst - resultant motion picture
// args - optional parameters
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/1000.; // get current time in seconds
CvSize size = cvSize(img->width,img->height); // get current frame size
int i, idx1 = last, idx2;
IplImage* silh;
CvSeq* seq;
CvRect comp_rect;
double count;
double angle;
CvPoint center;
double magnitude;
CvScalar color;
// allocate images at the beginning or
// reallocate them if the frame size is changed
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] );
}
cvReleaseImage( &mhi );
cvReleaseImage( &orient );
cvReleaseImage( &segmask );
cvReleaseImage( &mask );
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
cvZero( mhi ); // clear MHI at the beginning
orient = cvCreateImage( size, IPL_DEPTH_32F, 1 );
segmask = cvCreateImage( size, IPL_DEPTH_32F, 1 );
mask = cvCreateImage( size, IPL_DEPTH_8U, 1 );
}
cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale
idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;
silh = buf[idx2];
// 相邻两帧的差
cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames
// 对差图像做二值化
cvThreshold( silh, silh, diff_threshold, 1, CV_THRESH_BINARY ); // and threshold it
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI
// convert MHI to blue 8u image
// cvCvtScale的第四个参数 shift = (MHI_DURATION - timestamp)*255./MHI_DURATION
// 控制帧差的消失速率
cvCvtScale( mhi, mask, 255./MHI_DURATION,
(MHI_DURATION - timestamp)*255./MHI_DURATION );
cvZero( dst );
cvCvtPlaneToPix(mask, 0, 0, 0, dst ); // B,G,R,0 -> dist : convert to BLUE image
// 计算运动的梯度方向以及正确的方向掩模mask
// Filter size = 3
cvCalcMotionGradient( mhi, mask, orient,
MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );
if( !storage )
storage = cvCreateMemStorage(0);
else
cvClearMemStorage(storage);
// 运动分割: 获得运动部件的连续序列
// segmask is marked motion components map. It is not used further
seq = cvSegmentMotion( mhi, segmask, storage, timestamp, MAX_TIME_DELTA );
// iterate through the motion components,
// One more iteration (i == -1) corresponds to the whole image (global motion)
for( i = 0; i < seq->total; i++ )
{
if( i < 0 ) { // case of the whole image,对整幅图像做操作
comp_rect = cvRect( 0, 0, size.width, size.height );
color = CV_RGB(255,255,255); // white color
magnitude = 100; // 画线长度以及圆半径的大小控制
}
else { // i-th motion component
comp_rect = ((CvConnectedComp*)cvGetSeqElem( seq, i ))->rect;
// 去掉小的部分
if( comp_rect.width + comp_rect.height < 100 )
continue;
color = CV_RGB(255,0,0); // red color
magnitude = 30;
//if(seq->total > 0) MessageBox(NULL,"Motion Detected",NULL,0);
}
// select component ROI
cvSetImageROI( silh, comp_rect );
cvSetImageROI( mhi, comp_rect );
cvSetImageROI( orient, comp_rect );
cvSetImageROI( mask, comp_rect );
// 在选择的区域内, 计算运动方向
angle = cvCalcGlobalOrientation( orient, mask, mhi, timestamp,
MHI_DURATION);
angle = 360.0 - angle; // adjust for images with top-left origin
// 在轮廓内计算点数
// Norm(L1) = sum of total pixel values
count = cvNorm( silh, 0, CV_L1, 0 );
// The function cvResetImageROI releases image ROI
cvResetImageROI( mhi );
cvResetImageROI( orient );
cvResetImageROI( mask );
cvResetImageROI( silh );
// check for the case of little motion
if( count < comp_rect.width*comp_rect.height * 0.05 ) // five percent of pixel
continue;
// draw a clock with arrow indicating the direction
center = cvPoint( (comp_rect.x + comp_rect.width/2),
(comp_rect.y + comp_rect.height/2) );
cvCircle( dst, center, cvRound(magnitude*1.2), color, 3, CV_AA, 0 );
cvLine( dst, center, cvPoint( cvRound( center.x +
magnitude*cos(angle*CV_PI/180)),
cvRound( center.y - magnitude*sin(angle*CV_PI/180))),
color, 3, CV_AA, 0 );
}
}
int main(int argc, char** argv)
{
IplImage* motion = 0;
CvCapture* capture = 0;
/* if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
else if( argc == 2 )
capture = cvCaptureFromAVI( argv[1] ); */
capture = cvCaptureFromAVI("1.AVI");
if( capture )
{
cvNamedWindow( "Motion", 1 );
for(;;)
{
IplImage* image;
if( !cvGrabFrame( capture ))
break;
image = cvRetrieveFrame( capture );
if( image )
{
if( !motion )
{
motion = cvCreateImage( cvSize(image->width,image->height),
8, 3 );
cvZero( motion );
motion->origin = image->origin;
}
}
update_mhi( image, motion, 60 );
cvShowImage( "Motion", motion );
if( cvWaitKey(10) >= 0 )
break;
}
cvReleaseCapture( &capture );
cvDestroyWindow( "Motion" );
}
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
opencv-detect.rar_cvaux.h_opencv tracking_opencv 跟踪_tracking ope
共69个文件
lib:19个
h:14个
dll:9个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 179 浏览量
2022-09-19
18:23:42
上传
评论
收藏 11.84MB RAR 举报
温馨提示
opencv图像处理, 运动视频图像跟踪
资源推荐
资源详情
资源评论
收起资源包目录
opencv-detect.rar (69个子文件)
opencv跟踪监测
cv.lib 31KB
quartz.lib 2KB
cvcompat.h 41KB
cv.hpp 2KB
cvaux.hpp 8KB
motempl.suo 10KB
highgui.lib 13KB
motempl.dsp 4KB
motempl.c 7KB
cvcam097.dll 84KB
motempl.opt 53KB
trsd.lib 17KB
ml.h 2KB
cxtsd.lib 38KB
motempl.plg 1KB
cxcore097.dll 788KB
cvaux097.dll 420KB
highgui097.dll 368KB
cvaux.lib 40KB
ddraw.lib 4KB
motempl.dsw 539B
hcnetsdk.h 227KB
quartz.dll 1.19MB
cxcored.lib 53KB
cvcam.h 7KB
cv.h 52KB
vssver2.scc 454B
cvd.lib 31KB
1.avi 22.62MB
strmiids.lib 266KB
motempl.vcproj 5KB
cxcore097d.dll 1.41MB
cvaux.h 59KB
ml.hpp 81KB
cv097d.dll 1.11MB
cxerror.h 9KB
cxcore.lib 53KB
motempl.vcproj.ONLINE-GSMAIN.Administrator.user 1KB
trs.lib 18KB
strmbasd.lib 2.43MB
plaympeg4.h 18KB
cxtypes.h 51KB
motempl.sln 878B
WinMM.Lib 45KB
highguid.lib 13KB
cxcore.h 70KB
cv097.dll 556KB
cvcam.lib 7KB
DecodeCardSdk.h 22KB
cvtypes.h 13KB
DataType.h 3KB
Makefile 135B
Debug
motempl.exe.embed.manifest.res 212B
motempl.ilk 1.11MB
BuildLog.htm 6KB
motempl.obj 32KB
mt.dep 67B
vc80.pdb 84KB
vc80.idb 235KB
motempl.pdb 1.95MB
motempl.exe 488KB
motempl.exe.intermediate.manifest 145B
motempl.exe.embed.manifest 146B
highgui.h 15KB
cxts.lib 38KB
motempl.ncb 6.71MB
cvhaartraining.lib 498KB
MSVCRTD.DLL 376KB
ml.lib 120KB
共 69 条
- 1
资源评论
寒泊
- 粉丝: 74
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功