25 #include <Base/Common/BIASpragma.hh>
27 #include <Base/Image/ImageBase.hh>
28 #include <Base/Image/ImageIO.hh>
32 #include "Geometry/PMatrix.hh"
33 #include "Image/HomographyMapping.hh"
34 #include "Image/Camera.hh"
48 cout <<
"Usage: biasmerge [options] files"<<endl;
49 cout <<
" Options: "<< endl
50 <<
" -s : search next smaller(string compare) PMatrix file"<<endl
51 <<
" -b : assume bbc camera file format"<<endl
52 <<
" -a : enforce aspect ratio 1, adapt P and image"<<endl
53 <<
" -p : force principal point=image center, adapt P and image"
56 cout <<
"This program opens the image files given by the command line, "
57 <<
"e.g. image037.pgm, looks at the command line for a file "
58 <<
"image037.pgm.mat and attaches the pmatrix to the pgm header."<<endl
59 <<
"With special options, you can enforce constraints on the KMatrix."
61 <<
"Returns: 0 on success, 1 if errors occured."<<endl<<endl
62 <<
"e.g. biasmerge *.ppm *.ppm.mat"<<endl<<endl;
70 EndOfPrefix = fn.rfind(
'.');
72 suffix = fn.substr(EndOfPrefix+1,fn.length());
83 int main (
int argc,
char *argv[])
85 bool Nearest =
false, BBC =
false, EnforceAspect =
false,
87 double ShrinkFactor = 1.0;
90 while ((i<argc) && (param)) {
92 if (!strcmp(
"-s",argv[i])) {
93 std::cout <<
"Using nearest P from command line also."<<endl;
99 if (!strcmp(
"-b",argv[i])) {
100 std::cout <<
"Trying to load BBC camera data."<<endl;
106 if (!strcmp(
"-a",argv[i])) {
107 std::cout <<
"Enforcing aspect ratio 1."<<endl;
108 EnforceAspect =
true;
113 if (!strcmp(
"-p",argv[i])) {
114 std::cout <<
"Enforcing principal point to image center."<<endl;
120 if (!strcmp(
"-h",argv[i])) {
121 std::cout <<
"Half image width and height."<<endl;
135 bool ErrorFlag =
false;
141 map<string, PMatrix> mapP;
142 map<string, BIAS::UUID> mapPUIDs;
144 for (
int j=i; j<argc; j++) {
145 if (strstr(argv[j],
".mat")!=NULL) {
146 cout<<
"arg "<<j<<
" is .mat";
153 std::ifstream fs( argv[j] );
157 }
while (!fs.eof() && ctmp!=
'#');
162 mapPUIDs[argv[j]] =Puid;
167 cout <<
"P is "<<P<<endl;
171 if (mapP.size()==0) {
172 BIASERR(
"No matrices found !");
180 if (strstr(argv[i],
".mat")!=NULL)
continue;
181 cout<<
"Trying "<<argv[i]<<endl;
182 if (DetermineFiletype(argv[i], FileType)!=0) {
183 BIASERR(
"Skipping current file because of unknown/unsuported format: "
184 <<argv[i]<<endl<<
"PMatrix can be written to mip,pgm,ppm,pnm");
192 BIASERR(
"Error "<<IOResult <<
" reading "<< argv[i]);
196 stringstream ss, filename;
198 ss << argv[i] <<
".mat";
199 if (mapP.find(ss.str())!=mapP.end()) {
200 cout<<
"Found a mat file for "<< filename.str() <<endl;
202 if (mapPUIDs.find(ss.str())!=mapPUIDs.end()) {
203 cout<<
"Found a UID in P-mat file "<< filename.str() <<
" UID "
204 << mapPUIDs[ss.str()] << endl;
205 CurrUID = mapPUIDs[ss.str()];
209 cout <<
"Found NO mat file for "<< filename.str()
210 <<
" !!!!! SKIPPING"<<endl;
213 map<string,PMatrix>::iterator it = mapP.lower_bound(ss.str());
214 if (it != mapP.begin()) it--;
215 else cout<<
"First element, cant use smaller, must use larger !";
217 cout <<
"Found next mat file for "<< filename.str()
218 <<
": "<<it->first<<endl;
221 if (EnforceAspect || EnforcePP || (ShrinkFactor!=1.0)) {
222 bool SkipWarping =
true;
225 int newimsizex = CurImage.
GetWidth(),
230 if (K[0][0]>K[1][1]) {
231 newimsizey = (int)rint(
double(newimsizey) * double(K[0][0]/K[1][1]));
232 K[1][2] *= double(K[0][0]/K[1][1]);
235 }
else if (K[0][0]<K[1][1]) {
236 newimsizex = (int)rint(
double(newimsizex) * double(K[1][1]/K[0][0]));
237 K[0][2] *= double(K[1][1]/K[0][0]);
244 if (rint(K[0][2]*2.0)!=(newimsizex-1)) {
245 cout<<
"changing ppx from "<<K[0][2];
247 K[0][2] = double(newimsizex-1) / 2.0;
248 cout<<
" to "<<K[0][2]<<endl;
250 if (rint(K[1][2]*2.0)!=(newimsizey-1)) {
251 cout<<
"changing ppy from "<<K[1][2];
253 K[1][2] = double(newimsizey-1) / 2.0;
254 cout<<
" to "<<K[1][2]<<endl;
257 if (ShrinkFactor != 1.0) {
258 K[0][0] *= ShrinkFactor;
259 K[1][1] *= ShrinkFactor;
260 K[0][2] *= ShrinkFactor;
261 K[1][2] *= ShrinkFactor;
262 newimsizex = int(rint(
double(newimsizex)*ShrinkFactor));
263 newimsizey = int(rint(
double(newimsizey)*ShrinkFactor));
279 cout<<
"Warping image from "<<CurImage.
GetWidth()<<
"x"
280 <<CurImage.
GetHeight()<<
" pixels to "<<newimsizex<<
"x"
281 <<newimsizey<<
" pixels."<<endl;
282 cout<<
" K was "<<P.GetK()<<endl;
283 cout<<
" K new "<<K<<endl;
284 cout<<
"Warping homography is "<<H<<endl;
293 Mapper.
Map(origtemplated, aspect1);
299 <<
" not implemented, please add the bit of code here");
302 filename <<
"_warped";
319 cout <<
"set UID from P matrix because image doesn't have a valid UID"
339 BIASERR(
"Error writing "<< filename.str());
341 }
else cout<<filename.str()<<endl;
344 if (ErrorFlag) cout <<
"There were errors." <<endl;
345 if (ErrorFlag)
return 1;
a 3x3 Matrix describing projective transformations between planes
Maps image src to image sink with homography H (software implementation)
void SetColorModel(EColorModel Model)
int SetP(const PMatrix &matP)
unsigned int GetWidth() const
const BIAS::UUID & GetUID() const
returns the UUID of the image
void SetZero()
Sets all values to zero.
int StealImage(ImageBase &source)
steals the image data array from source, after releasing the actual image data and sets source image ...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
TFileFormat
format specifier when writing an image to a file Do NOT change order or associated enum value because...
unsigned int GetHeight() const
void SetC(const Vector3< double > &C)
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.
void SetUID(const BIAS::UUID &id)
enum EColorModel GetColorModel() const
int UpdateMetaData()
copy P_ and co.
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
describes a projective 3D -> 2D mapping in homogenous coordinates
void SetK(const KMatrix &K)
interface class for producing/storing Universally Unique IDentifiers
void SetMetaData(const MetaData &m)
KMatrix Invert() const
returns analyticaly inverted matrix
This is the base class for images in BIAS.
void SetHomography(const HMatrix &H)
set your homography H (source = H * sink) before calling Map()
void SetR(const RMatrix &R)
bool IsValid() const
checks whether this uuid is valid(true) or unitialized(false)