Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasjoinimages.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Image/Image.hh>
26 #include <stdio.h>
27 #include <map>
28 #include <string>
29 #include <Base/Image/ImageIO.hh>
30 #include <Base/Image/ImageConvert.hh>
31 #include <Utils/Param.hh>
32 #include <Filter/Rescale.hh>
33 #include <Utils/IOUtils.hh>
34 
35 using namespace BIAS;
36 using namespace std;
37 
38 BIAS::Param params;
39 
40 void usage()
41 {
42  cout<<"Takes up to four image lists and creates new images containing all side by side"<<endl;
43  cout<<"\n\n biasjoinimages imagelist1 imagelist2 <imagelist3 imagelist4>\n\n"<<endl;
44  params.Usage();
45 }
46 
47 /**
48  @file
49  @ingroup g_tools
50  @brief Joins up to 4 images for video production. See biasjoinimages.cpp
51  @author ischiller
52 */
53 int main (int argc, char *argv[])
54 {
55  int ret = 0;
56  bool bfourImages = false;
57  string *list1 = params.AddParamString("list1","First list of images","",'1');
58  string *list2 = params.AddParamString("list2","Second list of images","",'2');
59  string *list3 = params.AddParamString("list3","Third list of images","",'3');
60  string *list4 = params.AddParamString("list4","Forth list of images","",'4');
61  double *rescaleFactor = params.AddParamDouble("RescaleFactor","Rescale factor, 2 means half size",1.0,0.0,10000,'r');
62 
63  bool r = IOUtils::ParseCommandLineEvalHelp(params,argc,argv);
64  if(!r || *list1 == "" ||*list1 == ""){
65  usage(); return 0;
66  }
67 
68  std::vector<std::string> images1,images2;
69  ret = Param::ParseListFile(*list1, images1);
70  if(ret < 0){ BIASERR("Could not parse list file"<<*list1); return 0;}
71  ret = Param::ParseListFile(*list2, images2);
72  if(ret < 0){ BIASERR("Could not parse list file"<<*list2); return 0;}
73 
74  std::vector<std::string> images3, images4;
75  unsigned int width, height;
76 
77  if(*list3 != "" && *list4!=""){
78  bfourImages = true;
79  ret = Param::ParseListFile(*list3, images3);
80  if(ret < 0){ BIASERR("Could not parse list file"<<*list3); return 0;}
81  ret = Param::ParseListFile(*list4, images4);
82  if(ret < 0){ BIASERR("Could not parse list file"<<*list4); return 0;}
83  }
84 
85  BIAS::ImageBase image1,image2,image3,image4;
86  BIAS::Image<unsigned char> imageUC,image1UC,image2UC,image3UC,image4UC;
87  BIAS::Image<unsigned char> image11,image22,image33,image44;
89  int dImageIndex =102;
90  for(unsigned int i=0;i< images1.size();i++){
91  //Clean image to enable reinit
92  if(!destImage.IsEmpty()) destImage.Release();
93 
94  cout<<"Image "<< dImageIndex<<endl;
95  BIAS::ImageIO::Load(images1[i], image1);
96  BIAS::ImageIO::Load(images2[i], image2);
97  //save dimension
98  width = image1.GetWidth();
99  height = image1.GetHeight();
100  //convert ST
102  (BIAS::Image<float>(image1)).ScaleShiftBetween(0,255);
104  }
105  else
106  image11 = image1;
107 
108  // 2. image
110  ((BIAS::Image<float>)(image2)).ScaleShiftBetween(0,255);
112  }
113  else
114  image22 = image2;
115 
116  // rescale all images to same size
117  if(image22.GetWidth() != width || image22.GetHeight() != height){
119  rescale.Upsample(image22,image2UC,width,height);
120  image22 = image2UC;
121  }
122 
123  //Convert all to RGB
124  BIAS::ImageConvert::ToRGB(image11,imageUC);
125  BIAS::ImageBase::EColorModel colorModel = imageUC.GetColorModel();
126  BIAS::ImageConvert::Convert(image22, image2UC,colorModel);
127 
128  //////////////////////////////////////////////////////////////////
129  if(bfourImages){
130  BIAS::ImageIO::Load(images3[i], image3);
131  BIAS::ImageIO::Load(images4[i], image4);
133  ((BIAS::Image<unsigned char>)(image3)).ScaleShiftBetween(0,255);
135  }
136  else image33 = image3;
137 
138  if(image33.GetWidth() != width || image33.GetHeight() != height){
140  rescale.Upsample(image33,image3UC,width,height);
141  image33 = image3UC;
142  }
143 
144 
146  ((BIAS::Image<unsigned char>)(image4)).ScaleShiftBetween(0,255);
148  }
149  else image44 = image4;
150 
151  if(image44.GetWidth() != width || image44.GetHeight() != height){
153  rescale.Upsample(image44,image4UC,image44.GetWidth()*4,image44.GetHeight()*4);
154  image44 =image4UC;
155  }
156 
157  BIAS::ImageConvert::Convert(image33, image3UC,colorModel);
158  BIAS::ImageConvert::Convert(image44, image4UC,colorModel);
159 
160  int destW = 0;
161  int destH = 0;
162  //width
163  if((image11.GetWidth() + image22.GetWidth()) > (image33.GetWidth()+ image44.GetWidth()) ){
164  destW = image11.GetWidth() + image22.GetWidth();
165  }
166  else
167  destW = image33.GetWidth()+ image44.GetWidth();
168 
169  //height
170  if((image11.GetHeight() + image33.GetHeight()) >(image22.GetHeight()+ image44.GetHeight()) ){
171  destH = image11.GetHeight() + image33.GetHeight();
172  }
173  else
174  destH = image22.GetHeight()+ image44.GetHeight();
175 
176  destImage.Init(destW,destH,3);
177  }//bfourimages
178  else{
179  destImage.Init(image11.GetWidth()+image22.GetWidth(),
180  image11.GetHeight(),3);
181  }
182 
183  //paste images to new image
184  unsigned ulx=0,uly=0,lrx=image11.GetWidth(),lry=image11.GetHeight();
185  int xoffset1=0, yoffset1=0;
186 
187  destImage.SetROICorners(ulx,uly,lrx,lry);
188  destImage.Paste2ROI(imageUC);
189 
190 
191  xoffset1 = (image11.GetWidth() - image22.GetWidth())/2;
192  yoffset1 = (image11.GetHeight() - image22.GetHeight())/2;
193  //cout<<"Xoffset:"<<xoffset1<<endl;
194  //cout<<"Yoffset:"<<yoffset1<<endl;
195 
196  ulx = image11.GetWidth()+xoffset1;
197  uly = yoffset1;
198  lrx = image11.GetWidth()+image22.GetWidth()+xoffset1;
199  lry = image22.GetHeight()+yoffset1;
200 
201 // ulx = xoffset1;
202 // uly = image11.GetHeight()+ yoffset1;
203 // lrx = image11.GetWidth()+xoffset1;
204 // lry = image11.GetHeight()+image22.GetHeight()+yoffset1;
205  destImage.SetROICorners(ulx,uly,lrx,lry);
206 
207  destImage.Paste2ROI(image2UC);
208 
209  if(bfourImages){
210  unsigned ulx=0,uly=0,lrx=0,lry=0;
211  //int xoffset2=0, yoffset2=0;
212  ulx = 0;
213  uly = image11.GetHeight();
214  lrx = image33.GetWidth();
215  lry = image11.GetHeight()+image33.GetHeight();
216 
217  destImage.SetROICorners(ulx,uly,lrx,lry);
218  destImage.Paste2ROI(image3UC);
219 
220  //xoffset2 = (image33.GetWidth() - image44.GetWidth())/2;
221  //cout<<"Xoffset2:"<<xoffset2<<endl;
222  //yoffset2 = (image33.GetHeight() - image44.GetHeight())/2;
223  //cout<<"Yoffset2:"<<yoffset2<<endl;
224 
225  ulx = image33.GetWidth()+65;
226  uly = image11.GetHeight()+150;
227  lrx = image33.GetWidth()+image44.GetWidth()+65;
228  lry = image11.GetHeight()+image44.GetHeight()+150;
229  //cout<<"UL:"<<ulx<<","<<uly<<endl;
230  //cout<<"LR:"<<lrx<<","<<lry<<endl;
231 
232  destImage.SetROICorners(ulx,uly,lrx,lry);
233 
234  destImage.Paste2ROI(image4UC);
235 
236  }
237  dImageIndex++;
238 
239  if(*rescaleFactor != 1.0){
242  tmp.SetColorModel(colorModel);
243  destImage.UnsetROI();
244  rescaler.SetFactor(*rescaleFactor);
245  rescaler.Filter(destImage,tmp);
246  stringstream name2;
247  name2<<"joined_rescale"<<BIAS::FileHandling::LeadingZeroString(dImageIndex,5)<<".png";
248 
249  BIAS::ImageIO::Save(name2.str(),tmp);
250  cout<<"saved:"<<name2.str()<<endl;
251  }
252  else{
253  stringstream name;
254  name<<"joined_"<<BIAS::FileHandling::LeadingZeroString(dImageIndex,5)<<".png";
255  BIAS::ImageIO::Save(name.str(),destImage);
256  cout<<"saved:"<<name.str()<<endl;
257  }
258  }
259 
260 }
261 
262 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
double * AddParamDouble(const std::string &name, const std::string &help, double deflt=0.0, double min=-DBL_MAX, double max=DBL_MAX, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:351
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
scales the src to dst using the downsampling factor from SetFactor()
Definition: Rescale.cpp:89
static std::string LeadingZeroString(const int &n, const unsigned int &digits=DEFAULT_LEADING_ZEROS)
Create a string with leading zeroes from number.
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int Upsample(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
generic upsample function
Definition: Rescale.cpp:1157
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
Definition: Param.cpp:1853
static bool ParseCommandLineEvalHelp(Param &params, int argc, char *argv[])
parses the command line, adds parameter &quot;help&quot;
Definition: IOUtils.cpp:176
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
Definition: Image.cpp:421
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
This class Param provides generic support for parameters.
Definition: Param.hh:231
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
(8bit) unsigned char image storage type
Definition: ImageBase.hh:112
int Paste2ROI(const ImageBase &Image)
paste Image to current ROI
Definition: ImageBase.cpp:603
int SetROICorners(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
Definition: ImageBase.cpp:1048
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:327
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
void SetFactor(double factor)
the downsampling factor
Definition: Rescale.hh:361
void UnsetROI()
deprecated, use GetROI()-&gt;UnsetROI()
Definition: ImageBase.cpp:1057