Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biascrop.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 
26 #include <iostream>
27 #include <Utils/IOUtils.hh>
28 
29 
30 using namespace std;
31 using namespace BIAS;
32 
33 /** \file
34  * \brief Tool crops a rectangular image region of specified image,
35  writing the result either into a file from specified name.
36  If no output name was specified the input file name is extended by
37  "_crop". If projection parameters are found in the meta data,
38  the principle point is shifted accordingly and the image size is adapted.
39  see biascrop.cpp
40  \author bartczak 10/2009
41 */
42 int main(int argc, char* argv[])
43 {
44  Param params(true);
45  Vector<int>* ulIn = params.AddParamVecInt("UL", "upper left corner", "");
46  Vector<int>* lrIn = params.AddParamVecInt("LR", "lower left corner", "");
47 
48  int fup;
49  if(!IOUtils::ParseCommandLineEvalHelp(params, argc, argv, fup) ) {
50  cout<<argv[0]<<" --UL=\"x y\" --LR=\"x y\" <sourceImageName> [targetImageName]\n";
51  cout<<" while the upper left corner (UL) is contained in the cropped region, "
52  "the LR corner is excluded, as are the lower and the right rectangle border.\n";
53  return 0;
54  }
55 
56  if(argc<4) {
57  cout<<argv[0]<<" --UL=\"x y\" --LR=\"x y\" <sourceImageName> [targetImageName]\n";
58  cout<<" while the upper left corner (UL) is contained in the cropped region, "
59  "the LR corner is excluded, as are the lower and the right rectangle border.\n";
60  return -1;
61  }
62 
63  //determine output name
64  string inName(argv[fup]);
65  string outName;
66  if(argc == 5) {
67  outName = argv[fup+1];
68  } else {
69  outName = IOUtils::ExtendOutName(inName, "crop");
70  }
71  // cout<<"outName = "<<outName<<endl;
72  int ul[2] = {(*ulIn)[0], (*ulIn)[1]};
73  int lr[2] = {(*lrIn)[0], (*lrIn)[1]};
74 
75  if(ul[0] < 0 || ul[1] < 0) {
76  BIASERR("negative coordinates are nonsense! : ("<<ul[0]<<", "<<ul[1]<<") -> ("<<lr[0]<<", "<<lr[1]<<")");
77  return -1;
78  }
79  if(ul[0] >= lr[0] || ul[1] >= lr[1]) {
80  BIASERR("zero area rectangel defined : ("<<ul[0]<<", "<<ul[1]<<") -> ("<<lr[0]<<", "<<lr[1]<<")");
81  return -1;
82  }
83 
84 
85  //load input image
86  ImageBase inputImg;
87  if(ImageIO::Load(inName, inputImg) !=0 ) {
88  BIASERR("error loading src image named : "<<inName);
89  return -1;
90  }
91 
92  bool haveProjection = false;
93  Projection proj;
94 #ifdef BIAS_HAVE_XML2
95  //load projection
96  haveProjection = IOUtils::GetProjection(inputImg, proj);
97 #endif
98  inputImg.SetROICorners(static_cast<unsigned int>(ul[0]),
99  static_cast<unsigned int>(ul[1]),
100  static_cast<unsigned int>(lr[0]),
101  static_cast<unsigned int>(lr[1]));
102  inputImg.Cut2ROI();
103 
104  if(haveProjection) {
105  unsigned int newWidth = static_cast<unsigned int>(lr[0] - ul[0]);
106  unsigned int newHeight = static_cast<unsigned int>(lr[1] - ul[1]);
107  proj.GetParameters()->SetImageSize(newWidth, newHeight);
108  proj.GetParameters()->SetPrincipal(static_cast<double>(ul[0]), static_cast<double>(ul[1]));
109  // BIASWARN("not completely implemented!");
110  }
111 
112  if(ImageIO::Save(outName, inputImg) !=0 ) {
113  BIASERR("error loading src image named : "<<inName);
114  return -1;
115  }
116 
117 
118 
119  return 0;
120 }
virtual void SetPrincipal(const double x, const double y)
Set principal point (in pixels relative to top left corner).
int Cut2ROI()
reduces image to current ROI, !!! image size changes !!!
Definition: ImageBase.cpp:646
virtual void SetImageSize(const unsigned int w, const unsigned int h)
Set image dimensions (in pixels).
const ProjectionParametersBase * GetParameters(unsigned int cam=0) const
const parameter access function
Definition: Projection.hh:194
This class hides the underlying projection model, like projection matrix, spherical camera...
Definition: Projection.hh:70
BIAS::Vector< int > * AddParamVecInt(const std::string &name, const std::string &help, const BIAS::Vector< int > &deflt, char cmdshort=0, int Group=GRP_NOSHOW)
Add a parameter that expects a string on command line like &quot;&lt;value0&gt; &lt;value1&gt; &lt;value2&gt; ...
Definition: Param.cpp:423
This class Param provides generic support for parameters.
Definition: Param.hh:231
int SetROICorners(unsigned int UpperLeftX, unsigned int UpperLeftY, unsigned int LowerRightX, unsigned int LowerRightY)
Definition: ImageBase.cpp:1048
This is the base class for images in BIAS.
Definition: ImageBase.hh:102