A multi-scale KCF Tracking Program code Analysis (II.)--image video conversion and initial frame input

Source: Internet
Author: User
Tags sprintf

A few days ago I analyzed the code here
But simply made a record, did not specify the use of
Image tracking, need three things: Tracking program, tracking image sequence, the initial box
corresponding to: Kcftracker code, image data and the name of the image sequence named Images.txt, the initial frame region.txt


Kcftracker code, it's been analyzed.


Image data, different people write the code interface is usually different, but mainly to the image and video conversion, and for the result, usually converted to video is easier to watch
In this analysis 2 programs, picture sequences converted into video files, and video files are converted into image sequences
1 The video is split into picture frames:
Using OPENCV to read video files, you define the name and storage location of the output image sequence. Give the input file and start, stop save frame number, save the middle frame
ARGV[1] is a video file name
ARGV[2] is the frame number from which the video file is saved: Use time * frame-frequency calculation
ARGV[3] is a video file from the stop-save frame number: Use time * frame-frequency calculation
ARGV[4] is to save a picture frame, every argv[4] frame, save a frame
When running on Ubuntu, it is usually the following command:
./opencv_player_save/media/li/os/users/lmw/desktop/\[shana\]1481948372.mp4 5825 7100 1
That is, save the. mp4 file from 5825 to 7100 in the middle of each frame.


#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;//Picture start frame number int endframe = 164;
  Char cur_fn[255]; Char prefix[] = "/media/li/os/users/lmw/desktop/1/";//the path of the picture sequence char ext[] = ". jpg";
  The suffix name of the sequence picture//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 "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 picture frame integration into video:
Here you need to define a picture of the width, height, video frame frequency, output file name, picture file path and the beginning of the end of the serial number:


#include <opencv/cv.h> #include <opencv2/highgui/highgui.hpp> #include <stdio.h>/**************** /int main () {int i = 0;////Initialize the video writer, the parameters are modified according to the actual video file cvvideowriter* writer = 0; int is
Color = 1; int fps = 25;
or 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;
Picture start frame number int endframe = 1654;
Char cur_fn[255]; Char prefix[] = "/home/li/work/kcf/project/kcf_src/";//the path of the picture sequence char ext[] = ". jpg";
Sequence picture suffix name//storage video file iplimage* img = 0;
int nframes = 50; for (i = 0; i < nframes; i++)//{//Cvwriteframe (WRITER,IMG);//write one frame to a video file Cvgrabframe (capture);//} while (STA
  Rtframe <= 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 (&AMP;IMG);
///Create Window//cvnamedwindow ("Mainwin", cv_window_autosize);
Cvshowimage ("Mainwin", IMG);
Cvwaitkey (20);
Release video memory Cvreleasevideowriter (&writer); }



3) Images.txt image sequence generation
In this, you also want to generate an image sequence file named 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<



4 Edit Area file Region.txt, should be the top left corner, upper right corner, lower left corner, lower right corner of the order.
Directly read the target location, modify the coordinates can be




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.