一种多尺度的KCF跟踪程序代码分析(二)——图片视频转换和初始框输入

前几天我在 这里 分析了一下代码
但只是简单做了个记录,没有说明具体的使用
图像跟踪,需要三样东西: 跟踪程序,跟踪图片序列,初始框
对应的是:KCFTracker代码,图像数据和名为images.txt的图像序列名字,初始框region.txt


KCFTracker代码,已经分析过了


图像数据,不同人写的代码接口通常不同,但主要是图片和视频的来回转换,而且对于结果来说,通常转换成视频比较易于观看
在这分析2个程序,图片序列转换成视频文件,和视频文件转换成图像序列
1)视频拆成图片帧:
使用OpenCV读取视频文件,自己定义输出图像序列的名字和存储位置。给出输入文件和起始、停止保存的帧号,保存中间的帧
argv[1]是视频文件名字
argv[2]是视频文件从开始保存的帧号:使用时间*帧频计算
argv[3]是视频文件从停止保存的帧号:使用时间*帧频计算
argv[4]是保存图片帧时,每argv[4]帧,保存一帧
在Ubuntu上面运行的时候,通常是如下命令:
./Opencv_player_save  /media/li/OS/Users/lmw/Desktop/\[SHANA\]1481948372.mp4 5825 7100 1
即保存.mp4文件从5825到7100中间的每一帧。


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;

int main(int argc, char ** argv){
  int startframe = 8881; //图片开始帧号
  int endframe = 164;
  char cur_fn[255];
  char prefix[] = "/media/li/OS/Users/lmw/Desktop/1/";//图片序列的路径
  char ext[] = ".jpg"; //序列图片的后缀名
  //open the video
  cv::VideoCapture capture(argv[1]);
  //check the video
  if(!capture.isOpened())
    return 1;
  //get the rate
  double rate = capture.get(CV_CAP_PROP_FPS);
  bool stop(false);
  cv::Mat frame;
  cv::namedWindow("Opencv Player");
  capture.read(frame);
  std::cout<<"the width is "<<frame.cols<<std::endl;
  std::cout<<"the highth is "<<frame.rows<<std::endl;
  //get the delay
  int delay = 1000/rate;
  if (delay == 0)
    delay = 33;
  int god = 0;
  //show every picture of the video
  while(!stop){
    //try to get next picture
    if(!capture.read(frame))
      break;
    //cv::imshow("Opencv Player", frame);
    //if(cv::waitKey(delay)>=0)
    //  stop = true;

  god++;
  cout<<"frame :" <<god<<endl;
  if(god>atof(argv[2]) && god<atof(argv[3])  && god%((int)atof(argv[4]))==0){
    strcpy(cur_fn,"");
    sprintf(cur_fn,"%s%04d%s",prefix,startframe,ext);
    startframe++;
    cv::imwrite(cur_fn, frame);
  }
  else if(god>=atof(argv[3])){
    exit(0);
  }
  }
  capture.release();
}



2)图片帧整合成视频:
在这里需要定义一副图像的宽、高,视频帧频,输出文件名字,图片文件的路径和起始结束的序号:


#include <opencv/cv.h>
#include <opencv2/highgui/highgui.hpp>
#include <stdio.h>
/*******************************************************/
int main()
{
int i = 0;
//初始化视频编写器,参数根据实际视频文件修改
CvVideoWriter* writer = 0;
int isColor = 1;
//int fps = 25; // or 30
double fps = 15;
int frameW = 1920;
int frameH = 1080;
writer = cvCreateVideoWriter("RGB.avi",CV_FOURCC('X','V','I','D'),fps, cvSize(frameW, frameH), isColor);
printf("\tvideo height:%d\n\tvidoe width:%d\n\t\fps:%f\n",frameH, frameW, fps);
int startframe = 1; //图片开始帧号
int endframe = 1654;
char cur_fn[255];
char prefix[] = "/home/li/work/KCF/project/KCF_src/";//图片序列的路径
char ext[] = ".jpg"; //序列图片的后缀名
//存储视频文件
IplImage* img = 0;
// int nFrames = 50;
// for (i = 0; i < nFrames; i++)
// {
// cvWriteFrame(writer,img); //写入一帧到一个视频文件中 cvGrabFrame(capture);
// }
while (startframe <= endframe)
{
  strcpy(cur_fn,"");
  sprintf(cur_fn,"%s%04d%s",prefix,startframe,ext);
  img = cvLoadImage(cur_fn,isColor);
  if (!img){
    printf("can not open file\n");
    return 0;
  }
  //cvNamedWindow("mainWin0",CV_WINDOW_AUTOSIZE);
  //cvShowImage("mainWin0",img);
  //cvWaitKey(20);
  cvWriteFrame(writer,img);
  cvWaitKey(20);
  startframe++;
  cvReleaseImage(&img);
}
//创建窗口
//cvNamedWindow("mainWin",CV_WINDOW_AUTOSIZE);
//cvShowImage("mainWin",img);
//cvWaitKey(20);
//释放视频存储器
cvReleaseVideoWriter(&writer);
}



