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"
100typedef ImageSelector < Z2i::Domain, unsigned char>::Type Image;
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)]++;
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;
120 unsigned int sumMuAll= 0;
121 for(
unsigned int t=0; t< histo.size();t++){
122 sumMuAll+=histo[t]*t;
125 unsigned int thresholdRes=0;
127 for(
unsigned int t=0; t< histo.size(); t++){
138 double muAr=muA/(double)sumA;
139 double muBr=muB/(double)sumB;
140 double sigma= (double)sumA*(
double)sumB*(muAr-muBr)*(muAr-muBr);
150 bool operator()(std::vector<Z2i::Point> a, std::vector<Z2i::Point> b ){
151 return a.size() > b.size();
156void saveAllContoursAsFc( std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels,
157 unsigned int minSize,
bool sort=
false){
160 std::sort(vectContoursBdryPointels.begin(), vectContoursBdryPointels.end(), comp);
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;
172void saveSelContoursAsFC(std::vector< std::vector< Z2i::Point > > vectContoursBdryPointels,
173 unsigned int minSize, Z2i::Point refPoint,
double selectDistanceMax,
177 std::sort(vectContoursBdryPointels.begin(), vectContoursBdryPointels.end(), comp);
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;
196int main(
int argc,
char** argv )
200 std::string inputFileName;
201 std::string outputFileName {
"result.fc"};
203 double minThreshold {128};
204 double maxThreshold {255};
205 unsigned int minSize {0};
207 bool sortCnt {
false};
209 Z2i::Point selectCenter;
210 unsigned int selectDistanceMax = 0;
211 std::vector<int> cntConstraints;
212 std::vector<int> vectRangeMin, vectRangeMax, vectRange;
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)." )
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" )
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." )
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." )
230 app.get_formatter()->column_width(40);
231 CLI11_PARSE(app, argc, argv);
235 bool thresholdRange=vectRangeMax.size()==3 || vectRangeMin.size()==3;
236 typedef functors::IntervalThresholder<Image::Value> Binarizer;
237 Image image = GenericReader<Image>::import( inputFileName );
240 if(cntConstraints.size()==3){
242 selectCenter[0]= cntConstraints.at(0);
243 selectCenter[1]= cntConstraints.at(1);
244 selectDistanceMax= (
unsigned int) cntConstraints.at(2);
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) {
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;
260 vectRange = (vectRangeMin.size()==3) ? vectRangeMin : vectRangeMax;
262 increment=vectRange.at(1);
263 max = vectRange.at(2);
270 if(! ks.init( image.domain().lowerBound(),
271 image.domain().upperBound(),
true )){
272 trace.error() <<
"Problem in KSpace initialisation"<< std::endl;
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 );
285 saveSelContoursAsFC(vectContoursBdryPointels, minSize, selectCenter, selectDistanceMax, sortCnt);
287 saveAllContoursAsFc(vectContoursBdryPointels, minSize, sortCnt);
290 for(
int i=0; minThreshold+i*increment< maxThreshold; i++){
291 if(vectRangeMin.size()==3){
292 min = (int)(minThreshold+(i)*increment);
294 if(vectRangeMax.size()==3){
295 max = (int)(maxThreshold-(i)*increment);
297 Binarizer b(min, max);
298 functors::PointFunctorPredicate<Image,Binarizer> predicate(image, b);
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 );
306 saveSelContoursAsFC(vectContoursBdryPointels, minSize,
307 selectCenter, selectDistanceMax, sortCnt);
309 saveAllContoursAsFc(vectContoursBdryPointels,
312 trace.info() <<
" [done]" << std::endl;