25 #include <Base/Common/BIASpragma.hh>
27 #include <Image/ProjectionMapping.hh>
28 #include <Base/Image/ImageIO.hh>
29 #include <Base/Image/ImageConvert.hh>
30 #include <Base/Image/Image.hh>
31 #include <Base/Debug/TimeMeasure.hh>
32 #include <Base/Math/Vector3.hh>
33 #include <Base/Math/Vector.hh>
34 #include <Utils/Param.hh>
35 #include <Utils/ThreeDOut.hh>
36 #include <Geometry/ProjectionParametersSpherical.hh>
37 #include <Geometry/ProjectionParametersZoom.hh>
38 #include <Image/Camera.hh>
39 #include <Filter/DeInterlace.hh>
40 #include <Utils/IOUtils.hh>
53 {UNDEF, PERSPECTIVE, PERSPECTIVE_ZOOM, SPHERICAL};
55 void IdealizeAspectRatio(
KMatrix& K,
unsigned int& width,
unsigned int& height){
57 double factor = K[0][0] / K[1][1];
60 K[1][2] = factor * K[1][2];
61 height = (
unsigned int)(rint(
double(height)*factor));
64 K[0][2] = K[1][2] / factor;
65 width = (
unsigned int)(rint(
double(width) / factor));
69 int main(
int argc,
char *argv[])
72 MappingType mappingType = UNDEF;
74 string sourceImageName,sinkImageName,sourceCamName,sinkCamName;
82 unsigned int width, height;
84 bool ComputeInFloat =
false;
96 "A List of source Images, where each image has the same properties "
97 "(e.g. from same camera). If set, this will be used instead of SourceImage and output filenames will be bp-OrigName",
102 "Where to save the sink Image",
107 "The source Camera calibration XML",
112 "The sink Camera calibration XML filename or leave "
113 "empty, if you want an ideal image (no distortion)",
118 "Which interpolation shall be used?",
125 "use first and then every other image row",
129 "file containing LUT, if not existent "
130 "appropriate LUT with name is created.",
134 "don't change aspect ratio during undistortion",
false);
137 "rescale factor applied to sink camera parameters",
138 1.0, 0.0, DBL_MAX,
'r');
142 cout<<endl<<
"Usage example for image undistortion (if projection in header):"
143 <<endl<<endl<<
"\t biasproject -i distorted.pgm"<<endl
144 <<endl<<
" for unidealized aspct ratio use :"
145 <<endl<<endl<<
"\t biasproject -i distorted.pgm --leaveAspect"<<endl;
152 "Usage example for image (with projection in header) undistortion:"
154 <<
" biasproject -i distorted.pgm"<<endl);
162 string sourceImageListName = *param->
GetParamString(
"SourceImageList");
164 vector<string> sourceImageList;
165 vector<string> sinkImageList;
167 if (sourceImageListName.size() > 0) {
170 BIASERR(
"error parsing source image names!");
174 sourceImageName = sourceImageList[0];
175 sinkImageList.resize(sourceImageList.size());
176 for (
unsigned int i=0; i < sourceImageList.size(); i++) {
181 sinkImageList[i] = string(
"bp-")+ base + suffix;
186 sourceImageList.push_back(sourceImageName);
187 if (sinkImageName.size()==0) {
193 sinkImageName = string(
"bp-")+ base + suffix;
194 cout<<
"using output image name "<<sinkImageName<<
" for input file "<<
195 sourceImageName<<endl;
197 sinkImageList.push_back(sinkImageName);
205 case 2: interpolation_Method =
MapBicubic;
break;
209 BIASERR(
"unknown interpolation_Method");
216 bool generateLUT =
false;
217 bool mapperPrepared =
false;
221 file.open(lut->c_str());
223 cerr<<
"could not load lut, will generate lut file!\n";
230 for (
unsigned int i=0;i<sourceImageList.size(); i++) {
232 cout <<
"BIASPROJECT: " << sourceImageList[i] <<
" -> " << sinkImageList[i] << endl;
235 BIASERR(
"error loading image "<<sourceImageList[i]);
242 cout<<
"deinterlacing .."<<endl<<flush;
247 sourceimg = &sourceimgfloat;
249 ComputeInFloat =
true;
253 cout<<
"deinterlacing .."<<endl<<flush;
258 sourceimg = &sourceimguc;
260 ComputeInFloat =
false;
263 BIASERR(
"unsupported data type in input image (float and uchar supported)");
271 cout<<
"For safety your color format is converted to RGB (e.g. Bayer),"
272 <<
" so channelwise interpolation causes no problems."<<endl;
279 if (sourceCamName==
"" && sourceimguc.
IsProjValid()) {
280 sourceCam = sourceimguc.
GetProj();
281 sourceCam.
XMLWrite(
"source.projection");
282 }
else if (sourceCam.
Load(sourceCamName)!=0) {
283 BIASERR(
"Error reading projection (leave empty if in meta data)"
290 if (sinkCamName.size()==0) {
303 mappingType = PERSPECTIVE;
309 mappingType = PERSPECTIVE_ZOOM;
314 mappingType = SPHERICAL;
316 cerr <<
"Sorry, but your projection parameters don't match any case that can be computed." << endl
317 <<
"Please use tool biasprojectspherical for sphere undistortion" << endl;
330 unsigned int width=0, height=0;
332 IdealizeAspectRatio(K,width,height);
338 case PERSPECTIVE_ZOOM:
342 cerr <<
"Not implemented yet" << endl;
350 }
else if (sinkCam.
Load(sinkCamName)!=0) {
351 BIASERR(
"Error reading "<<sinkCamName);
356 if(*rescale > 0.0 && *rescale != 1.0) {
361 cout<<
"ROI of source image is "<<*sourceimg->
GetROI()
362 <<
" while image size is "<<width<<
" x "<<height<<endl;
367 cout<<
"You disabled anti-aliasing, mapping with "<<
368 interpolation_Method<<endl;
374 if (!mapperPrepared) {
382 if (mapper_float.
SetLookupTable(*lut, interpolation_Method) != 0) {
383 BIASERR(
"Setting lut from file failed");
385 cerr <<
"successfully loaded lut from file" << endl;
389 mapperPrepared =
true;
394 mapper_float.
Map(sourceimgfloat, sinkimgfloat, interpolation_Method);
401 BIASERR(
"error writing image to disk");
406 if (!mapperPrepared) {
415 BIASERR(
"Setting lut from file failed");
417 cerr <<
"successfully loaded lut from file" << endl;
421 mapperPrepared =
true;
426 mapper_uc.
Map(sourceimguc, sinkimguc, interpolation_Method);
432 BIAS_DEFAULT_SYNC, BIAS_DEFAULT_IMAGE_QUALITY,
434 BIASERR(
"error writing image to disk");
InterpolationMethod
accuracy for resampling
virtual int Load(const std::string &filename)
convenience wrapper which tries to read different formats
int SetLookupTable(const std::string &filename, InterpolationMethod &method)
Loads the LUT from a file, replaces the call to PrepareLookupTableMapping().
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
averages over a region with constant weights
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(),.
camera parameters which define the mapping between rays in the camera coordinate system and pixels in...
void SetSourceCam(const Projection &P)
Set your source projection before calling Map()
virtual void SetImageSize(const unsigned int w, const unsigned int h)
Set image dimensions (in pixels).
virtual void Rescale(double ratio, const double offset=0.0)
Adapt internal parameters to resampled image.
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
double * AddParamDouble(const std::string &name, const std::string &help, double deflt=0.0, double min=-DBL_MAX, double max=DBL_MAX, char cmdshort=0, int Group=GRP_NOSHOW)
int GetLookupTable(const std::string &filename, InterpolationMethod method)
Returns the LUT generated by PrepareLookupTableMapping() as an file.
void SetUndistortion(double kc1, double kc2)
bool * GetParamBool(const std::string &name) const
Deinterlacer filter for images.
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
ROI * GetROI()
Returns a pointer to the roi object.
This class hides the underlying projection model, like projection matrix, spherical camera...
std::string * GetParamString(const std::string &name) const
int XMLWrite(const std::string &Filename, int CompressionLevel=0, bool AutoAddCompressionSuffix=true, std::string encoding="UTF-8") const
call this to add the class to a new xml tree and write it to the file Filename.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
color values, 3 channels, order: red,green,blue
int SetProj(const Projection &Proj)
int * GetParamInt(const std::string &name) const
static void SplitName(const std::string &fullname, std::string &dir, std::string &base, std::string &suffix)
Split full path into:
int Map(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod=MapTrilinear, bool newSink=false, double SuperSampling=1.0)
backward mapping with various interpolations
int PrepareLookupTableMapping(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod method, bool newSink=false)
precomputes lookup coordinates for accessing src
static int ParseListFile(const std::string &ListFileName, std::vector< std::string > &LinesInFile)
Extracts lines from passed file.
static bool ParseCommandLineEvalHelp(Param ¶ms, int argc, char *argv[])
parses the command line, adds parameter "help"
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.
virtual int GetImageSize(unsigned int &Width, unsigned int &Height) const
Obtain image dimensions.
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
virtual BIAS::KMatrix GetK() const
enum EColorModel GetColorModel() const
void SetDeInterlaceType(BIAS_DEINTERLACE_TYPE type)
Set the Type of deinterlacing e.g.
int UpdateMetaData()
copy P_ and co.
This class Param provides generic support for parameters.
int MapWithLookupTable(const Image< InputStorageType > &src, Image< OutputStorageType > &sink, InterpolationMethod method)
applies precomputed coordinates in src, fast for repeated usages of same mapping function ...
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...
K describes the mapping from world coordinates (wcs) to pixel coordinates (pcs).
enum EStorageType GetStorageType() const
(8bit) unsigned char image storage type
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:
void SetMetaData(const MetaData &m)
const BIAS::Projection & GetProj() const
This is the base class for images in BIAS.
virtual void SetK(const KMatrix &K)
sets the internal parameters from a given KMatrix and updates the cached K and its inverse ...
std::string * AddParamString(const std::string &name, const std::string &help, std::string deflt="", char cmdshort=0, int Group=GRP_NOSHOW)
static int ToRGB(const Image< StorageType > &source, Image< StorageType > &dest)
Create a RGB converted copy of source image in this.
Depth images A: separated for now.
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...