学习程序.docx_第1页
学习程序.docx_第2页
学习程序.docx_第3页
学习程序.docx_第4页
学习程序.docx_第5页
已阅读5页,还剩4页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include opencv2/opencv.hpp#include #include using namespace std;using namespace cv;class FrameProcessor;/帧处理基类class FrameProcessorpublic:virtual void process(Mat &input,Mat &ouput)=0;/特征跟踪类,继承自帧处理基类class FeatureTracker : public FrameProcessorMat gray; /当前灰度图Mat gray_prev; /之前的灰度图vector points2;/前后两帧的特征点vector initial;/初始特征点vector features;/检测到的特征int max_count; /要跟踪特征的最大数目double qlevel; /特征检测的指标double minDist;/特征点之间最小容忍距离vector status; /特征跟踪状态/vector这样定义已经初始化了,其特性就是可以动态存取。vector err; /跟踪时的错误public:FeatureTracker():max_count(500),qlevel(0.01),minDist(10.)void process(Mat &frame,Mat &output)/得到灰度图cvtColor (frame,gray,CV_BGR2GRAY);frame.copyTo (output);/特征点太少了,重新检测特征点if(addNewPoint()detectFeaturePoint ();/插入检测到的特征点points0.insert (points0.end (),features.begin (),features.end ();initial.insert (initial.end (),features.begin (),features.end ();/第一帧if(gray_prev.empty ()gray.copyTo (gray_prev);/根据前后两帧灰度图估计前一帧特征点在当前帧的位置/默认窗口是*15calcOpticalFlowPyrLK (gray_prev,/前一帧灰度图gray,/当前帧灰度图points0,/前一帧特征点位置points1,/当前帧特征点位置status,/特征点被成功跟踪的标志err);/前一帧特征点点小区域和当前特征点小区域间的差,根据差的大小可删除那些运动变化剧烈的点int k = 0;/去除那些未移动的特征点for(int i=0;ipoints1.size ();i+)if(acceptTrackedPoint (i)initialk=initiali;points1k+ = points1i;points1.resize (k);initial.resize (k);/标记被跟踪的特征点handleTrackedPoint (frame,output);/为下一帧跟踪初始化特征点集和灰度图像std:swap(points1,points0);cv:swap(gray_prev,gray);void detectFeaturePoint()goodFeaturesToTrack (gray,/图片features,/输出特征点max_count,/特征点最大数目qlevel,/质量指标minDist);/最小容忍距离bool addNewPoint()/若特征点数目少于,则决定添加特征点return points0.size ()2);/画特征点void handleTrackedPoint(Mat &frame,Mat &output)for(int i=0;ipointsi.size ();i+)/当前特征点到初始位置用直线表示line(output,initiali,points1i,Scalar:all (0);/当前位置用圈标出circle(output,points1i,3,Scalar:all(0),(-1);class VideoProcessorprivate:VideoCapture caputure;/写视频流对象VideoWriter writer;/输出文件名string Outputfile;int currentIndex;int digits;string extension;FrameProcessor *frameprocessor;/图像处理函数指针void (*process)(Mat &,Mat &);bool callIt;string WindowNameInput;string WindowNameOutput;/延时int delay;long fnumber;/第frameToStop停止long frameToStop;/暂停标志bool stop;/图像序列作为输入视频流vector images;/迭代器public:VideoProcessor() : callIt(true),delay(0),fnumber(0),stop(false),digits(0),frameToStop(-1)/设置图像处理函数void setFrameProcessor(void (*process)(Mat &,Mat &)frameprocessor = 0;this-process = process;CallProcess ();/打开视频bool setInput(string filename)fnumber = 0;/若已打开,释放重新打开caputure.release ();return caputure.open (filename);/设置输入视频播放窗口void displayInput(string wn)WindowNameInput = wn;namedWindow (WindowNameInput);/设置输出视频播放窗口void displayOutput(string wn)WindowNameOutput = wn;namedWindow (WindowNameOutput);/销毁窗口void dontDisplay()destroyWindow (WindowNameInput);destroyWindow (WindowNameOutput);WindowNameInput.clear ();WindowNameOutput.clear ();/启动void run()Mat frame;Mat output;if(!isOpened()return;stop = false;while(!isStopped()/读取下一帧if(!readNextFrame(frame)break;if(WindowNameInput.length ()!=0)imshow (WindowNameInput,frame);/处理该帧if(callIt)if(process)process(frame,output);else if(frameprocessor)frameprocessor-process (frame,output);elseoutput = frame;if(Outputfile.length ()cvtColor (output,output,CV_GRAY2BGR);writeNextFrame (output);if(WindowNameOutput.length ()!=0)imshow (WindowNameOutput,output);/按键暂停,继续按键继续if(delay=0&waitKey (delay)=0)waitKey(0);/到达指定暂停键,退出if(frameToStop=0&getFrameNumber()=frameToStop)stopIt();/暂停键置位void stopIt()stop = true;/查询暂停标志位bool isStopped()return stop;/返回视频打开标志bool isOpened()return caputure.isOpened ()|!images.empty ();/设置延时void setDelay(int d)delay = d;/读取下一帧bool readNextFrame(Mat &frame)if(images.size ()=0)return caputure.read (frame);elseif(itImg!=images.end()frame = imread (*itImg);itImg+;return frame.data?1:0;elsereturn false;void CallProcess()callIt = true;void dontCallProcess()callIt = false;/设置停止帧void stopAtFrameNo(long frame)frameToStop = frame;/ 获得当前帧的位置long getFrameNumber()long fnumber = static_cast(caputure.get (CV_CAP_PROP_POS_FRAMES);return fnumber;/获得帧大小Size getFrameSize() if (images.size()=0) / 从视频流获得帧大小int w= static_cast(caputure.get(CV_CAP_PROP_FRAME_WIDTH);int h= static_cast(caputure.get(CV_CAP_PROP_FRAME_HEIGHT);return Size(w,h);else /从图像获得帧大小cv:Mat tmp= cv:imread(images0);return (tmp.data)?(tmp.size():(Size(0,0);/获取帧率double getFrameRate()return caputure.get(CV_CAP_PROP_FPS);vector:const_iterator itImg;bool setInput (const vector &imgs)fnumber = 0;caputure.release ();images = imgs;itImg = images.begin ();return true;void setFrameProcessor(FrameProcessor *frameprocessor)process = 0;this-frameprocessor = frameprocessor;CallProcess ();/获得编码类型int getCodec(char codec4) if (images.size()!=0)return -1;union / 数据结构-charint value;char code4; returned;/获得编码值returned.value= static_cast(caputure.get(CV_CAP_PROP_FOURCC);/ get the 4 characterscodec0= returned.code0;codec1= returned.code1;codec2= returned.code2;codec3= returned.code3;return returned.value;bool setOutput(const string &filename,int codec = 0,double framerate = 0.0,bool isColor = true)/设置文件名Outputfile = filename;/清空扩展名extension.clear ();/设置帧率if(framerate =0.0)framerate = getFrameRate ();/获取输入原视频的编码方式char c4;if(codec=0)codec = getCodec(c);return writer.open(Outputfile,codec,framerate,getFrameSize(),isColor);/输出视频帧到图片fileme+currentIndex.ext,如filename001.jpgbool setOutput (const string &filename,/路径const string &ext,/扩展名int numberOfDigits=3,/数字位数int startIndex=0 )/起始索引if(numberOfDigits0)return false;Outputfile = filename;extension = ext;digits = numberOfDigits;currentIndex = startIndex;return true;/写下一帧void writeNextFrame(Mat &frame)/如果扩展名不为空,写到图片文件中if(extension.length ()stringstream ss;ssOutputfilesetfill(0)setw(digits)currentIndex+extension;imwrite (ss.str (),frame);/反之,写到视频文件中elsewriter

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论