32 #include "DGtal/base/Common.h"
34 #include "DGtal/topology/KhalimskySpaceND.h"
37 #include "DGtal/shapes/ShapeFactory.h"
38 #include "DGtal/shapes/Shapes.h"
39 #include "DGtal/helpers/StdDefs.h"
40 #include "DGtal/geometry/helpers/ContourHelper.h"
43 #include "DGtal/io/colormaps/GrayscaleColorMap.h"
44 #include "DGtal/images/imagesSetsUtils/ImageFromSet.h"
45 #include "DGtal/images/imagesSetsUtils/SetFromImage.h"
46 #include "DGtal/images/ImageContainerBySTLVector.h"
47 #include "DGtal/images/ImageSelector.h"
48 #include "DGtal/io/readers/PNMReader.h"
49 #include "DGtal/geometry/curves/FreemanChain.h"
51 #include "DGtal/io/boards/Board2D.h"
52 #include "DGtal/topology/helpers/Surfaces.h"
54 #include <boost/program_options/options_description.hpp>
55 #include <boost/program_options/parsers.hpp>
56 #include <boost/program_options/variables_map.hpp>
61 using namespace DGtal;
67 namespace po = boost::program_options;
69 int main(
int argc,
char** argv )
72 po::options_description general_opt(
"Allowed options are: ");
73 general_opt.add_options()
74 (
"help,h",
"display this message")
75 (
"image,i", po::value<std::string>(),
"image file name")
76 (
"min,m", po::value<int>(),
"min image threshold value (default 128)")
77 (
"max,M", po::value<int>(),
"max image threshold value (default 255)")
79 (
"minSize,s", po::value<int>(),
"minSize of the extracted freeman chain (default 0)")
80 (
"contourSelect,s", po::value<vector <int> >()->multitoken(),
81 "Select contour according reference point and maximal distance: ex. --contourSelect X Y distanceMax")
82 (
"thresholdRange,R", po::value<vector <int> >()->multitoken(),
83 "use a range interval as threshold : --thresholdRange min increment max : for each possible i, it define a digital sets [min+(i*increment),min+((i+1)*increment)] and extract their boundary. ");
88 po::store(po::parse_command_line(argc, argv, general_opt), vm);
90 if(vm.count(
"help")||argc<=1)
92 trace.
info()<<
"Extract FreemanChains from thresholded image" <<std::endl <<
"Basic usage: "<<std::endl
93 <<
"\t image2freeman [options] --image <imageName> -min 128 -max 255 > contours.fc"<<std::endl
94 << general_opt <<
"\n";
99 double minThreshold = 128;
100 double maxThreshold = 255;
101 unsigned int minSize =0;
103 bool thresholdRange=vm.count(
"thresholdRange");
105 unsigned int selectDistanceMax = 0;
109 if (!(vm.count(
"image"))){
110 trace.
info() <<
"Image file name needed"<< endl;
115 minThreshold= vm[
"min"].as<
int>();
118 maxThreshold= vm[
"max"].as<
int>();
120 if(vm.count(
"minSize")){
121 minSize = vm[
"minSize"].as<
int>();
123 if(vm.count(
"contourSelect")){
125 vector<int> cntConstraints= vm[
"contourSelect"].as<vector <int> >();
126 if(cntConstraints.size()!=3){
127 trace.
info() <<
"Incomplete option \"--contourSelect\""<< endl;
130 selectCenter[0]= cntConstraints.at(0);
131 selectCenter[1]= cntConstraints.at(1);
132 selectDistanceMax= (
unsigned int) cntConstraints.at(2);
135 int min, max, increment;
136 if(! thresholdRange){
137 min=(int)minThreshold;
138 max= (int)maxThreshold;
139 increment = (int)(maxThreshold- minThreshold);
141 vector<int> vectRange= vm[
"thresholdRange"].as<vector <int> >();
142 if(vectRange.size()!=3){
143 trace.
info() <<
"Incomplete option \"--thresholdRange\""<< endl;
147 increment=vectRange.at(1);
148 max = vectRange.at(2);
154 string imageFileName = vm[
"image"].as<std::string>();
159 for(
int i=0; minThreshold+i*increment< maxThreshold; i++){
160 min = (int)(minThreshold+i*increment);
161 max = (int)(minThreshold+(i+1)*increment);
165 trace.
info() <<
"DGtal set imported from thresholds ["<< min <<
"," << max <<
"]" << endl;
167 if(! ks.
init( image.domain().lowerBound(),
168 image.domain().upperBound(), true )){
169 trace.
error() <<
"Problem in KSpace initialisation"<< endl;
173 std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels;
175 ks, set2dPredicate, sAdj );
176 for(
unsigned int k=0; k<vectContoursBdryPointels.size(); k++){
177 if(vectContoursBdryPointels.at(k).size()>minSize){
180 unsigned int distance = (
unsigned int)ceil(sqrt((
double)(ptMean[0]-selectCenter[0])*(ptMean[0]-selectCenter[0])+
181 (ptMean[1]-selectCenter[1])*(ptMean[1]-selectCenter[1])));
182 if(distance<=selectDistanceMax){
184 cout << fc.
x0 <<
" " << fc.y0 <<
" " << fc.chain << endl;
188 cout << fc.
x0 <<
" " << fc.y0 <<
" " << fc.chain << endl;