18 #include <itksys/SystemTools.hxx> 24 #include <itkImageRegionIterator.h> 35 #include <QStringList> 43 int main(
int argc,
char* argv[])
64 parser.
setTitle(
"Random Forest Training");
69 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
70 if (parsedArgs.size()==0)
73 std::string inputdir =
us::any_cast<std::string>(parsedArgs[
"inputdir"]);
74 std::string outputdir =
us::any_cast<std::string>(parsedArgs[
"outputdir"]);
75 std::string classmask =
us::any_cast<std::string>(parsedArgs[
"classmask"]);
77 int treecount = parsedArgs.count(
"treecount") ?
us::any_cast<
int>(parsedArgs[
"treecount"]) : 50;
78 int treedepth = parsedArgs.count(
"treedepth") ?
us::any_cast<
int>(parsedArgs[
"treedepth"]) : 50;
79 int minsplitnodesize = parsedArgs.count(
"minsplitnodesize") ?
us::any_cast<
int>(parsedArgs[
"minsplitnodesize"]) : 2;
80 float precision = parsedArgs.count(
"precision") ?
us::any_cast<
float>(parsedArgs[
"precision"]) :
mitk::eps;
81 float fraction = parsedArgs.count(
"fraction") ?
us::any_cast<
float>(parsedArgs[
"fraction"]) : 0.6;
82 bool withreplacement = parsedArgs.count(
"replacment") ?
us::any_cast<
float>(parsedArgs[
"replacment"]) :
true;
83 std::string filt_select =
"*.nrrd";
85 QString filter(filt_select.c_str());
89 QDir dir(inputdir.c_str());
90 auto strl = dir.entryList(filter.split(
" "),QDir::Files);
94 unsigned int num_samples = 0;
98 Eigen::MatrixXd X(num_samples, strl.size());
100 for(
int i = 0 ; i < strl.size(); i++)
105 Eigen::MatrixXd _x = mitk::CLUtil::Transform<double>(img,
mask);
107 X.block(0,i,num_samples,1) = _x;
112 Eigen::MatrixXi Y = mitk::CLUtil::Transform<int>(
mask,
mask);
115 classifier->SetTreeCount(treecount);
116 classifier->SetMaximumTreeDepth(treedepth);
117 classifier->SetMinimumSplitNodeSize(minsplitnodesize);
118 classifier->SetPrecision(precision);
119 classifier->SetSamplesPerTree(fraction);
120 classifier->UseSampleWithReplacement(withreplacement);
122 classifier->PrintParameter();
123 classifier->Train(X,Y);
131 Eigen::MatrixXi Y_pred = classifier->Predict(X);
132 Eigen::MatrixXd Probs = classifier->GetPointWiseProbabilities();
134 MITK_INFO << Y_pred.rows() <<
" " << Y_pred.cols();
135 MITK_INFO << Probs.rows() <<
" " << Probs.cols();
void setContributor(std::string contributor)
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
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, mitkCommandLineParser::Channel channel=mitkCommandLineParser::Channel::None)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
static void Save(const mitk::BaseData *data, const std::string &path, bool setPathProperty=false)
Save a mitk::BaseData instance.
MITKCORE_EXPORT const ScalarType eps
mitk::Image::Pointer mask
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)