DGtalTools 2.0.0
Loading...
Searching...
No Matches
img2freeman.cpp
1
24#include "DGtal/io/colormaps/GrayscaleColorMap.h"
25#include "DGtal/io/readers/GenericReader.h"
26#include "DGtal/images/ImageContainerBySTLVector.h"
27#include "DGtal/images/ImageSelector.h"
28#include "DGtal/geometry/curves/FreemanChain.h"
29#include "DGtal/geometry/helpers/ContourHelper.h"
30#include "DGtal/topology/helpers/Surfaces.h"
31
32#include "CLI11.hpp"
33
34#include <vector>
35#include <string>
36#include <climits>
37
38
39using namespace DGtal;
100typedef ImageSelector < Z2i::Domain, unsigned char>::Type Image;
101
102
103std::vector<unsigned int> getHistoFromImage(const Image &image){
104 const Image::Domain &imgDom = image.domain();
105 std::vector<unsigned int> vectHisto(UCHAR_MAX);
106 for(Image::Domain::ConstIterator it=imgDom.begin(); it!= imgDom.end(); ++it){
107 vectHisto[image(*it)]++;
108 }
109 return vectHisto;
110}
111
112unsigned int
113getOtsuThreshold(const Image &image){
114 std::vector<unsigned int> histo = getHistoFromImage(image);
115 unsigned int imageSize = image.domain().size();
116 unsigned int sumA = 0;
117 unsigned int sumB = imageSize;
118 unsigned int muA=0;
119 unsigned int muB=0;
120 unsigned int sumMuAll= 0;
121 for( unsigned int t=0; t< histo.size();t++){
122 sumMuAll+=histo[t]*t;
123 }
124
125 unsigned int thresholdRes=0;
126 double valMax=0.0;
127 for( unsigned int t=0; t< histo.size(); t++){
128 sumA+=histo[t];
129 if(sumA==0)
130 continue;
131 sumB=imageSize-sumA;
132 if(sumB==0){
133 break;
134 }
135
136 muA+=histo[t]*t;
137 muB=sumMuAll-muA;
138 double muAr=muA/(double)sumA;
139 double muBr=muB/(double)sumB;
140 double sigma= (double)sumA*(double)sumB*(muAr-muBr)*(muAr-muBr);
141 if(valMax<=sigma){
142 valMax=sigma;
143 thresholdRes=t;
144 }
145 }
146 return thresholdRes;
147}
148
149struct CompContours{
150 bool operator()(std::vector<Z2i::Point> a, std::vector<Z2i::Point> b ){
151 return a.size() > b.size();
152 }
153};
154
155
156void saveAllContoursAsFc( std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels,
157 unsigned int minSize, bool sort=false){
158 CompContours comp;
159 if(sort){
160 std::sort(vectContoursBdryPointels.begin(), vectContoursBdryPointels.end(), comp);
161 }
162 for(unsigned int k=0; k<vectContoursBdryPointels.size(); k++){
163 if(vectContoursBdryPointels.at(k).size()>minSize){
164 FreemanChain<Z2i::Integer> fc (vectContoursBdryPointels.at(k));
165 std::cout << fc.x0 << " " << fc.y0 << " " << fc.chain << std::endl;
166
167 }
168 }
169}
170
171
172void saveSelContoursAsFC(std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels,
173 unsigned int minSize, Z2i::Point refPoint, double selectDistanceMax,
174 bool sort=false){
175 CompContours comp;
176 if(sort){
177 std::sort(vectContoursBdryPointels.begin(), vectContoursBdryPointels.end(), comp);
178 }
179
180 for(unsigned int k=0; k<vectContoursBdryPointels.size(); k++){
181 if(vectContoursBdryPointels.at(k).size()>minSize){
182 Z2i::RealPoint ptMean = ContourHelper::getBarycenter(vectContoursBdryPointels.at(k));
183 unsigned int distance = (unsigned int)ceil(sqrt((ptMean[0]-refPoint[0])*(ptMean[0]-refPoint[0])+
184 (ptMean[1]-refPoint[1])*(ptMean[1]-refPoint[1])));
185 if(distance<=selectDistanceMax){
186 FreemanChain<Z2i::Integer> fc (vectContoursBdryPointels.at(k));
187 std::cout << fc.x0 << " " << fc.y0 << " " << fc.chain << std::endl;
188 }
189 }
190 }
191}
192
193
194
195
196int main( int argc, char** argv )
197{
198 // parse command line using CLI ----------------------------------------------
199 CLI::App app;
200 std::string inputFileName;
201 std::string outputFileName {"result.fc"};
202
203 double minThreshold {128};
204 double maxThreshold {255};
205 unsigned int minSize {0};
206 bool select {false};
207 bool sortCnt {false};
208
209 Z2i::Point selectCenter;
210 unsigned int selectDistanceMax = 0;
211 std::vector<int> cntConstraints;
212 std::vector<int> vectRangeMin, vectRangeMax, vectRange;
213
214 app.description("Extract FreemanChains from thresholded image.\n Basic example: \t img2freeman [options] --input <imageName> -min 128 -max 255 > contours.fc \n Note that if you don't specify any threshold a threshold threshold max is automatically defined from the Otsu algorithm with min=0. ");
215 app.add_option("-i,--input,1", inputFileName, "input image file name (any 2D image format accepted by DGtal::GenericReader)." )
216 ->required()
217 ->check(CLI::ExistingFile);
218 app.add_option("-m,--min", minThreshold, "min image threshold value (default 128)");
219 app.add_option("-M,--max", maxThreshold, "max image threshold value (default 255)");
220 app.add_flag("--sort", sortCnt,"to sort the resulting freemanchain by decreasing size." );
221 app.add_option("-s,--minSize", minSize,"minSize of the extracted freeman chain (default 0)" );
222 app.add_option("contourSelect",cntConstraints,"Select contour according reference point and maximal distance: ex. --contourSelect X Y distanceMax" )
223 -> expected(3);
224 app.add_option("-r,--thresholdRangeMin",vectRangeMin, "use a range interval as threshold (from min) : --thresholdRangeMin min increment max : for each possible i, it define a digital sets [min, min+((i+1)*increment)] such that min+((i+1)*increment)< max and extract their boundary." )
225 -> expected(3);
226 app.add_option("-R,--thresholdRangeMax",vectRangeMax, "use a range interval as threshold (from max) : --thresholdRangeMax min increment max : for each possible i, it define a digital sets [ max-((i)*increment), max] such that max-((i)*increment)>min and extract their boundary." )
227 -> expected(3);
228
229
230 app.get_formatter()->column_width(40);
231 CLI11_PARSE(app, argc, argv);
232 // END parse command line using CLI ----------------------------------------------
233
234
235 bool thresholdRange=vectRangeMax.size()==3 || vectRangeMin.size()==3;
236 typedef functors::IntervalThresholder<Image::Value> Binarizer;
237 Image image = GenericReader<Image>::import( inputFileName );
238
239
240 if(cntConstraints.size()==3){
241 select=true;
242 selectCenter[0]= cntConstraints.at(0);
243 selectCenter[1]= cntConstraints.at(1);
244 selectDistanceMax= (unsigned int) cntConstraints.at(2);
245 }
246
247 int min, max, increment;
248 if(! thresholdRange){
249 min=(int)minThreshold;
250 max= (int)maxThreshold;
251 increment = (int)(maxThreshold - minThreshold);
252 if(minThreshold == 128 && maxThreshold == 255) {
253 min=0;
254 trace.info() << "Min/Max threshold values not specified, set min to 0 and computing max with the otsu algorithm...";
255 max = getOtsuThreshold(image);
256 trace.info() << "[done] (max= " << max << ") "<< std::endl;
257 }
258
259 }else{
260 vectRange = (vectRangeMin.size()==3) ? vectRangeMin : vectRangeMax;
261 min=vectRange.at(0);
262 increment=vectRange.at(1);
263 max = vectRange.at(2);
264 minThreshold=min;
265 maxThreshold=max;
266 }
267
268
269 Z2i::KSpace ks;
270 if(! ks.init( image.domain().lowerBound(),
271 image.domain().upperBound(), true )){
272 trace.error() << "Problem in KSpace initialisation"<< std::endl;
273 }
274
275
276 if (!thresholdRange){
277 Binarizer b(min, max);
278 functors::PointFunctorPredicate<Image,Binarizer> predicate(image, b);
279 trace.info() << "DGtal contour extraction from thresholds ["<< min << "," << max << "]" ;
280 SurfelAdjacency<2> sAdj( true );
281 std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels;
282 Surfaces<Z2i::KSpace>::extractAllPointContours4C( vectContoursBdryPointels,
283 ks, predicate, sAdj );
284 if(select){
285 saveSelContoursAsFC(vectContoursBdryPointels, minSize, selectCenter, selectDistanceMax, sortCnt);
286 }else{
287 saveAllContoursAsFc(vectContoursBdryPointels, minSize, sortCnt);
288 }
289 }else{
290 for(int i=0; minThreshold+i*increment< maxThreshold; i++){
291 if(vectRangeMin.size()==3){
292 min = (int)(minThreshold+(i)*increment);
293 }
294 if(vectRangeMax.size()==3){
295 max = (int)(maxThreshold-(i)*increment);
296 }
297 Binarizer b(min, max);
298 functors::PointFunctorPredicate<Image,Binarizer> predicate(image, b);
299
300 trace.info() << "DGtal contour extraction from thresholds ["<< min << "," << max << "]" ;
301 SurfelAdjacency<2> sAdj( true );
302 std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels;
303 Surfaces<Z2i::KSpace>::extractAllPointContours4C( vectContoursBdryPointels,
304 ks, predicate, sAdj );
305 if(select){
306 saveSelContoursAsFC(vectContoursBdryPointels, minSize,
307 selectCenter, selectDistanceMax, sortCnt);
308 }else{
309 saveAllContoursAsFc(vectContoursBdryPointels,
310 minSize, sortCnt);
311 }
312 trace.info() << " [done]" << std::endl;
313 }
314 }
315}
316
Definition ATu0v1.h:57