版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、计算方法实验报告实验名称光流法 实验时间2015.1.12 小组舵手不剁手 班级12电科 成员谢静 韦飞燕 靳婷婷 张一帆 陈泽宇 一、 实验目的,内容 目的 1.了解光流法的背景知识;2.掌握光流法的基本原理和方法,并用用MATLAB或vc6.0下实现光流法;3.掌握课外知识并提高小组成员之间的合作能力;4. 通过实验将理论和实践联系起来,提升对于理论知识的认识。内容1.用MATLAB或vc6.0下实现光流法,并实现视频或者摄像头的监控2.编程实现对进入视觉范围内的运动物体实施监测与跟。;二、相关背景知识介绍 1.光流的概念(Optical flow or optic flow)1950年,
2、Gibuson首先提出了光流的概念,所谓光流就是指图像表现运动的速度。物体在运动的时候之所以能被人眼发现,就是因为当物理运动时,会在人的视网膜上形成一系列的连续变化的图像,这些变化信息在不同时间,不断的流过眼睛视网膜,就好像一种光流过一样,故称之为光流。2.光流法的原理 光流法用于目标检测的原理:给图像中的每个像素点赋予了一个速度矢量,这样就形成了一个运动矢量场。在某一特定时刻,图像上的点与三维物体上的点一一对应,这种对应关系可以通过投影来计算得到。根据各个像素点的速度矢量特征,可以对图像进行动态分析。如果图像中没有运动目标,则光流矢量在整个图像区域是连续变化的。当图像中有运动物体时,目标和背
3、景存在着相对运动。运动物体所形成的速度矢量必然和背景的速度矢量有所不同,如此便可以计算出运动物体的位置。需要提醒的是,利用光流法进行物体检测时,计算量较大,无法保证实时性和实用性。光流法用于目标跟踪的原理:(1)对一个连续的视频帧序列进行处理;(2)针对每一个视频序列,利用一定的目标检测方法,检测可能出现的前景目标;(3)如果某一帧出现了前景目标,找到其具有代表性的关键特征点;(4)对之后的任意两个相邻视频帧而言,寻找上一帧中出现的关键特征点在当前帧中的最佳位置,从而得到前景目标在当前帧中的位置坐标。3.光流的计算方法从不同的分析角度引入不同的约束条件,就会导致产生不同的光流计算方法。目前应用
4、较为普遍的光流计算方法主要有以下四种:(1)基于梯度的方法(2)基于匹配的方法(3)基于相位的方法(4)基于能量的方法基于时空梯度的光流算法也称为微分法,是一种最常用的计算方法,根据时变图像灰度的梯度函数,梯度算法得到图像中每个像素点的运动矢量。基于匹配的方法就是在图像序列的顺序图像之间,搜索与相对像素点最拟合的位移,这个位移就是最终需要的速度矢量。基于相位的方法在光流的计算中引入了相位信息,在带通调谐滤波器的输出中,利用与等相位轮廓垂直的瞬时运动可以确定分速度。三、代码 CppExp.cpp#include #include cv.h#include highgui.h#include ma
5、th.hstatic const double pi = 3.;inline static double square(int a)return a * a;/* This is just an inline that allocates images. I did this to reduce clutter in the* actual computer vision algorithmic code. Basically it allocates the requested image* unless that image is already non-NULL. It always l
6、eaves a non-NULL image as-is even* if that images size, depth, and/or channels are different than the request.*/*这仅仅是一个行内分配图像。我这样做是在实际的计算机视觉算法的代码,以减少混乱。基本上它分配请求的图像,除非该图像已经非NULL。为正的,即使该图像的大小,深度和/或信道比的要求是不同的,它总是会留下一个非NULL图像。*/inline static void allocateOnDemand( IplImage *img, CvSize size, int depth,
7、int channels)if ( *img != NULL ) return;*img = cvCreateImage( size, depth, channels );if ( *img = NULL )fprintf(stderr, Error: Couldnt allocate image. Out of memory?n);exit(-1);int main(void)/* Create an object that decodes the input video stream. */*创建一个对象,解码输入的视频数据流。*/CvCapture *input_video = cvCa
8、ptureFromFile(video1.avi);/CvCapture *input_video = cvCaptureFromCAM(-1);if (input_video = NULL)/* Either the video didnt exist OR it uses a codec OpenCV doesnt support.*/fprintf(stderr, Error: Cant open video.n);return -1;/* This is a hack. If we dont call this first then getting capture* propertie
9、s (below) wont work right. This is an OpenCV bug. We* ignore the return value here. But its actually a video frame.*/cvQueryFrame( input_video );/* Read the videos frame size out of the AVI. */CvSize frame_size;frame_size.height =(int) cvGetCaptureProperty( input_video, CV_CAP_PROP_FRAME_HEIGHT );fr
10、ame_size.width =(int) cvGetCaptureProperty( input_video, CV_CAP_PROP_FRAME_WIDTH );/* Determine the number of frames in the AVI. */long number_of_frames;/* Go to the end of the AVI (ie(即): the fraction is 1) */cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_AVI_RATIO, 1. );/* Now that were at the
11、 end, read the AVI position in frames */number_of_frames = (int) cvGetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES );/* Return to the beginning */cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES, 0. );/* Create three windows called Frame N, Frame N+1, and Optical Flow* for visualizi
12、ng the output. Have those windows automatically change their* size to match the output.*/cvNamedWindow(Optical Flow, CV_WINDOW_AUTOSIZE);long current_frame = 0;while(true)static IplImage *frame = NULL, *frame1 = NULL, *frame1_1C = NULL, *frame2_1C =NULL, *eig_image = NULL, *temp_image = NULL, *pyram
13、id1 = NULL, *pyramid2 = NULL;/* Go to the frame we want. Important if multiple frames are queried in* the loop which they of course are for optical flow. Note that the very* first call to this is actually not needed. (Because the correct position* is set outsite the for() loop.)*/cvSetCapturePropert
14、y( input_video, CV_CAP_PROP_POS_FRAMES, current_frame );/* Get the next frame of the video.* IMPORTANT! cvQueryFrame() always returns a pointer to the _same_* memory location. So successive calls:* frame1 = cvQueryFrame();* frame2 = cvQueryFrame();* frame3 = cvQueryFrame();* will result in (frame1 =
15、 frame2 & frame2 = frame3) being true.* The solution is to make a copy of the cvQueryFrame() output.*/frame = cvQueryFrame( input_video );if (frame = NULL)/* Why did we get a NULL frame? We shouldnt be at the end. */fprintf(stderr, Error: Hmm. The end came sooner than we thought.n);return -1;/* Allo
16、cate another image if not already allocated.* Image has ONE challenge of color (ie: monochrome) with 8-bit color depth.* This is the image format OpenCV algorithms actually operate on (mostly).*/allocateOnDemand( &frame1_1C, frame_size, IPL_DEPTH_8U, 1 );/* Convert whatever the AVI image format is i
17、nto OpenCVs preferred format.* AND flip the image vertically. Flip is a shameless hack. OpenCV reads* in AVIs upside-down by default. (No comment :-)*/cvConvertImage(frame, frame1_1C, CV_CVTIMG_FLIP);/* Well make a full color backup of this frame so that we can draw on it.* (Its not the best idea to
18、 draw on the static memory space of cvQueryFrame().)*/allocateOnDemand( &frame1, frame_size, IPL_DEPTH_8U, 3 );cvConvertImage(frame, frame1, CV_CVTIMG_FLIP);/* Get the second frame of video. Sample principles as the first. */frame = cvQueryFrame( input_video );if (frame = NULL)fprintf(stderr, Error:
19、 Hmm. The end came sooner than we thought.n);return -1;allocateOnDemand( &frame2_1C, frame_size, IPL_DEPTH_8U, 1 );cvConvertImage(frame, frame2_1C, CV_CVTIMG_FLIP);/* Shi and Tomasi Feature Tracking! */* Preparation: Allocate the necessary storage. */allocateOnDemand( &eig_image, frame_size, IPL_DEP
20、TH_32F, 1 );allocateOnDemand( &temp_image, frame_size, IPL_DEPTH_32F, 1 );/* Preparation: This array will contain the features found in frame 1. */CvPoint2D32f frame1_features400;/* Preparation: BEFORE the function call this variable is the array size* (or the maximum number of features to find). AF
21、TER the function call* this variable is the number of features actually found.*/int number_of_features;/* Im hardcoding this at 400. But you should make this a #define so that you can* change the number of features you use for an accuracy/speed tradeoff analysis.*/number_of_features = 400;/* Actuall
22、y run the Shi and Tomasi algorithm(算法)!* frame1_1C is the input image.* eig_image and temp_image are just workspace for the algorithm.* The first .01 specifies(指定) the minimum quality of the features(特征) (based on theeigenvalues).* The second .01 specifies the minimum Euclidean(欧几里得) distance betwee
23、n features.* NULL means use the entire input image. You could point to a part of theimage.* WHEN THE ALGORITHM RETURNS:* frame1_features will contain the feature points.* number_of_features will be set to a value = 400 indicating(表明指出) the number of feature points found.*/cvGoodFeaturesToTrack(frame
24、1_1C, eig_image, temp_image, frame1_features, &number_of_features, .01, .01, NULL);/* Pyramidal(金字塔的) Lucas Kanade Optical Flow! */* This array will contain the locations of the points from frame 1 in frame 2. */CvPoint2D32f frame2_features400;/* The i-th element of this array will be non-zero if an
25、d only if the i-th feature of* frame 1 was found in frame 2.*/char optical_flow_found_feature400;/* The i-th element of this array is the error in the optical flow for the i-th feature* of frame1 as found in frame 2. If the i-th feature was not found (see the array above)* I think the i-th entry in
26、this array is undefined.*/float optical_flow_feature_error400;/* This is the window size to use to avoid the aperture problem (see slide(幻灯片) Optical Flow: Overview). */CvSize optical_flow_window = cvSize(3,3);/* This termination(终止) criteria(条件) tells the algorithm to stop when it has either done 2
27、0 iterations(重复) or when* epsilon is better than .3. You can play with these parameters(参数)for speed vs. accuracy but these values* work pretty well in many situations. cvTermCriteria(type,最大迭代次数,结果精确性)*/CvTermCriteria optical_flow_termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRI
28、T_EPS, 20, .3 );/* This is some workspace for the algorithm.* (The algorithm actually carves(切割) the image into pyramids of different resolutions(分辨率).)*/allocateOnDemand( &pyramid1, frame_size, IPL_DEPTH_8U, 1 );allocateOnDemand( &pyramid2, frame_size, IPL_DEPTH_8U, 1 );/* Actually run Pyramidal Lu
29、cas Kanade Optical Flow!* frame1_1C is the first frame with the known features.* frame2_1C is the second frame where we want to find the first frames features.* pyramid1 and pyramid2 are workspace for the algorithm.* frame1_features are the features from the first frame.* frame2_features is the (out
30、putted) locations of those features in the second frame.* number_of_features is the number of features in the frame1_features array.* optical_flow_window is the size of the window to use to avoid the aperture problem.* 5 is the maximum number of pyramids to use. 0 would be just one level.* optical_f
31、low_found_feature is as described above (non-zero iff feature found by the flow).* optical_flow_feature_error is as described above (error in the flow for this feature).* optical_flow_termination_criteria is as described above (how long the algorithm should look).* 0 means disable enhancements(增强).
32、(For example, the second array isnt preinitialized with guesses.)*/cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features,frame2_features, number_of_features, optical_flow_window, 5,optical_flow_found_feature, optical_flow_feature_error,optical_flow_termination_criteria, 0
33、);/* For fun (and debugging :), lets draw the flow field. */for(int i = 0; i number_of_features; i+)/* If Pyramidal Lucas Kanade didnt really find the feature, skip it. */if ( optical_flow_found_featurei = 0 ) continue;int line_thickness; line_thickness = 1;/* CV_RGB(red, green, blue) is the red, gr
34、een, and blue components* of the color you want, each out of 255.*/CvScalar line_color; line_color = CV_RGB(255,0,0);/* Lets make the flow field look nice with arrows. */* The arrows will be a bit too short for a nice visualization because of thehigh framerate* (ie: theres not much motion between th
35、e frames). So lets lengthen themby a factor of 3.*/CvPoint p,q;p.x = (int) frame1_featuresi.x;p.y = (int) frame1_featuresi.y;q.x = (int) frame2_featuresi.x;q.y = (int) frame2_featuresi.y;double angle; angle = atan2( (double) p.y - q.y, (double) p.x - q.x );double hypotenuse; hypotenuse = sqrt( squar
36、e(p.y - q.y) + square(p.x - q.x) );/* Here we lengthen the arrow by a factor of three. */q.x = (int) (p.x - 3 * hypotenuse * cos(angle);q.y = (int) (p.y - 3 * hypotenuse * sin(angle);/* Now we draw the main line of the arrow. */* frame1 is the frame to draw on.* p is the point where the line begins.
37、* q is the point where the line stops.* CV_AA means antialiased drawing.* 0 means no fractional bits in the center cooridinate or radius.*/cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );/* Now draw the tips of the arrow. I do some scaling so that the* tips look proportional to the main line of the arrow.*/p.x = (int) (q.x + 9 * cos(angle + pi / 4);p.y = (int) (q.y + 9 * sin(angle + pi / 4);cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );p.x = (int) (q.x + 9 * cos(angle - pi / 4);p.y = (int) (q.y + 9 * sin(an
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 一年级卡通人物描写
- 《棱柱结构特征》课件
- T/VSTR 010-2021室内通信设备机柜通用技术要求
- T/TMAC 239.2-2025汽车安全气囊模块 第2部分:前排左侧右侧座椅模块
- 光机电一体化公司副总经理述职报告
- 《地球运动的意义》课件
- 《凸透镜及其应用》课件
- 《计算机网络》谢希仁第五版课后题答案
- 2026 年秋冬季流感预防:医疗机构空气消毒流感防控课件
- 2026年营养素摄入与平衡习题集
- 2026中国移动校园招聘备考考试题库附答案解析
- 2025年陪诊师考试题库(附答案)
- 三一研发项目管理办法
- 运营商反诈知识培训课件
- 北大印章管理办法
- 前列腺癌护理个案查房
- 患者自行拔出尿管护理不良事件
- 高三化学一轮复习-配合物 课件
- 2024江苏南京证券校园招聘129人高频500题难、易错点模拟试题附带答案详解
- 凝中国心铸中华魂铸牢中华民族共同体意识-小学民族团结爱国主题班会课件
- 季节性安全教育培训
评论
0/150
提交评论