DGtalTools  1.2.0
freeman2img.cpp
1 
30 #include <iostream>
31 
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 
35 //image
36 #include "DGtal/io/readers/PointListReader.h"
37 #include "DGtal/io/writers/GenericWriter.h"
38 
39 //contour
40 #include "DGtal/geometry/curves/FreemanChain.h"
41 #include "DGtal/topology/helpers/Surfaces.h"
42 #include "DGtal/topology/SurfelSetPredicate.h"
43 
44 //STL
45 #include <vector>
46 #include <string>
47 
48 #include "CLI11.hpp"
49 
50 
51 using namespace DGtal;
52 
53 
106 int main( int argc, char** argv )
107 {
108 
109  // parse command line using CLI ----------------------------------------------
110  CLI::App app;
111  std::string inputFileName;
112  std::string outputFileName="result.pgm";
113  unsigned int border {0};
114  std::vector<int> space;
115 
116  app.description("Transform one or several freeman chains into an grayscale image file by filling its interior areas.\nThe transformation can fill shapes with hole by using the freemanchain orientation. The interior is considered on the left according to a freeman chain move, i.e. a clockwise oriented contour represents a hole in the shape. Basic example:\n \t freeman2img -i inputChain.fc -o contourDisplay.pgm -b 5");
117  app.add_option("-i,--input,1", inputFileName, "Input freeman chain file name." )
118  ->required()
119  ->check(CLI::ExistingFile);
120  app.add_option("-b,--border",border, "add a border in the resulting image (used only in the automatic mode i.e when --space is not used.");
121  app.add_option("-o,--output,2", outputFileName, "the output fileName", true);
122  app.add_option("-s,--space", space, "Define the space from its bounding box (lower and upper coordinates) else the space is automatically defined from the freemanchain bounding boxes." )
123  ->expected(4);
124 
125 
126  app.get_formatter()->column_width(40);
127  CLI11_PARSE(app, argc, argv);
128  // END parse command line using CLI ----------------------------------------------
129 
130 
131  typedef KhalimskySpaceND<2, int>::Cell Cell;
132  typedef KhalimskySpaceND<2, int>::SCell SCell;
133  typedef FreemanChain<Z2i::Integer> FreemanChain;
134  typedef DGtal::KhalimskySpaceND< 2, int > KSpace;
135  typedef DGtal::ImageContainerBySTLVector<Z2i::Domain, unsigned char> Image2D ;
136 
137 
138  std::vector< FreemanChain > vectFcs = PointListReader< Z2i::Point >::getFreemanChainsFromFile<Z2i::Integer> (inputFileName);
139  int minx=std::numeric_limits<int>::max();
140  int miny=std::numeric_limits<int>::max();
141  int maxx=std::numeric_limits<int>::min();
142  int maxy=std::numeric_limits<int>::min();
143 
144 
145  if(space.size()!=4){
146  for(std::vector< FreemanChain >::const_iterator it = vectFcs.begin(); it!= vectFcs.end(); it++){
147  FreemanChain fc = *it;
148  int t_minx=std::numeric_limits<int>::max();
149  int t_miny=std::numeric_limits<int>::max();
150  int t_maxx=std::numeric_limits<int>::min();
151  int t_maxy=std::numeric_limits<int>::min();
152  fc.computeBoundingBox(t_minx, t_miny, t_maxx, t_maxy);
153  minx = t_minx > minx? minx: t_minx;
154  miny = t_miny > miny? miny: t_miny;
155  maxx = t_maxx < maxx? maxx: t_maxx;
156  maxy = t_maxy < maxy? maxy: t_maxy;
157  }
158  minx-=border; miny-=border; maxx+=border; maxy+=border;
159  }else{
160  minx = space[0];
161  miny = space[1];
162  maxx = space[2];
163  maxy = space[3];
164  }
165  KSpace aKSpace;
166  aKSpace.init(Z2i::Point(minx, miny), Z2i::Point(maxx, maxy), true);
167  std::set<SCell> boundarySCell;
168  std::set<Cell> interiorCell;
169  for(std::vector< FreemanChain >::const_iterator it = vectFcs.begin(); it!= vectFcs.end(); it++){
170  FreemanChain fc = *it;
171  FreemanChain::getInterPixelLinels(aKSpace, fc, boundarySCell, true);
172  }
173 
174  Image2D imageResult (Z2i::Domain(Z2i::Point(minx, miny), Z2i::Point(maxx, maxy)));
175  Surfaces<KSpace>::uFillInterior(aKSpace, functors::SurfelSetPredicate<std::set<SCell>,SCell>(boundarySCell), imageResult, 255, false, false );
176  imageResult >> outputFileName;
177 
178 }
179 
Definition: ATu0v1.h:57