Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasapplybayer.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 /**
26  @file
27  @ingroup g_tools
28  @brief converts various Bayer pattern images to RGB images, see biasapplybayer.cpp
29  @author bartczak
30  */
31 
32 #include <Base/Image/Image.hh>
33 #include <Base/Image/ImageConvert.hh>
34 #include <Base/Image/ImageIO.hh>
35 #include <Image/Camera.hh>
36 #include <Utils/IOUtils.hh>
37 #include <Utils/Param.hh>
38 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
39 #include <Image/ConvertBayerPattern.hh>
40 #endif
41 
42 using namespace std;
43 using namespace BIAS;
44 
45 bool ApplyHalfingBayerConversion(Image<unsigned char>& in,
47 
48 void PrintHelp() {
49  cout << "use: biasapplybayer [options] in out pattern [projection]" << endl;
50  cout << "in: input image (mip)" << endl;
51  cout << "out: output image (mip)" << endl;
52  cout << "pattern: RGGB, GBRG, ..." << endl;
53  cout << "projection: camera projection params to be set (optional)" << endl;
54 
55 }
56 
57 
58 int main(int argc, char* argv[]) {
59 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
61 #endif
62 
63  Param params(true);
64  bool* half =
65  params.AddParamBool("half",
66  "Discards half of the green pixels. "
67  "No interpolation resulting image has half resolution.",
68  false, 'H');
69  int fup = 0; //first unknown argument index
70  if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv, fup) || fup + 2 >= argc) {
71  PrintHelp();
72  return 0;
73  }
74 
75  Projection proj;
76  bool bsetproj_ = false;
77 
78  if (argc-fup==4) {
79  if (proj.Load(argv[fup+3]) != 0) {
80  BIASERR("error loading projection!");
81  return -1;
82  } else {
83  bsetproj_ = true;
84  }
85  }
86 
89 
90  const int
91  inIndex = fup,
92  outIndex = fup+1,
93  patternIndex = fup+2;
94 
95 
98  if (strcmp(argv[patternIndex], "BGGR")==0) {
99  cout << "converting bayer BGGR to RGB (" << argv[inIndex] << " => " << argv[outIndex] << ")" << endl;
100  pattern = ImageBase::CM_Bayer_BGGR;
101  } else if (strcmp(argv[patternIndex], "GBRG")==0) {
102  cout << "converting bayer GBRG to RGB (" << argv[inIndex] << " => " << argv[outIndex] << ")" << endl;
103  pattern = ImageBase::CM_Bayer_GBRG;
104  } else if (strcmp(argv[patternIndex], "GRBG")==0) {
105  cout << "converting bayer GRBG to RGB (" << argv[inIndex] << " => " << argv[outIndex] << ")" << endl;
106  pattern = ImageBase::CM_Bayer_GRBG;
107  } else if (strcmp(argv[patternIndex], "RGGB")==0) {
108  cout << "converting bayer RGGB to RGB (" << argv[inIndex] << " => " << argv[outIndex] << ")" << endl;
109  pattern = ImageBase::CM_Bayer_RGGB;
110  } else {
111  cout << argv[patternIndex]<<"? i dont know this pattern... abort!" << endl;
112  }
113  ImageIO::Load(argv[inIndex], in);
114 
115  in.SetColorModel(pattern);
116 
117  if(!*half) {
118 #if !defined(WIN32) && defined(BIAS_HAVE_DC1394)
119  out.Release();
120  out.Init(in.GetWidth(), in.GetHeight(), 3);
121  if (in.GetChannelCount() == 1){
122  demosaic.Convert(in, out);
123  cout<<"Schoen!"<<endl;
124  }
125  else
126  ImageConvert::ToRGB(in, out);
127  out.SetMetaData(*in.GetMetaData());
128 #else
129  ImageConvert::ToRGB(in, out);
130 #endif
131 
132  } else {
133  if(!ApplyHalfingBayerConversion(in, out)) {
134  return -1;
135  }
136  proj.Rescale(2.0);
137  }
138 
139  if (bsetproj_) {
140  if (out.SetProj(proj)!=0) {
141  cout << "warning: cannot set projection parameters" << endl;
142  }
143  out.UpdateMetaData();
144  }
145  // IOUtils::SaveCamera(argv[outIndex], out);
146 
147  ImageIO::Save(argv[outIndex], out);
148 
149  return 0;
150 }
151 
152 
153 bool ApplyHalfingBayerConversion(Image<unsigned char>& in,
155 {
156 
157  unsigned int width = in.GetWidth();
158  unsigned int height = in.GetHeight();
159 
160  bool even = (width%2 == 0) && (height%2 == 0);
161  if(!even) {
162  BIASERR("cannot half this resolution!");
163  return false;
164  }
165  unsigned int sinkWidth = width/2;
166  unsigned int sinkHeight = height/2;
167  out.Release();
168  out.Init(sinkWidth, sinkHeight, 3);
169 
170  int offsetR, offsetB;
171  int index = 0, indexG = 0;
173  switch(pattern) {
174  case ImageBase::CM_Bayer_BGGR :
175  indexG = 1;
176  offsetR = width;
177  offsetB = -1;
178  break;
179  case ImageBase::CM_Bayer_RGGB :
180  indexG = 1;
181  offsetR = -1;
182  offsetB = width;
183  break;
184  case ImageBase::CM_Bayer_GBRG :
185  indexG = 0;
186  offsetR = width;
187  offsetB = +1;
188  break;
189  case ImageBase::CM_Bayer_GRBG :
190  indexG = 0;
191  offsetR = +1;
192  offsetB = width;
193  break;
194  default :
195  BIASERR("unknown bayer");
196  return false;
197  break;
198  }
199 
200  unsigned char* sinkPixels = out.GetImageData();
201  unsigned char* grabedPixels = in.GetImageData();
202 
203  for(unsigned int j = 0; j < sinkHeight; j++) {
204  for(unsigned int i = 0; i < sinkWidth; i++) {
205  // R
206  sinkPixels[index++] = grabedPixels[indexG+offsetR];
207  // G
208  sinkPixels[index++] = grabedPixels[indexG];
209  // B
210  sinkPixels[index++] = grabedPixels[indexG+offsetB];
211  indexG += 2;
212  }//end of over width
213  indexG += width;
214  }//end of over height
215 
216  return true;
217 }
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void Release()
reimplemented from ImageBase
Definition: Image.cpp:1579
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
void SetColorModel(EColorModel Model)
Definition: ImageBase.hh:561
MetaData * GetMetaData()
Definition: ImageBase.hh:456
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
void Rescale(float ratio, unsigned int cam=0)
adapt internal params to resampled image
Definition: Projection.hh:471
unsigned int GetWidth() const
Definition: ImageBase.hh:312
Wrapper class to dc1394 bayer conversion class.
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
int SetProj(const Projection &Proj)
Definition: Camera.hh:106
unsigned int GetHeight() const
Definition: ImageBase.hh:319
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
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
Definition: Image.hh:137
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
This class Param provides generic support for parameters.
Definition: Param.hh:231
invalid (not set) image format
Definition: ImageBase.hh:129
void SetMetaData(const MetaData &m)
Definition: ImageBase.hh:470
int Convert(Image< StorageType > &src, Image< StorageType > &dst, BayerDemosaicMethod method=BAYER_DEMOSAIC_METHOD_AHD, ImageBase::EColorModel bayer_pattern=ImageBase::CM_invalid)
converts image with bayerpattern to rgb.