26 #include <Image/Camera.hh>
27 #include <Base/Image/ImageBase.hh>
28 #include <Base/Image/ImageIO.hh>
29 #include <Base/Image/ImageConvert.hh>
30 #include <Base/Image/ImageAttributes.hh>
31 #include <Geometry/PMatrix.hh>
32 #include <bias_config.h>
35 #include <Utils/Param.hh>
38 # include "Base/Common/getopt_W32.h"
52 void writePixelToFileFloat(
void** imageArray,
unsigned int w,
53 unsigned int h, ofstream& ofs) {
54 float* row = (
float*)imageArray[h];
58 void writePixelToFileUChar(
void** imageArray,
unsigned int w,
59 unsigned int h, ofstream& ofs) {
60 unsigned char* row = (
unsigned char*)imageArray[h];
65 int DetermineFiletype(
const string &fn)
72 void AdoptPMatrix(
PMatrix &P,
float scalex,
float scaley)
97 int ReadPMatrix(
PMatrixBase &pmat,
const string &filename)
100 ifstream pmatfile(filename.c_str());
102 cerr <<
"unable to open " << filename<< endl;
106 pmatfile >> row >> col;
108 cerr <<
"error reading " << filename << endl;
111 if (row!=3 || col!=4){
112 cerr <<
"P Matrix should have size 3x4, not: " <<row<<
"x"<<col << endl;
117 for (
int r=0; r < row; r++)
118 for (
int c=0; c< col; c++)
119 pmatfile >> pmat[r][c];
124 int ReadEXIF(
const string &ifile,
KMatrix &KExif,
int &EXIFMode)
129 if (Att.
Read(ifile)!=BIAS_IMAGEATTRIBUTES_SUCCESS) {
130 cerr<<
"error reading exif tags ..."<<endl;
142 cout<<
"Found a K="<<KExif<<endl;
144 cout<<
"Error, not enough EXIF data to get K:"<<KExif<<endl;
147 return (success)?(0):(-1);
152 int main (
int argc,
char *argv[])
156 bool *help = param.
AddParamBool(
"help",
"display help",
false,
'h');
157 bool *togrey = param.
AddParamBool(
"toGrey",
"convert image to grey image",
false,
'g');
158 bool *toHSL = param.
AddParamBool(
"toHSL",
"convert image to HSL image",
false,
'l');
159 bool *toRGB = param.
AddParamBool(
"toRGB",
"convert image to RGB image",
false,
'r');
161 bool *verbose = param.
AddParamBool(
"verbose",
"verbose output",
false,
'v');
162 int *EXIFMode = param.
AddParamInt(
"KFromExif",
"Compute K Matrix from exif data (0,1,2):", 0, 0, 2,
'e');
163 string *pmatfile = param.
AddParamString(
"PMatrixFile",
"add P-Matrix",
"",
'p');
164 double *scale = param.
AddParamDouble(
"modulate",
"Modulate (scale) all pixel values", 1.0, -DBL_MAX, DBL_MAX,
'm');
165 double *shift = param.
AddParamDouble(
"offset",
"Offset all pixels", 0.0, -DBL_MAX, DBL_MAX,
'o');
166 double *newzero = param.
AddParamDouble(
"newZero",
"new value for zero (useful for inverting depth maps)", 0.0, -DBL_MAX, DBL_MAX,
'z');
167 int *imagequality = param.
AddParamInt(
"quality",
"quality for lossy formats [0;100] best=100", BIAS_DEFAULT_IMAGE_QUALITY, 0, 100,
'q');
168 bool *clip = param.
AddParamBool(
"clip",
"clip values to [0,255], only effective if -m or -o",
false,
'c');
169 bool *convertToMatrix = param.
AddParamBool(
"convertToMatrix",
"convert to matrix textfile, readable my matlab with simple header",
false,
'T');
170 bool *convertFromMatrix = param.
AddParamBool(
"convertFromMatrix",
"convert from matrix textfile containing rows cols colorchannels and interleaved flag in the first line and then the image matrix row by row",
false,
'F');
171 bool *ToIMA = param.
AddParamBool(
"convertToIma",
"convert into binary ima fileformat needed by RealEyes",
false,
'I');
173 vector<string> st_enum;
178 #ifdef BUILD_IMAGE_USHORT
182 bool replacezero =
false;
183 bool maxDynamics =
false;
187 bool scaleshift = (*scale != 1.0 || *shift != 0.0);
189 if (*help || argc <3) {
190 cout <<
"Usage: " << argv[0] <<
" [options] infile outfile\n";
192 cout <<
"if neither -m nor -o is given float images are scaled \n"
193 <<
"and shifted automatically"<<endl;
196 string ifile = argv[fup];
197 string ofile = argv[fup+1];
203 #ifdef BUILD_IMAGE_USHORT
213 ifile = argv[argc-2];
214 ofile = argv[argc-1];
216 if (*newzero != 0.0) {
221 if(!*convertToMatrix && !*ToIMA) {
226 BIASWARN(
"unknown FileFormat"<<ofile);
235 cout <<
"load "<<ifile<<
", "<<flush;
239 if (*convertFromMatrix) {
243 BIASERR(
"could not load " <<ifile);
264 cout<<
"writing IMA format (RealEyes)\n";
279 if(*convertToMatrix) {
281 if(filename != ofile) {
282 BIASWARNONCE(
"Changed filenames to extension .matrix")
289 if (ReadEXIF(ifile, KExif, *EXIFMode)!=0)
return -1;
293 if (verbose) cout <<
"storagetype is float"<<endl;
295 fcam.ParseMetaData();
304 fcam.GetMinMaxPixelValue(min,max);
305 cout <<
"min = " << min <<
", max = " << max << endl;
309 float *fcamptr=fcam.GetImageData();
313 for (
unsigned int i = 0; i < fcam.GetWidth()*fcam.GetHeight(); i++){
314 if (*fcamptr != -1.0){
322 fcamptr=fcam.GetImageData();
324 float scaledmean=0.0;
325 for (
unsigned int i = 0; i < fcam.GetWidth()*fcam.GetHeight();i++){
326 if (*fcamptr != -1.0)
327 variance += (*fcamptr-mean)*(*fcamptr-mean);
330 fcam.ScaleShift(*scale,*shift);
331 fcamptr=fcam.GetImageData();
333 for (
unsigned int i = 0; i < fcam.GetWidth()*fcam.GetHeight();i++){
334 *fcamptr = (*fcamptr < 255) ? *fcamptr : 255 ;
335 *fcamptr = (*fcamptr > 0) ? *fcamptr : 0 ;
336 scaledmean += *fcamptr;
342 cout <<
"mean = " << mean <<
", scaledmean="
343 << scaledmean/num_elem
345 << variance <<
", fill rate = "
346 << num_elem/fcam.GetWidth()/fcam.GetHeight() << endl;
351 fcam.GetMinMaxPixelValue(min,max);
352 cout <<
"min = " << min <<
", max = " << max << endl;
354 fcam.ScaleShiftBetween(0,255);
380 *fcamptr = fcam.GetImageData();
382 if ((*fcamptr) <= 0) {
383 *focamptr = float(*newzero);
406 if (*verbose) cout <<
"storagetype is unsigned char"<<endl;
409 uccam.ParseMetaData();
413 if (*verbose) cout <<
"P: "<<P_<<endl;
417 BIASWARN(
"You scale an unsigned char image with more than 1.0");
419 uccam.ScaleShift(*scale,*shift);
432 #ifdef BUILD_IMAGE_USHORT
441 cout <<
"input storagetype is unsigned short int"<<endl
442 <<
"output file type is "<<ofiletype<<endl;
447 uscam.ParseMetaData();
451 if (verbose) cout <<
"P: "<<P_<<endl;
490 if (*verbose) cout <<
"output storage type is "<<OutST<<endl;
494 if (*pmatfile !=
"") {
495 if (*verbose) cout <<
"read "<<*pmatfile<<
", ";
496 if (ReadPMatrix(P_,*pmatfile) < 0)
515 if (EXIFMode>0) ucocam.
SetK(KExif);
517 if (*verbose) cout <<
"write unsigned char "<<ofile<<
", "<<flush;
529 string::size_type endofprefix = ofile.rfind(
'.');
530 ofile = ofile.substr(0,endofprefix);
535 if (*EXIFMode>0) focam.
SetK(KExif);
541 if (*verbose) cout <<
"write float "<<ofile<<
", "<<flush;
543 #ifdef BUILD_IMAGE_USHORT
545 if (*EXIFMode>0) usocam.
SetK(KExif);
549 if (*verbose) cout <<
"write unsigned short "<<ofile<<
", "<<flush;
553 if (*verbose) cout <<
"done"<<endl;
static long int GetGlobalDebugLevel()
void PrintHeader(std::ostream &os=std::cout) const
int AboveThresholdToValue(StorageType Threshold, StorageType Value)
sets alls pixels with values above Threshold to Value
(16bit) unsigned integer image storage type
static int ExportMatrix(const std::string &FileName, const ImageBase &img, const bool verbose)
Export BIAS images to text images.
int BelowThresholdToValue(StorageType Threshold, StorageType Value)
sets alls pixels with values below Threshold to Value
int ScaleShiftBetween(double Min, double Max)
scales and shifts image so afterwards every pixel has a value between Min and Max ...
bool * AddParamBool(const std::string &name, const std::string &help, bool deflt=false, char cmdshort=0, int Group=GRP_NOSHOW)
int GetPrincipalPoint(double &x, double &y)
look up principal point
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)
static std::string ExtensionName(const std::string &oldName, const std::string &newExtension)
appends extension to name if extension is not already extension, avoids double extension like "...
int * AddParamEnum(const std::string &name, const std::string &help, const std::vector< std::string > &enums, const int deflt=0, const std::vector< int > *IDs=NULL, const char cmdshort=0, const int Group=GRP_NOSHOW)
int SetP(const PMatrix &matP)
int ScaleShift(double Scale, double Shift)
scales and shifts image (all channels simultanously)
static BIASImageBase_EXPORT int ToHSL(const Image< StorageType > &source, Image< StorageType > &dest)
Create a HSL converted copy of source image in this Release() and Init() are called if necessary...
int GetR(Matrix3x3< double > &R)
static BIAS::ImageIO::TFileFormat GetFileFormat(const std::string &str)
determines the BIAS::ImageIO::TFileFormat for a given filename/ extension Decision based on extension...
unsigned int GetWidth() const
int ParseCommandLine(int &argc, char *argv[])
scan command line arguments for valid parameters
invalid not set image storage type
void Usage(std::ostream &os=std::cout)
print Help-Information to stdout
int StealImage(ImageBase &source)
steals the image data array from source, after releasing the actual image data and sets source image ...
static int ConvertST(const BIAS::ImageBase &source, BIAS::ImageBase &dest, ImageBase::EStorageType targetST)
Function to convert the storage type of images e.g.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
color values, 3 channels, order: red,green,blue
TFileFormat
format specifier when writing an image to a file Do NOT change order or associated enum value because...
unsigned int GetHeight() const
class BIASGeometry_EXPORT PMatrix
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.
int Read(const std::string &filename)
main entry routine to read all EXIF tags from the header of image file.
describes a projective 3D -> 2D mapping in homogenous coordinates
int GetFocalLengthXPixel(double &fp)
compute focallength in pixels
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
static std::string GetExtension(const BIAS::ImageIO::TFileFormat &fmt)
return the default extension for a given FileFormat enum
int UpdateMetaData()
copy P_ and co.
int GetC(Vector3< double > &C)
computes translation vector origin world coo -> origin camera coo (center), uses decomposition, which is cached
This class Param provides generic support for parameters.
contains all atribute info and values of e.g. a file.
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...
void InitExifTags()
initialize me vector with all known EXIF tags
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
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:
static int ImportMatrix(const std::string &FileName, ImageBase &img, const bool verbose)
Import text images to BIAS.
describes a projective 3D -> 2D mapping in homogenous coordinates
void SetK(const KMatrix &K)
static int WriteRAW_IMA(const std::string &FileName, const ImageBase &img_const)
Write image in binary file format needed by RealEyes.
This is the base class for images in BIAS.
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.
int ParseMetaData(bool bUse2x64bitTS=true)
After ImageIO::Load() operated on AppData_, this method fills P_, Timestamp, DC_*, ...
int GetFocalLengthYPixel(double &fp)
compute focallength in pixels
static void SetGlobalDebugLevel(long int lev)
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
int GetK(KMatrix &K)
calibration matrix
static int ToGrey(const ImageBase &source, ImageBase &dest)
wrapper for the templated function ToGrey