Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasbayer2rgb.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 <Base/Image/ImageConvert.hh>
27 #include <Base/Image/ImageIO.hh>
28 #include <Image/Camera.hh>
29 #include <Utils/IOUtils.hh>
30 #include <Utils/Param.hh>
31 #include <Base/Common/FileHandling.hh>
32 
33 #include <Image/ConvertHDR.hh>
34 
35 using namespace std;
36 using namespace BIAS;
37 
38 /**
39  @file
40  @ingroup g_tools
41  @brief convert a (hdr) bayer image to unsigned char rgb, see biasbayer2rgb.cpp
42  @author bartczak 12/2008
43  */
44 
45 int main(int argc, char* argv[])
46 {
47  // Parse parameters
48  Param *p = new Param();
49  p->AddParamString("image", "Image in Bayer pattern", "", 'i');
50  p->AddParamString("imageList", "List of images in Bayer pattern", "", 'l');
51  p->AddParamString("bayer", "Bayer pattern (e.g. RGGB)", "", 'b');
52  p->AddParamInt("colorDepth", "Specify color depth (e.g. use 255 for 8 bit image)", 255);
53  if (!IOUtils::ParseCommandLineEvalHelp(*p, argc, argv)) {
54  return -1;
55  }
56 
57  // Read image filenames
58  string imageFileName = *(p->GetParamString("image"));
59  string imageListName = *(p->GetParamString("imageList"));
60  string bayer = *(p->GetParamString("bayer"));
61  int colorDepth = *(p->GetParamInt("colorDepth"));
62  vector<string> imageNames;
63  if (!imageListName.empty()) {
64  if (Param::ParseListFile(imageListName, imageNames) != 0) {
65  BIASERR("Failed to load image list file " << imageListName << "!");
66  return -1;
67  } else {
68  cout << "Loaded image list file " << imageListName << endl;
69  }
70  } else if (!imageFileName.empty()) {
71  imageNames.push_back(imageFileName);
72  } else {
73  p->Usage();
74  return 0;
75  }
76 
77  // Parse Bayer pattern
79  if (strcmp(bayer.c_str(), "BGGR") == 0) {
80  cout << "Converting Bayer pattern BGGR to RGB" << endl;
81  pattern = ImageBase::CM_Bayer_BGGR;
82  } else if (strcmp(bayer.c_str(), "GBRG") == 0) {
83  cout << "Converting Bayer pattern GBRG to RGB" << endl;
84  pattern = ImageBase::CM_Bayer_GBRG;
85  } else if (strcmp(bayer.c_str(), "GRBG") == 0) {
86  cout << "Converting Bayer pattern GRBG to RGB" << endl;
87  pattern = ImageBase::CM_Bayer_GRBG;
88  } else if (strcmp(bayer.c_str(), "RGGB") == 0) {
89  cout << "Converting Bayer pattern RGGB to RGB" << endl;
90  pattern = ImageBase::CM_Bayer_RGGB;
91  } else {
92  cout << "Unknown Bayer pattern " << bayer << " given!" << endl;
93  return -1;
94  }
95 
96  // Convert images
97  for (unsigned int i = 0; i < imageNames.size(); i++)
98  {
99  string imageName = imageNames[i];
100  string newImageName = "rgb_" + imageName;
101  ImageBase tmp;
102  if (ImageIO::Load(imageName, tmp) != 0) {
103  BIASERR("Failed to load image " << imageName << "!");
104  return -1;
105  }
106 
107  // convert HDR image
108  if (colorDepth > 256)
109  {
110  // convert HDR image to float image
111  Image<float> hdrFloat;
112  ImageConvert::ConvertST(tmp, hdrFloat, ImageBase::ST_float);
113  ConvertHDR<float> hdrconverter;
114  Image<unsigned char> tmpUC;
115  // convert float image to unsigned char image
116  hdrconverter.ToUnsignedCharGamma(hdrFloat, tmpUC, 1.0, colorDepth);
117  tmp = tmpUC;
118  }
119 
120  // convert Bayer image to RGB image
121  if (tmp.GetStorageType() == ImageBase::ST_unsignedchar)
122  {
123  tmp.SetColorModel(pattern);
124 
125  Camera<unsigned char> bayerImage(tmp);
126  Camera<unsigned char> rgbImage;
127  bayerImage.ParseMetaData();
128  ImageConvert::ToRGB(bayerImage, rgbImage);
129  cout << "Converted " << imageName << " : " << bayerImage.GetColorModel()
130  << " ---> " << newImageName << " : " << rgbImage.GetColorModel() << endl;
131  rgbImage.SetMetaData(*bayerImage.GetMetaData());
132  if (ImageIO::Save(newImageName, rgbImage) != 0) {
133  BIASERR("Failed to save RGB image to file " << newImageName << "!");
134  continue;
135  }
136  } else {
137  BIASERR("Bayer image " << imageName << " has invalid storage type "
138  "(unsigned char needed)!");
139  continue;
140  }
141  }
142 
143  return 0;
144 }
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
This class handles conversions of HDR images to for example unsigned char.
Definition: ConvertHDR.hh:47
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
int ToUnsignedCharGamma(BIAS::Image< StorageType > &src, BIAS::Image< unsigned char > &dst, float gamma, unsigned int currentMaxVal)
Conversion to unsigned char image, leaves min/max part intact.
Definition: ConvertHDR.cpp:119
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
std::string * GetParamString(const std::string &name) const
Definition: Param.cpp:649
int * GetParamInt(const std::string &name) const
Definition: Param.cpp:618
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
This class Param provides generic support for parameters.
Definition: Param.hh:231
invalid (not set) image format
Definition: ImageBase.hh:129
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
int * AddParamInt(const std::string &name, const std::string &help, int deflt=0, int min=std::numeric_limits< int >::min(), int max=std::numeric_limits< int >::max(), char cmdshort=0, int Group=GRP_NOSHOW)
For all adding routines:
Definition: Param.cpp:276
void SetMetaData(const MetaData &m)
Definition: ImageBase.hh:470
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
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
Definition: Camera.cpp:154