tools/ImagesJoiner/main.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2016, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
29 #include "rtabmap/utilite/UTimer.h"
31 #include "rtabmap/utilite/UFile.h"
33 #include <opencv2/core/core.hpp>
34 #include <opencv2/highgui/highgui.hpp>
35 
36 void showUsage()
37 {
38  printf("Usage:\n"
39  "imagesJoiner.exe [option] path\n"
40  "imagesJoiner.exe path_left path_right\n"
41  " Options:\n"
42  " -inv option for copying odd images on the right\n\n");
43  exit(1);
44 }
45 
46 int main(int argc, char * argv[])
47 {
48  if(argc < 2)
49  {
50  showUsage();
51  }
52 
53  bool inv = false;
54  for(int i=1; i<argc-1; ++i)
55  {
56  if(strcmp(argv[i], "-inv") == 0)
57  {
58  inv = true;
59  printf(" Inversing option activated...\n");
60  continue;
61  }
62  if(argc > 3)
63  {
64  showUsage();
65  printf(" Not recognized option: \"%s\"\n", argv[i]);
66  }
67  }
68 
69  std::string path, pathRight;
70 
71  if(argc == 3 && !inv)
72  {
73  //two paths
74  path = argv[1];
75  pathRight = argv[2];
76 
77  printf(" Path left = %s\n", path.c_str());
78  printf(" Path right = %s\n", pathRight.c_str());
79  }
80  else
81  {
82  path = argv[argc-1];
83  printf(" Path = %s\n", path.c_str());
84  }
85 
86  UDirectory dir(path, "jpg bmp png tiff jpeg");
87  UDirectory dirRight(pathRight, "jpg bmp png tiff jpeg");
88  if(!dir.isValid() || (!pathRight.empty() && !dirRight.isValid()))
89  {
90  printf("Path invalid!\n");
91  exit(-1);
92  }
93 
94  std::string targetDirectory = path+"_joined";
95  UDirectory::makeDir(targetDirectory);
96  printf(" Creating directory \"%s\"\n", targetDirectory.c_str());
97 
98 
99  std::string fileNameA = dir.getNextFilePath();
100  std::string fileNameB;
101  if(dirRight.isValid())
102  {
103  fileNameB = dirRight.getNextFilePath();
104  }
105  else
106  {
107  fileNameB = dir.getNextFilePath();
108  }
109 
110  int i=1;
111  while(!fileNameA.empty() && !fileNameB.empty())
112  {
113  if(inv)
114  {
115  std::string tmp = fileNameA;
116  fileNameA = fileNameB;
117  fileNameB = tmp;
118  }
119 
120  std::string ext = UFile::getExtension(fileNameA);
121 
122  std::string targetFilePath = targetDirectory+UDirectory::separator()+uNumber2Str(i++)+"."+ext;
123 
124  cv::Mat imageA = cv::imread(fileNameA.c_str());
125  cv::Mat imageB = cv::imread(fileNameB.c_str());
126 
127  fileNameA.clear();
128  fileNameB.clear();
129 
130  if(!imageA.empty() && !imageB.empty())
131  {
132  cv::Size sizeA = imageA.size();
133  cv::Size sizeB = imageB.size();
134  cv::Size targetSize(0,0);
135  targetSize.width = sizeA.width + sizeB.width;
136  targetSize.height = sizeA.height > sizeB.height ? sizeA.height : sizeB.height;
137  cv::Mat targetImage(targetSize, imageA.type());
138 
139  cv::Mat roiA(targetImage, cv::Rect( 0, 0, sizeA.width, sizeA.height ));
140  imageA.copyTo(roiA);
141  cv::Mat roiB( targetImage, cv::Rect( sizeA.width, 0, sizeB.width, sizeB.height ) );
142  imageB.copyTo(roiB);
143 
144  if(!cv::imwrite(targetFilePath.c_str(), targetImage))
145  {
146  printf("Error : saving to \"%s\" goes wrong...\n", targetFilePath.c_str());
147  }
148  else
149  {
150  printf("Saved \"%s\" \n", targetFilePath.c_str());
151  }
152 
153  fileNameA = dir.getNextFilePath();
154  if(dirRight.isValid())
155  {
156  fileNameB = dirRight.getNextFilePath();
157  }
158  else
159  {
160  fileNameB = dir.getNextFilePath();
161  }
162  }
163  else
164  {
165  printf("Error: loading images failed!\n");
166  }
167  }
168  printf("%d files processed\n", i-1);
169 
170  return 0;
171 }
static bool makeDir(const std::string &dirPath)
Definition: UDirectory.cpp:333
bool isValid()
Definition: UDirectory.cpp:216
static std::string separator()
Definition: UDirectory.cpp:391
void showUsage()
Some conversion functions.
std::string getExtension()
Definition: UFile.h:140
int main(int argc, char *argv[])
std::string getNextFilePath()
Definition: UDirectory.cpp:232
ULogger class and convenient macros.
std::string UTILITE_EXP uNumber2Str(unsigned int number)
Definition: UConversion.cpp:91


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:37:29