img2arv example

<< return to examples index

Overview

Convert a serie of image to an arv video file.

Download

The whole example is available here: img2arv.zip

Source: img2arv.cpp

Download img2arv.cpp.

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <alvision/alvisiondefinitions.h>
#include <alvision/alvideo.h>


namespace fs = boost::filesystem;

using namespace AL;

void printUsage()
{
  printf("Usage: imgFiles2arv DIRECTORY ARV_FILE\n"
         "Parse all image files in DIRECTORY and place them into an ARV_FILE.\n"
         "Note that only color images with the same resolution than the first one"
         " found in the folder will be processed.\n\n" );
}



int main(int argc, char** argv) {

  //int streamNumber = 0;

  if (argc < 3) {
    printUsage();
    return -1;
  }

  //const char* imgFolder = argv[1];
  char* arvFile   = argv[2];

  if( strlen(arvFile) > 4 ) //remove arv extension if writen by the user
    if( !strcmp( arvFile+strlen(arvFile)-4 ,".arv") )
      arvFile[strlen(arvFile)-4] = '\0';

  fs::path full_path( fs::initial_path<fs::path>() );
  full_path = fs::system_complete( fs::path( argv[1] ) );
  if ( !fs::exists( full_path ) )
  {
    std::cout << "\nNot found: " << full_path.string() << std::endl;
    return -2;
  }

  if ( fs::is_directory( full_path ) )
  {
    //*********** initialisation using the first image file ***********//
    char fileName[2048];
    long long timestamp = 0;
    IplImage* pImg = NULL;
    streamHeader tmpStreamHeader;
    std::vector<streamHeader> streamHeaderVector;
    ALVideo videoFile;
    bool isInitialized = false;


    fs::directory_iterator end_iter;
    for ( fs::directory_iterator dir_itr( full_path ); dir_itr != end_iter; ++dir_itr )
    {
      std::string fileName = ( full_path / dir_itr->path().filename() ).string();
      if ( fs::is_regular_file( dir_itr->status() ) )
      {
        try {
          pImg = cvLoadImage(fileName.c_str(), 1);
        }
        catch ( const std::exception & ex ) {
          std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl;
        }

        if(pImg)
        {
          std::cout<< "File : " << fileName << std::endl;

          if( isInitialized == false )
          {
            tmpStreamHeader.width      = pImg->width;
            tmpStreamHeader.height     = pImg->height;
            tmpStreamHeader.colorSpace = kYUV422ColorSpace;
            tmpStreamHeader.pixelDepth = 8;

            streamHeaderVector.push_back(tmpStreamHeader);

            std::cout<<"Output arv file properties: "<< streamHeaderVector[0].width <<"x"<< streamHeaderVector[0].height
              <<" Colorspace id:"<< streamHeaderVector[0].colorSpace <<" Pixel depth:"<< streamHeaderVector[0].pixelDepth
              <<std::endl;

            if( !videoFile.recordVideo( arvFile, 0, streamHeaderVector ) )
            { std::cout<<"Error writing "<< arvFile <<" file."<<std::endl;
              return -3;
            }
            isInitialized = true;
          }

          if(  (pImg->width == tmpStreamHeader.width)
            && (pImg->height == tmpStreamHeader.height)
            && (pImg->nChannels == 3) )
          {
            videoFile.write(pImg, timestamp, 0);
            timestamp += 66000;
          }

          cvReleaseImage(&pImg);
        }
      }
    }

    if( isInitialized )
      videoFile.closeVideo();
  }

  return 0;
}

Source: CMakeLists.txt

Download CMakeLists.txt.

cmake_minimum_required(VERSION 2.6.4 FATAL_ERROR)
project(Img2Arv)
include("qibuild.cmake")

qi_create_bin(img2arv img2arv.cpp)
qi_use_lib(img2arv ALVISION OPENCV BOOST_FILESYSTEM)

Note

When running CMake, you might have to replace the default OpenCV libraries by the ones on your system. To do that, switch to Advanced view in Cmake-GUI and replace the arguments of OPENCV_INCLUDE_DIR and OPENCV_LIBRARIES with the OpenCV paths of your system.