33 #include "DGtal/base/Common.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/topology/helpers/Surfaces.h"
37 #include "DGtal/images/ImageContainerBySTLVector.h"
38 #include "DGtal/io/readers/GenericReader.h"
39 #include "DGtal/io/writers/GenericWriter.h"
40 #include "DGtal/images/IntervalForegroundPredicate.h"
41 #include <DGtal/topology/SetOfSurfels.h>
42 #include "DGtal/topology/DigitalSurface.h"
43 #include "DGtal/topology/helpers/Surfaces.h"
46 #include <boost/program_options/options_description.hpp>
47 #include <boost/program_options/parsers.hpp>
48 #include <boost/program_options/variables_map.hpp>
51 using namespace DGtal;
55 namespace po = boost::program_options;
112 std::vector<unsigned int> getHistoFromImage(
const Image3D &image){
114 std::vector<unsigned int> vectHisto(UCHAR_MAX);
116 vectHisto[image(*it)]++;
124 getOtsuThreshold(
const Image3D &image){
125 std::vector<unsigned int> histo = getHistoFromImage(image);
126 unsigned int imageSize = image.
domain().size();
127 unsigned int sumA = 0;
128 unsigned int sumB = imageSize;
131 unsigned int sumMuAll= 0;
132 for(
unsigned int t=0; t< histo.size();t++){
133 sumMuAll+=histo[t]*t;
136 unsigned int thresholdRes=0;
138 for(
unsigned int t=0; t< histo.size(); t++){
149 double muAr=muA/(double)sumA;
150 double muBr=muB/(double)sumB;
151 double sigma= (double)sumA*(
double)sumB*(muAr-muBr)*(muAr-muBr);
162 int main(
int argc,
char** argv )
169 po::options_description general_opt(
" \n Allowed options are ");
170 general_opt.add_options()
171 (
"help,h",
"display this message")
172 (
"input,i", po::value<std::string>(),
"volumetric input file (.vol, .pgm, .pgm3d, .longvol) " )
173 (
"output,o", po::value<std::string>(),
"volumetric output file (.vol, .pgm, .pgm3d, .longvol) " )
174 (
"labelBackground",
"option to define a label to regions associated to object background. ")
175 (
"thresholdMin,m", po::value<int>()->default_value(0),
"min threshold (if not given the max threshold is computed with Otsu algorithm)." )
176 (
"thresholdMax,M", po::value<int>(),
"max threshold (default 255)" );
180 po::variables_map vm;
182 po::store(po::parse_command_line(argc, argv, general_opt), vm);
183 }
catch(
const std::exception& ex){
185 trace.
info()<<
"Error checking program options: "<< ex.what()<< endl;
188 if( !parseOK || vm.count(
"help")||argc<=1)
190 std::cout <<
"Usage: " << argv[0] <<
" [input] [output]\n"
191 <<
"Segment volumetric file from a simple threshold which can be set automatically from the otsu estimation.\n"
192 <<
"The segmentation result is given by an integer label given in the resulting image."
193 << general_opt <<
"\n";
194 std::cout <<
"Example:\n"
195 <<
"volSegment -i ${DGtal}/examples/samples/lobster.vol -o segmentation.vol \n";
200 if(! vm.count(
"input") ||! vm.count(
"output"))
202 trace.
error() <<
" Input and output filename are needed to be defined" << endl;
207 string inputFilename = vm[
"input"].as<std::string>();
208 string outputFilename = vm[
"output"].as<std::string>();
210 trace.
info() <<
"Reading input file " << inputFilename ;
214 trace.
info() <<
" [done] " << std::endl ;
215 std::ofstream outStream;
216 outStream.open(outputFilename.c_str());
217 int minTh = vm[
"thresholdMin"].as<
int>();
219 if(!vm.count(
"thresholdMax")){
220 maxTh = getOtsuThreshold(inputImage);
221 trace.
info() <<
"maximal threshold value not specified, using Otsu value: " << maxTh << std::endl;
223 maxTh = vm[
"thresholdMax"].as<
int>();
225 trace.
info() <<
"Processing image to output file " << outputFilename << std::endl;
230 bool space_ok = K.
init( inputImage.
domain().lowerBound(), inputImage.
domain().upperBound(), false );
232 trace.
error() <<
"problem initializing 3d space" << endl;
236 std::vector< std::vector<Z3i::SCell > > vectConnectedSCell;
239 for(
unsigned int i = 0; i<vectConnectedSCell.size(); i++)
242 MySetOfSurfels aSet(K, SAdj);
246 for(std::vector<Z3i::SCell>::const_iterator it= vectConnectedSCell.at(i).begin(); it != vectConnectedSCell.at(i).end(); ++it)
248 aSet.surfelSet().insert(aSet.surfelSet().begin(), *it);
249 unsigned int orth_dir = K.
sOrthDir( *it );
252 if(p1[0] < lowerPoint[0]) lowerPoint[0]= p1[0];
253 if(p1[1] < lowerPoint[1]) lowerPoint[1]= p1[1];
254 if(p1[2] < lowerPoint[2]) lowerPoint[2]= p1[2];
256 if(p1[0] > upperPoint[0]) upperPoint[0]= p1[0];
257 if(p1[1] > upperPoint[1]) upperPoint[1]= p1[1];
258 if(p1[2] > upperPoint[2]) upperPoint[2]= p1[2];
260 if(p2[0] < lowerPoint[0]) lowerPoint[0]= p2[0];
261 if(p2[1] < lowerPoint[1]) lowerPoint[1]= p2[1];
262 if(p2[2] < lowerPoint[2]) lowerPoint[2]= p2[2];
264 if(p2[0] > upperPoint[0]) upperPoint[0]= p2[0];
265 if(p2[1] > upperPoint[1]) upperPoint[1]= p2[1];
266 if(p2[2] > upperPoint[2]) upperPoint[2]= p2[2];
271 kRestr.
init( lowerPoint, upperPoint,
false );
272 if(simplePredicate(p2)){
274 imageResuSegmentation,
276 }
else if (vm.count(
"labelBackground")){
278 imageResuSegmentation,
void progressBar(const double currentValue, const double maximalValue)
std::set< SCell > SurfelSet
static TContainer import(const std::string &filename, std::vector< unsigned int > dimSpace=std::vector< unsigned int >())
Dimension sOrthDir(const SCell &s) const
bool init(const Point &lower, const Point &upper, bool isClosed)
SCell sIncident(const SCell &c, Dimension k, bool up) const
Point sCoords(const SCell &c) const
Trace trace(traceWriterTerm)
const Domain & domain() const
unsigned static int uFillInterior(const KSpace &aKSpace, const TSurfelPredicate &aSurfPred, TImageContainer &anImage, const typename TImageContainer::Value &aValue, bool empty_is_inside=false, bool incrementMode=true)
std::vector< Value >::const_iterator ConstIterator
unsigned static int uFillExterior(const KSpace &aKSpace, const SurfelPredicate &aSurfPred, TImageContainer &anImage, const typename TImageContainer::Value &aValue, bool empty_is_outside=true, bool incrementMode=true)