Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raulmur committed Mar 6, 2015
0 parents commit c5d02c9
Show file tree
Hide file tree
Showing 6 changed files with 369 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

project(BagFromImages)

rosbuild_init()

FIND_PACKAGE(OpenCV REQUIRED)

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

rosbuild_add_executable(${PROJECT_NAME}
main.cc
Thirdparty/DLib/FileFunctions.cpp)

target_link_libraries (${PROJECT_NAME}
${OpenCV_LIBS})
124 changes: 124 additions & 0 deletions Thirdparty/DLib/FileFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* File: FileFunctions.cpp
* Author: Dorian Galvez-Lopez
* Date: June 2009
* Description: file system functions
* License: see the LICENSE.txt file
*
*/

#include "FileFunctions.h"
#include <vector>
#include <string>
#include <fstream>
#include <cstdio>
#include <algorithm>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#define mkdir(a) mkdir(a, 0755)


using namespace std;
using namespace DUtils;

void FileFunctions::MkDir(const char *path)
{
mkdir(path);
}

void FileFunctions::RmDir(const char *path)
{
// empty path
vector<string> files = FileFunctions::Dir(path, "");
for(vector<string>::iterator it = files.begin(); it != files.end(); it++){
remove(it->c_str());
}
rmdir(path);
}

void FileFunctions::RmFile(const char *path)
{
remove(path);
}

bool FileFunctions::FileExists(const char *filename)
{
std::fstream f(filename, ios::in);

if(f.is_open()){
f.close();
return true;
}else
return false;
}

bool FileFunctions::DirExists(const char *path)
{
DIR *dirp;
if((dirp = opendir(path)) != NULL){
closedir(dirp);
return true;
}else
return false;
}

std::vector<std::string> FileFunctions::Dir(const char *path, const char *right,
bool sorted)
{
DIR *dirp;
struct dirent *entry;
vector<string> ret;

if((dirp = opendir(path)) != NULL){
while((entry = readdir(dirp)) != NULL){
string name(entry->d_name);
string r(right);
if((name.length() >= r.length()) &&
(name.substr(name.length() - r.length()).compare(r) == 0))
{
ret.push_back(string(path) + "/" + entry->d_name);
}
}
closedir(dirp);
}

if(sorted) sort(ret.begin(), ret.end());

return ret;
}
std::string FileFunctions::FileName(const std::string filepath)
{
string::size_type p = filepath.find_last_of('/');
string::size_type p2 = filepath.find_last_of('\\');
if(p2 != string::npos && p2 > p) p = p2;
return filepath.substr(p+1);
}

void FileFunctions::FileParts(const std::string filepath, std::string &path,
std::string &filename, std::string &ext)
{
string::size_type p = filepath.find_last_of('/');
string::size_type p2 = filepath.find_last_of('\\');
if(p == string::npos || (p2 != string::npos && p2 > p)) p = p2;

std::string filext;

if(p == string::npos){
path = "";
filext = filepath;
}else{
path = filepath.substr(0, p);
filext = filepath.substr(p+1);
}

p = filext.find_last_of('.');
if(p == string::npos){
filename = filext;
ext = "";
}else{
filename = filext.substr(0, p);
ext = filext.substr(p+1);
}
}

87 changes: 87 additions & 0 deletions Thirdparty/DLib/FileFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* File: FileFunctions.h
* Author: Dorian Galvez-Lopez
* Date: June 2009
* Description: file system functions
* License: see the LICENSE.txt file
*
*/

#ifndef __D_FILE_FUNCTIONS__
#define __D_FILE_FUNCTIONS__

#pragma once

#include <vector>
#include <string>

