Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasBackwardMapping.cpp
1 #include <Base/Image/Image.hh>
2 #include <Base/Image/ImageIO.hh>
3 #include <Geometry/Projection.hh>
4 #include <Geometry/ProjectionParametersFactory.hh>
5 #include <Geometry/ProjectionParametersPerspective.hh>
6 #include <Geometry/ProjectionParametersSpherical.hh>
7 #include <Geometry/ProjectionParametersCylindric.hh>
8 #include <Utils/Param.hh>
9 #include <cmath>
10 
11 using namespace std;
12 using namespace BIAS;
13 
14 /** @file biasBackwardMapping
15  @brief Conversion between different projections using backward mapping.
16  @todo Use BIAS::BackwardMapping class instead of manual backwards mapping!
17  @ingroup g_tools
18  @author esquivel 11/2011
19  */
20 
21 int main(int argc, char *argv[])
22 {
23  if (argc < 4) {
24  cout << "Usage: " << argv[0] << " <input image file> [<output image>] "
25  << "<source projection> <output projection>" << endl;
26  exit(0);
27  }
28 
29  // Load source image
31  if (BIAS::ImageIO::Load(argv[1], srcImage) != 0) {
32  cerr << "Failed to load image \"" << argv[1] << "\"!" << endl;
33  exit(0);
34  }
35 
36  // Create projection factory
38 
39  // Create source projection
40  const std::string srcFile = argv[argc > 4 ? 3 : 2];
41  BIAS::ProjectionParametersBase *srcProj = factory.Load(srcFile);
42  if (srcProj == NULL) {
43  // Load source projection from file
44  BIAS::Projection proj;
45  if (proj.Load(srcFile) != 0) {
46  cerr << "Failed to load source projection from \"" << srcFile << "\"!" << endl;
47  exit(0);
48  } else if (proj.Size() != 1) {
49  cerr << "Source projection contains " << proj.Size() << " parameters!" << endl;
50  exit(0);
51  }
52  srcProj = proj.GetParameterCloneWithAbsolutePose();
53  }
54  BIAS::Pose srcPose; // set canonical pose
55  srcProj->SetPose(srcPose);
56 
57  // Read properties of source projection
58  unsigned int srcWidth, srcHeight;
59  const unsigned int srcChannels = srcImage.GetChannelCount();
60  srcProj->GetImageSize(srcWidth, srcHeight);
61  if (srcWidth != srcImage.GetWidth() || srcHeight != srcImage.GetHeight()) {
62  cerr << "Source image and source projection have different dimensions!" << endl;
63  exit(0);
64  }
65 
66  // Create output projection
67  const std::string outFile = argv[argc > 4 ? 4 : 3];
68  BIAS::ProjectionParametersBase *outProj = factory.Load(outFile);
69  if (outProj == NULL) {
70  // Load output projection from file
71  BIAS::Projection proj;
72  if (proj.Load(outFile) != 0) {
73  cerr << "Failed to load output projection from \"" << outFile << "\"!" << endl;
74  exit(0);
75  } else if (proj.Size() != 1) {
76  cerr << "Output projection contains " << proj.Size() << " parameters!" << endl;
77  exit(0);
78  }
79  outProj = proj.GetParameterCloneWithAbsolutePose();
80  }
81  BIAS::Pose outPose; // set canonical pose
82  outProj->SetPose(outPose);
83 
84  // Read properties of output projection
85  unsigned int outWidth, outHeight;
86  const unsigned int outChannels = srcChannels;
87  outProj->GetImageSize(outWidth, outHeight);
88 
89  // Create output image
90  BIAS::Image<unsigned char> outImage(outWidth, outHeight, outChannels);
91  outImage.Clear(0);
92 
94 
95  // ----- BACKWARD MAPPING -----
96 
97  // Map source to output image
98  for (unsigned int u = 0; u < outWidth; u++) {
99  for (unsigned int v = 0; v < outHeight; v++) {
100  // compute ray for pixel (u,v) in output image
102  outProj->UnProjectToRay(BIAS::HomgPoint2D(u,v), p, ray);
103  if (ray.NormL2() == 0.0) continue;
104  // project ray into input image
105  BIAS::HomgPoint2D srcPos = srcProj->Project(BIAS::HomgPoint3D(ray));
106  if (!srcPos.IsAtInfinity()) {
107  // set pixel in output image
108  if ((int)srcPos[0] >= 0 && (int)srcPos[0] < (int)srcWidth &&
109  (int)srcPos[1] >= 0 && (int)srcPos[1] < (int)srcHeight) {
110  for (unsigned int c = 0; c < outChannels; c++)
111  outImage.SetPixel(srcImage.PixelValue((int)srcPos[0] % srcWidth, (int)srcPos[1], (unsigned short)c),
112  u, v, (unsigned short)c);
113  }
114  }
115  }
116  }
117 
118  // Write output image to file
119  const std::string outFilename = (argc > 4 ? argv[2] : "output.png");
120  if (BIAS::ImageIO::Save(outFilename, outImage) != 0) {
121  cerr << "Failed to save output image to \"" << outFilename << "\"!" << endl;
122  }
123 
124  // Release memory
125  delete srcProj;
126  delete outProj;
127 
128  return 0;
129 }
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
Definition: Projection.cpp:62
unsigned int GetWidth() const
Definition: ImageBase.hh:312
virtual void UnProjectToRay(const HomgPoint2D &pos, Vector3< double > &origin, Vector3< double > &direction, bool ignoreDistortion=false) const
Calculates the view ray, which belongs to the given position on the image plane, in global coordinate...
StorageType PixelValue(const unsigned int x, const unsigned int y, const unsigned short int channel=0) const
Returns value of pixel at specific position, using specific channel as offset.
Definition: Image.hh:91
virtual HomgPoint2D Project(const HomgPoint3D &X, bool IgnoreDistortion=false) const
calculates the projection of a point in the world coordinate system to a pixel in the image plane of ...
virtual void SetPose(const BIAS::Pose pose)
Set pose from pose object.
ProjectionParametersBase * Load(const std::string &fileName)
Loads a projection parameters object from a file.
Represents 3d pose transformations, parametrized as Euclidean translation and unit quaternion orienta...
Definition: Pose.hh:73
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
bool IsAtInfinity() const
Definition: HomgPoint2D.hh:165
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
unsigned int GetHeight() const
Definition: ImageBase.hh:319
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
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
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
unsigned int Size() const
Determine number of ProjectionParameterBase pointers in Projection.
Definition: Projection.hh:178
Camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
ProjectionParametersBase * GetParameterCloneWithAbsolutePose(unsigned int cam=0) const
Allocates memory for parameters base type and returns pointer to it!
Definition: Projection.hh:218
double NormL2() const
the L2 norm sqrt(a^2 + b^2 + c^2)
Definition: Vector3.hh:633