25 #include "Image/ProjectionMapping.hh"
26 #include <Geometry/RMatrix.hh>
27 #include <Base/Geometry/HomgPoint2D.hh>
28 #include <Geometry/Projection.hh>
29 #include <Base/Math/Random.hh>
36 template <
class InputStorageType,
class OutputStorageType>
41 template <
class InputStorageType,
class OutputStorageType>
46 depthMapSink_(NULL), mappingPlane_(0,0,0,1),
47 accountForDifferentCameraCenters_(false)
53 template <
class InputStorageType,
class OutputStorageType>
57 accountForDifferentCameraCenters_(false)
62 template <
class InputStorageType,
class OutputStorageType>
75 template <
class InputStorageType,
class OutputStorageType>
78 unsigned int srcheight,
79 unsigned int sinkwidth,
80 unsigned int sinkheight,
97 std::list<HomgPoint2D> borderpoints;
99 borderpoints.push_back(
HomgPoint2D(srcwidth/2,0,1));
100 borderpoints.push_back(
HomgPoint2D(srcwidth-1,0,1));
102 borderpoints.push_back(
HomgPoint2D(0,srcheight/2,1));
103 borderpoints.push_back(
HomgPoint2D(srcwidth/2,srcheight/2,1));
104 borderpoints.push_back(
HomgPoint2D(srcwidth-1,srcheight/2,1));
106 borderpoints.push_back(
HomgPoint2D(0,srcheight-1,1));
107 borderpoints.push_back(
HomgPoint2D(srcwidth/2,srcheight-1,1));
108 borderpoints.push_back(
HomgPoint2D(srcwidth-1,srcheight-1,1));
110 double dtlx=sinkwidth, dtly=sinkheight, dbrx=-1.0, dbry=-1.0;
117 int countsuccessfulsamples = 0;
118 list<HomgPoint2D>::iterator bit = borderpoints.begin();
119 for (; bit!=borderpoints.end(); bit++) {
121 int mapresult =this->GetSourceCoordinates_(*bit,sinkpoint);
123 BIASWARN(*bit<<
" is mapped to "<<sinkpoint<<
" with an error: "<<mapresult);
127 if (sinkpoint[0]<dtlx) dtlx = sinkpoint[0];
128 if (sinkpoint[1]<dtly) dtly = sinkpoint[1];
129 if (sinkpoint[0]>dbrx) dbrx = sinkpoint[0];
130 if (sinkpoint[1]>dbry) dbry = sinkpoint[1];
131 countsuccessfulsamples++;
136 if (countsuccessfulsamples<7) {
142 BIASWARNONCE(
"approximation of bounding box in sink failed with only "<<countsuccessfulsamples
143 <<
" of 9 samples. Using whole image. Might be slow.");
148 if (dtlx > sinkwidth-1) dtlx = sinkwidth-1;
149 if (dtly > sinkheight-1) dtly = sinkheight-1;
150 if (dbrx > sinkwidth) dbrx = sinkwidth;
151 if (dbry > sinkheight) dbry = sinkheight;
153 if (dtlx < -1.0) dtlx = -1.0;
154 if (dtly < -1.0) dtly = -1.0;
155 if (dbrx < 0.0) dbrx = 0.0;
156 if (dbry < 0.0) dbry = 0.0;
160 brx = ( int)ceil(dbrx)+1;
161 bry = ( int)ceil(dbry)+1;
168 template <
class InputStorageType,
class OutputStorageType>
173 if (accountForDifferentCameraCenters_) {
177 if (depthMapSink_==NULL) {
181 SinkP_.GetParameters()->UnProjectToRay(sink, p, d);
185 if (Ray.
NormL2()<0.5)
return -1;
188 GetLineIntersection(
HomgPoint3D(SinkP_.GetParameters()->GetC()),
189 Ray, pglobal)<0)
return -1;
193 depthMapSink_->GetImageDataArray()[int(sink[1])][int(sink[0])];
194 if (depth<=0.0)
return -1;
196 pglobal = SinkP_.GetParameters()->UnProjectToPoint(sink, depth);
197 if (pglobal[3]==0.0)
return -1;
200 source = SourceP_.GetParameters()->Project(pglobal);
206 SinkP_.GetParameters()->UnProjectLocal(sink, p, d);
207 source = SourceP_.GetParameters()->
208 ProjectLocal(relativeRSinkToSource_ * d);
210 unsigned int width, height;
211 SourceP_.GetParameters()->GetImageSize(width,height);
212 if(source[0] < 0 || source[0] >= width ||
213 source[1] < 0 || source[1] >= height )
218 if (source[2]< 0.5) {
230 #if defined(BUILD_IMAGE_CHAR)
235 #if defined(BUILD_IMAGE_USHORT)
239 #if defined(BUILD_IMAGE_SHORT)
243 #if defined(BUILD_IMAGE_SHORT)&&defined(BUILD_IMAGE_USHORT)
247 #if defined(BUILD_IMAGE_INT)
251 #if defined(BUILD_IMAGE_USHORT)
255 #if defined(BUILD_IMAGE_USHORT) && defined(BUILD_IMAGE_INT)
259 #if defined(BUILD_IMAGE_DOUBLE)
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
const Image< float > * depthMapSink_
depth map used for sink if camera centers differ
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
Maps source pixel to sink pixel of given projections.
HomgPlane3D mappingPlane_
map via this plane
double NormL2() const
Return the L2 norm: sqrt(a^2 + b^2 + c^2 + d^2)
RMatrix relativeRSinkToSource_
bool accountForDifferentCameraCenters_
dont map across plane at infinity
virtual int GetBoundingBox(unsigned int srcwidth, unsigned int srcheight, unsigned int sinkwidth, unsigned int sinkheight, int &tlx, int &tly, int &brx, int &bry)
uses forward mapping to calculate the bounding box in sink image, where to actually do the backward m...
This class hides the underlying projection model, like projection matrix, spherical camera...
class HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
virtual ~ProjectionMapping()
ProjectionMapping< InputStorageType, OutputStorageType > & operator=(const ProjectionMapping &src)
required because of const members
Projection SourceP_
Projections for backward mapping: Run over sink and compute source.
class BIASGeometryBase_EXPORT HomgPoint2D
virtual int GetSourceCoordinates_(const HomgPoint2D &sink, HomgPoint2D &source) const
Reimplementation for Pixel transformation, takes sink and computes coordinates in source...
class BIASGeometryBase_EXPORT HomgPoint3D
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...