namespace DUtils {

/// General functions to manipulate directories and files
class FileFunctions
{
public:

/**
* Creates the directory 'path'. The parent directory must exist
* @param path
*/
static void MkDir(const char *path);

/**
* Removes a directory and its content
* @param path
*/
static void RmDir(const char *path);

/**
* Removes a file
* @param path
*/
static void RmFile(const char *path);

/**
* Checks the existence of a folder
* @return true iff the directory exists
*/
static bool DirExists(const char *path);

/**
* Checks the existence of a file
* @returns true iff the file exists
*/
static bool FileExists(const char *filename);

/**
* Returns the relative path of the files located in the path given and
* whose right path of the name matches 'right'
* @param path: path to directory
* @param right: string like "_L.png"
* @param sorted: if true, the files are sorted alphabetically
* @return path list
*/
static std::vector<std::string> Dir(const char *path, const char *right,
bool sorted = false);

/**
* Extracts the filename of the given path
* @param filepath path
* @return file name
*/
static std::string FileName(const std::string filepath);

/**
* Extracts the path, file name and extension of the given path
* @param filepath
* @param path (out): path to file
* @param filename (out): filename without extension or dot
* @param ext (out): extension without dot
*/
static void FileParts(const std::string filepath, std::string &path,
std::string &filename, std::string &ext);

};

}

#endif
58 changes: 58 additions & 0 deletions Thirdparty/DLib/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Copyright (c) 2015 Dorian Galvez-Lopez. http://doriangalvez.com
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.
3. The original author of the work must be notified of any
redistribution of source code or in binary form.
4. Neither the name of copyright holders nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

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 COPYRIGHT HOLDERS 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.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This software includes the dirent API for Microsoft Visual Studio, by
Toni Ronkko, and has its own license, reproduced below:

/*****************************************************************************
* dirent.h - dirent API for Microsoft Visual Studio
*
* Copyright (C) 2006 Toni Ronkko
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* ``Software''), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL TONI RONKKO BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************/
67 changes: 67 additions & 0 deletions main.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include<iostream>
#include<ros/ros.h>
#include<rosbag/bag.h>
#include<rosbag/view.h>
#include<sensor_msgs/Image.h>
#include<std_msgs/Time.h>
#include<std_msgs/Header.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <sensor_msgs/image_encodings.h>

#include "Thirdparty/DLib/FileFunctions.h"


using namespace std;

int main(int argc, char **argv)
{
ros::init(argc, argv, "BagFromImages");

if(argc!=5)
{
cerr << "Usage: rosrun BagFromImages BagFromImages <path to image directory> <image extension .ext> <frequency> <path to output bag>" << endl;
return 0;
}

ros::start();

// Vector of paths to image
vector<string> filenames =
DUtils::FileFunctions::Dir(argv[1], argv[2], true);

cout << "Images: " << filenames.size() << endl;

// Frequency
double freq = atof(argv[3]);

// Output bag
rosbag::Bag bag_out(argv[4],rosbag::bagmode::Write);

ros::Time t = ros::Time::now();

const float T=1.0f/freq;
ros::Duration d(T);

for(size_t i=0;i<filenames.size();i++)
{
if(!ros::ok())
break;

cv::Mat im = cv::imread(filenames[i],CV_LOAD_IMAGE_COLOR);
cv_bridge::CvImage cvImage;
cvImage.image = im;
cvImage.encoding = sensor_msgs::image_encodings::RGB8;
cvImage.header.stamp = t;
bag_out.write("/camera/image_raw",ros::Time(t),cvImage.toImageMsg());
t+=d;
cout << i << " / " << filenames.size() << endl;
}

bag_out.close();

ros::shutdown();

return 0;
}
16 changes: 16 additions & 0 deletions manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<package>
<description brief="BagFromImages">

BagFromImages

</description>
<author>Raul Mur-Artal</author>
<depend package="roscpp"/>
<depend package="sensor_msgs"/>
<depend package="std_msgs"/>
<depend package="rosbag"/>
<depend package="cv_bridge"/>
<depend package="image_transport"/>
</package>


0 comments on commit c5d02c9

Please sign in to comment.