Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasUndistort.cpp
1 #include <Base/Image/Image.hh>
2 #include <Base/Image/ImageIO.hh>
3 #include <Geometry/Projection.hh>
4 #include <Geometry/ProjectionParametersPerspective.hh>
5 #include <Image/Camera.hh>
6 #include <Image/ProjectionMapping.hh>
7 #include <Utils/Param.hh>
8 
9 using namespace std;
10 using namespace BIAS;
11 
12 /** @file biasUndistort
13  @brief Undistorts an image using backward mapping. Projection image
14  metadata is updating when reading/writing MIP image files.
15  @note Supports only perspective cameras and unsigned char images yet!
16  @ingroup g_tools
17  @author esquivel 02/2013
18  */
19 
20 int main(int argc, char *argv[])
21 {
22  cout << "\n---- biasUndistort ----\n";
23 
24  // Read parameters
25  BIAS::Param params;
26  bool *help = params.AddParamBool("help", "Show information about parameters.", false, 'h');
27  std::string *srcFile = params.AddParamString("input", "Filename of input image (needed)", "", 'i');
28  std::string *outFile = params.AddParamString("output", "Filename of output image (needed)", "", 'o');
29  std::string *projFile = params.AddParamString("proj", "Filename of projection (optional)", "", 'p');
30  params.ParseCommandLine(argc, argv);
31 
32  if (*help || srcFile->empty() || outFile->empty()) {
33  params.Usage();
34  exit(0);
35  }
36 
37  // Load source image
39  if (BIAS::ImageIO::Load(*srcFile, srcImage) != 0) {
40  cout << "[ERROR] Failed to load image from file " << *srcFile << "!\n\n";
41  exit(-1);
42  }
43  srcImage.ParseMetaData();
44 
45  // Load projection or read from metadata
46  BIAS::Projection srcProj;
47  if (!projFile->empty()) {
48  if (srcProj.Load(*projFile) != 0) {
49  cout << "[ERROR] Failed to load projection from file " << *projFile << "!\n\n";
50  exit(-1);
51  } else {
52  cout << "- Loaded projection from file " << *projFile << "\n";
53  }
54  } else if (srcImage.IsProjValid()) {
55  srcProj = srcImage.GetProj();
56  cout << "- Read projection from image metadata\n";
57  } else {
58  cout << "[ERROR] Failed to read projection from image metadata!\n\n";
59  exit(-1);
60  }
63  if (srcProj.Size() != 1 || srcPPP == NULL) {
64  cout << "[ERROR] Invalid projection found! Supports only perspecive cameras!\n\n";
65  exit(-1);
66  }
67 
68  // Determine if projection contains distortion
69  if (!srcPPP->IsDistorted()) {
70  cout << "[ERROR] Image projection contains no distortion!\n\n";
71  exit(-1);
72  }
73 
74  // Create output image
75  unsigned int srcWidth, srcHeight;
76  //const unsigned int srcChannels = srcImage.GetChannelCount();
77  srcPPP->GetImageSize(srcWidth, srcHeight);
78  if (srcWidth != srcImage.GetWidth() || srcHeight != srcImage.GetHeight()) {
79  cout << "[ERROR] Image and projection have different dimensions!\n\n";
80  exit(-1);
81  }
82 
83  // Create output projection
84  BIAS::Projection outProj = srcProj;
87  outPPP->SetUndistortion(0, 0, 0, 0);
88  outPPP->SetDistortionType(DISTYPE_NONE);
89 
90  // Create output image
91  BIAS::Camera<unsigned char> outImage = srcImage;
92  outImage.SetProj(outProj);
93  outImage.UpdateMetaData();
94  outImage.Clear(0);
95 
96  // Perform backward mapping to remove distortion
97  cout << "- Creating undistorted image via backward mapping\n";
99  mapping.SetSourceCam(srcProj);
100  mapping.SetSinkCam(outProj);
101  if (mapping.Map(srcImage, outImage) != 0) {
102  cout << "[ERROR] Failed to undistort image via backward mapping!\n\n";
103  exit(-1);
104  }
105 
106  // Write output image to file
107  if (BIAS::ImageIO::Save(*outFile, outImage) != 0)
108  cout << "[ERROR] Failed to save image to file " << *outFile << "!\n\n";
109  else
110  cout << "- Saved undistorted image to file " << *outFile << "\n\n";
111 
112  return 0;
113 }
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
void SetDistortionType(BIAS_ProjParaPersp_DISTORTION_TYPE distype)
Set type of distortion parameters.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void SetSinkCam(const Projection &P, const Image< float > *sinkdepth=NULL)
Set your sink projection before calling Map(),.
void SetSourceCam(const Projection &P)
Set your source projection before calling Map()
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
Definition: Param.cpp:305
unsigned int GetWidth() const
Definition: ImageBase.hh:312
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
Definition: Param.cpp:1028
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
Definition: Param.cpp:176
void Clear(const StorageType value=0)
sets all pixels to zero/value
Definition: Image.hh:289
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
int SetProj(const Projection &Proj)
Definition: Camera.hh:106
unsigned int GetHeight() const
Definition: ImageBase.hh:319
int Map(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod=MapTrilinear, bool newSink=false, double SuperSampling=1.0)
backward mapping with various interpolations
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
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
int UpdateMetaData()
copy P_ and co.
Definition: Camera.cpp:446
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
bool IsProjValid() const
Definition: Camera.hh:221
unsigned int Size() const
Determine number of ProjectionParameterBase pointers in Projection.
Definition: Projection.hh:178
const BIAS::Projection & GetProj() const
Definition: Camera.hh:109
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