Simplified rtabmap-imagesJoiner tools

This commit is contained in:
Mathieu Labbe
2015-01-30 18:07:28 -05:00
parent 1fe2a9ed95
commit 9d20403930
3 changed files with 60 additions and 72 deletions

View File

@@ -28,6 +28,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "rtabmap/utilite/ULogger.h" #include "rtabmap/utilite/ULogger.h"
#include "rtabmap/utilite/UTimer.h" #include "rtabmap/utilite/UTimer.h"
#include "rtabmap/utilite/UDirectory.h" #include "rtabmap/utilite/UDirectory.h"
#include "rtabmap/utilite/UFile.h"
#include "rtabmap/utilite/UConversion.h" #include "rtabmap/utilite/UConversion.h"
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp> #include <opencv2/highgui/highgui.hpp>
@@ -35,97 +36,70 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
void showUsage() void showUsage()
{ {
printf("Usage:\n" printf("Usage:\n"
"imagesJoiner.exe \"# (see below)\" \"type\" [option]\n" "imagesJoiner.exe [option] path\n"
" # : Name pattern is how the file names are formatted (in number).\n"
" Examples: 4 for pictures with 0001.jpg, 0002.jpg, ..., 0010.jpg, 0100.jpg, 1000.jpg, ...\n"
" 1 for pictures with 1.jpg, 2.jpg, ..., 10.jpg, 100.jpg, 1000.jpg, ...\n"
" type : is the extension (jpg, bmp, png...)\n"
" Options:\n" " Options:\n"
" -inv option for copying odd images on the right\n" " -inv option for copying odd images on the right\n\n");
" -d # destination filename size\n");
exit(1); exit(1);
} }
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
if(argc < 3) if(argc < 2)
{ {
showUsage(); showUsage();
} }
bool inv = false; bool inv = false;
unsigned int sizeFileName = std::atoi(argv[1]); for(int i=1; i<argc-1; ++i)
int sizeTargetFileName = sizeFileName;
std::string type = argv[2];
for(int i=3; i<argc; ++i)
{ {
if(strcmp(argv[i], "-inv") == 0) if(strcmp(argv[i], "-inv") == 0)
{ {
inv = true; inv = true;
printf(" Inversing option activated...\n"); printf(" Inversing option activated...\n");
continue;
} }
if(strcmp(argv[i], "-d") == 0 && i+1<argc) showUsage();
{ printf(" Not recognized option: \"%s\"\n", argv[i]);
sizeTargetFileName = std::atoi(argv[i+1]);
if(sizeTargetFileName < 0)
{
showUsage();
}
printf(" Target size file name option activated=%d\n",sizeTargetFileName);
++i;
}
} }
printf(" Format = %s\n", argv[1]); std::string path = argv[argc-1];
printf(" Type = %s\n", argv[2]); printf(" Path = %s\n", path.c_str());
std::string targetDirectory = "imagesJoined/"; UDirectory dir(path, "jpg bmp png tiff jpeg");
UDirectory::makeDir((UDirectory::currentDir(true) + "imagesJoined").c_str()); if(!dir.isValid())
{
int counterJoined = 1; printf("Path invalid!\n");
int counterImages = 1; exit(-1);
bool imagesExist = true; }
std::string fileNameA; std::string targetDirectory = path+"_joined";
std::string fileNameB; UDirectory::makeDir(targetDirectory);
printf(" Creating directory \"%s\"\n", targetDirectory.c_str());
while(imagesExist)
std::string fileNameA = dir.getNextFilePath();
std::string fileNameB = dir.getNextFilePath();
int i=1;
while(!fileNameA.empty() && !fileNameB.empty())
{ {
std::string fileNameTarget = uNumber2Str(counterJoined);
if(inv) if(inv)
{ {
fileNameA = uNumber2Str(counterImages+1); std::string tmp = fileNameA;
fileNameB = uNumber2Str(counterImages); fileNameA = fileNameB;
} fileNameB = tmp;
else
{
fileNameA = uNumber2Str(counterImages);
fileNameB = uNumber2Str(counterImages+1);
} }
while(fileNameA.size() < sizeFileName) std::string ext = UFile::getExtension(fileNameA);
{
fileNameA.insert(0, "0");
}
while(fileNameB.size() < sizeFileName) std::string targetFilePath = targetDirectory+UDirectory::separator()+uNumber2Str(i++)+"."+ext;
{
fileNameB.insert(0, "0");
}
while(fileNameTarget.size() < (unsigned int)sizeTargetFileName)
{
fileNameTarget.insert(0, "0");
}
(fileNameTarget.insert(0, targetDirectory) += ".") += type;
(fileNameA += ".") += type;
(fileNameB += ".") += type;
IplImage * imageA = cvLoadImage(fileNameA.c_str(), CV_LOAD_IMAGE_COLOR); IplImage * imageA = cvLoadImage(fileNameA.c_str(), CV_LOAD_IMAGE_COLOR);
IplImage * imageB = cvLoadImage(fileNameB.c_str(), CV_LOAD_IMAGE_COLOR); IplImage * imageB = cvLoadImage(fileNameB.c_str(), CV_LOAD_IMAGE_COLOR);
fileNameA.clear();
fileNameB.clear();
if(imageA && imageB) if(imageA && imageB)
{ {
CvSize sizeA = cvGetSize(imageA); CvSize sizeA = cvGetSize(imageA);
@@ -142,26 +116,28 @@ int main(int argc, char * argv[])
cvCopy( imageB, targetImage ); cvCopy( imageB, targetImage );
cvResetImageROI( targetImage ); cvResetImageROI( targetImage );
if(!cvSaveImage(fileNameTarget.c_str(), targetImage)) if(!cvSaveImage(targetFilePath.c_str(), targetImage))
{ {
printf("Error : saving to \"%s\" goes wrong...\n", fileNameTarget.c_str()); printf("Error : saving to \"%s\" goes wrong...\n", targetFilePath.c_str());
} }
else else
{ {
printf("Saved \"%s\" \n", fileNameTarget.c_str()); printf("Saved \"%s\" \n", targetFilePath.c_str());
} }
cvReleaseImage(&targetImage); cvReleaseImage(&targetImage);
fileNameA = dir.getNextFilePath();
fileNameB = dir.getNextFilePath();
} }
else else
{ {
printf("Error : can't allocated the target image with size (%d,%d)\n", targetSize.width, targetSize.height); printf("Error : can't allocated the target image with size (%d,%d)\n", targetSize.width, targetSize.height);
imagesExist = false;
} }
} }
else else
{ {
imagesExist = false; printf("Error: loading images failed!\n");
} }
if(imageA) if(imageA)
@@ -172,13 +148,8 @@ int main(int argc, char * argv[])
{ {
cvReleaseImage(&imageB); cvReleaseImage(&imageB);
} }
counterJoined++;
counterImages += 2;
} }
printf("%d files processed\n", i-1);
return 0; return 0;
} }

View File

@@ -115,6 +115,12 @@ public:
*/ */
std::string getNextFileName(); std::string getNextFileName();
/**
* Get the next file path.
* @return the next file path
*/
std::string getNextFilePath();
/** /**
* Get all file names. * Get all file names.
* @see UDirectory() * @see UDirectory()

View File

@@ -229,6 +229,17 @@ std::string UDirectory::getNextFileName()
return fileName; return fileName;
} }
std::string UDirectory::getNextFilePath()
{
std::string filePath;
if(iFileName_ != fileNames_.end())
{
filePath = path_+separator()+*iFileName_;
++iFileName_;
}
return filePath;
}
void UDirectory::rewind() void UDirectory::rewind()
{ {
iFileName_ = fileNames_.begin(); iFileName_ = fileNames_.begin();