19 #include <boost/lexical_cast.hpp>
22 #include <itksys/SystemTools.hxx>
28 #include <itkImageRegionIterator.h>
39 #include <QStringList>
47 int main(
int argc,
char* argv[])
68 parser.
setTitle(
"Random Forest Training");
73 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
74 if (parsedArgs.size()==0)
77 std::string inputdir =
us::any_cast<std::string>(parsedArgs[
"inputdir"]);
78 std::string outputdir =
us::any_cast<std::string>(parsedArgs[
"outputdir"]);
79 std::string classmask =
us::any_cast<std::string>(parsedArgs[
"classmask"]);
81 int treecount = parsedArgs.count(
"treecount") ?
us::any_cast<
int>(parsedArgs[
"treecount"]) : 50;
82 int treedepth = parsedArgs.count(
"treedepth") ?
us::any_cast<
int>(parsedArgs[
"treedepth"]) : 50;
83 int minsplitnodesize = parsedArgs.count(
"minsplitnodesize") ?
us::any_cast<
int>(parsedArgs[
"minsplitnodesize"]) : 2;
84 float precision = parsedArgs.count(
"precision") ?
us::any_cast<
float>(parsedArgs[
"precision"]) :
mitk::eps;
85 float fraction = parsedArgs.count(
"fraction") ?
us::any_cast<
float>(parsedArgs[
"fraction"]) : 0.6;
86 bool withreplacement = parsedArgs.count(
"replacment") ?
us::any_cast<
float>(parsedArgs[
"replacment"]) :
true;
87 std::string filt_select =
"*.nrrd";
89 QString filter(filt_select.c_str());
93 QDir dir(inputdir.c_str());
94 auto strl = dir.entryList(filter.split(
" "),QDir::Files);
98 unsigned int num_samples = 0;
102 Eigen::MatrixXd X(num_samples, strl.size());
104 for(
int i = 0 ; i < strl.size(); i++)
109 Eigen::MatrixXd _x = mitk::CLUtil::Transform<double>(img,mask);
111 X.block(0,i,num_samples,1) = _x;
116 Eigen::MatrixXi Y = mitk::CLUtil::Transform<int>(mask,mask);
119 classifier->SetTreeCount(treecount);
120 classifier->SetMaximumTreeDepth(treedepth);
121 classifier->SetMinimumSplitNodeSize(minsplitnodesize);
122 classifier->SetPrecision(precision);
123 classifier->SetSamplesPerTree(fraction);
124 classifier->UseSampleWithReplacement(withreplacement);
126 classifier->PrintParameter();
127 classifier->Train(X,Y);
135 Eigen::MatrixXi Y_pred = classifier->Predict(X);
136 Eigen::MatrixXd Probs = classifier->GetPointWiseProbabilities();
138 MITK_INFO << Y_pred.rows() <<
" " << Y_pred.cols();
139 MITK_INFO << Probs.rows() <<
" " << Probs.cols();
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
void setContributor(std::string contributor)
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void addArgument(const std::string &longarg, const std::string &shortarg, Type type, const std::string &argLabel, const std::string &argHelp=std::string(), const us::Any &defaultValue=us::Any(), bool optional=true, bool ignoreRest=false, bool deprecated=false)
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
MITKCORE_EXPORT const ScalarType eps
int main(int argc, char *argv[])
void setTitle(std::string title)
static void CountVoxel(mitk::Image::Pointer image, std::map< unsigned int, unsigned int > &map)
CountVoxel.
void setDescription(std::string description)
static mitk::Image::Pointer LoadImage(const std::string &path)
LoadImage Convenience method to load an arbitrary mitkImage.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.