3)images.txt图像序列生成
在这还要生成名为images.txt的图像序列文件


#include <fstream>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
  string result_name = "images1.txt";
  ofstream result("images1.txt");
  if (!result)
    cout<<"error!"<<endl;

  int img_num = 8951;
  for (int i=8881;i<=img_num;i++)
  {
    char img_name[80];
    result<<"/media/li/OS/Users/lmw/Desktop/1/";
    sprintf(img_name, "%04d.jpg\n",i);
    result<<img_name;
  }
  return 0;
}



4)编辑区域文件region.txt,应该是左上角点,右上角点,左下角点,右下角点的顺序
直接读取目标位置,修改坐标即可




## Tracking with Kernelized Correlation Filters Code author : Tomas Vojir ________________ This is a C++ reimplementation of algorithm presented in "High-Speed Tracking with Kernelized Correlation Filters" paper. For more info and implementation in other languages visit the [autor's webpage!](http://home.isr.uc.pt/~henriques/circulant/). It is extended by a scale estimation (use several *7* different scales steps) and by a RGB (channels) and Color Names [2] features. Data for Color Names features were obtained from [SAMF tracker](https://github.com/ihpdep/samf). It is free for research use. If you find it useful or use it in your research, please acknowledge my git repository and cite the original paper [1]. The code depends on OpenCV 2.4+ library and is build via cmake toolchain. _________________ Quick start guide for linux: open terminal in the directory with the code $ mkdir build; cd build; cmake .. ; make This code compiles into binary **kcf_vot** ./kcf_vot - using VOT 2014 methodology (http://www.votchallenge.net/) - INPUT : expecting two files, images.txt (list of sequence images with absolute path) and region.txt with initial bounding box in the first frame in format "top_left_x, top_left_y, width, height" or four corner points listed clockwise starting from bottom left corner. - OUTPUT : output.txt containing the bounding boxes in the format "top_left_x, top_left_y, width, height" ./kcf_trax - using VOT 2014+ trax protocol (http://www.votchallenge.net/) - require [trax](https://github.com/votchallenge/trax) library to be compiled with opencv support and installed. See trax instruction for compiling and installing. ___________ Performance | | **VOT2016 - baseline EAO** | **VOT2016 - unsupervised EAO** | [**TV77**](http://cmp.felk.cvut.cz/~vojirtom/dataset/index.html) Avg. Recall | |:---------------|:--------------:|:------------------:|:----------------:| | kcf |0.1530 | 0.3859 | 51% | | skcf |0.1661 | 0.4155 | 56% | | skcf-cn |0.178 | 0.4136 | 58% | | kcf-master |**0.1994** | **0.4376** | **63%** | __________ References [1] João F. Henriques, Rui Caseiro, Pedro Martins, Jorge Batista, “High-Speed Tracking with Kernelized Correlation Filters“, IEEE Transactions on Pattern Analysis and Machine Intelligence, 2015 [2] J. van de Weijer, C. Schmid, J. J. Verbeek, and D. Larlus. "Learning color names for real-world applications." TIP, 18(7):1512–1524, 2009. _____________________________________ Copyright (c) 2014, Tomáš Vojíř Permission to use, copy, modify, and distribute this software for research purposes is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. __________________ Additional Library NOTE: The following files are part of Piotr's Toolbox, and were modified for usage with c++ src/piotr_fhog/gradientMex.cpp src/piotr_fhog/sse.hpp src/piotr_fhog/wrappers.hpp You are encouraged to get the [full version of this library here.](http://vision.ucsd.edu/~pdollar/toolbox/doc/index.html) ______________________________________________________________________________ Copyright (c) 2012, Piotr Dollar All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project.
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值