19 #include <boost/lexical_cast.hpp>
22 #include <itksys/SystemTools.hxx>
28 #include <itkImageRegionIterator.h>
40 #include <QStringList>
53 int main(
int argc,
char* argv[])
73 parser.
setTitle(
"Native Head CT Segmentation");
78 std::map<std::string, us::Any> parsedArgs = parser.
parseArguments(argc, argv);
79 if (parsedArgs.size()==0)
82 std::string inputdir =
us::any_cast<std::string>(parsedArgs[
"inputdir"]);
83 std::string outputdir =
us::any_cast<std::string>(parsedArgs[
"outputdir"]);
91 std::string filt_select = parsedArgs.count(
"select") ?
us::any_cast<std::string>(parsedArgs[
"select"]) :
"*.mitk";
93 QString filter(filt_select.c_str());
94 QDir dir(inputdir.c_str());
95 auto strl = dir.entryList(filter.split(
" "),QDir::Files);
99 uint n_samples = 45000;
100 uint n_samples_per_image = n_samples / strl.size();
104 Eigen::MatrixXd feature_matrix(n_samples, n_features);
105 Eigen::MatrixXi label_matrix(n_samples, 1);
109 for(
auto entry : strl)
123 feature_matrix.block(pos, 0, n_samples_per_image, 1) = mitk::CLUtil::Transform<double>(raw_image,brain_mask_sampled);
124 feature_matrix.block(pos, 1, n_samples_per_image, 1) = mitk::CLUtil::Transform<double>(csf_prob,brain_mask_sampled);
126 label_matrix.block(pos, 0, n_samples_per_image, 1) = mitk::CLUtil::Transform<int>(class_mask, brain_mask_sampled);
127 pos += n_samples_per_image;
129 classifier->Train(feature_matrix, label_matrix);
130 classifier->PrintParameter();
133 std::map<std::string, std::pair<double, double> > map_error;
135 for(
auto entry: strl)
151 Eigen::MatrixXd feature_matrix(count,n_features);
152 feature_matrix.block(0, 0, count, 1) = mitk::CLUtil::Transform<double>(raw_image,brain_mask);
153 feature_matrix.block(0, 1, count, 1) = mitk::CLUtil::Transform<double>(csf_prob,brain_mask);
155 mitk::Image::Pointer result_mask = mitk::CLUtil::Transform<int>(classifier->Predict(feature_matrix),brain_mask);
157 std::string name = itksys::SystemTools::GetFilenameWithoutExtension(entry.toStdString());
162 map_error[entry.toStdString()] = std::make_pair(std::abs(map_true[1] - map_pred[1]) / map_true[1], map_true[2] != 0 ? std::abs(map_true[2] - map_pred[2]) / map_true[2]: 0);
165 double mean_error_csf = 0;
166 double mean_error_les = 0;
167 double num_subjects = map_error.size();
168 for(
auto entry: map_error)
170 MITK_INFO(entry.first.c_str()) <<
"CSF error: " << entry.second.first <<
"%\t LES error: " << entry.second.second <<
"%";
171 mean_error_csf += entry.second.first;
172 mean_error_les += entry.second.second;
174 MITK_INFO(
"Mean") <<
"CSF error: " << mean_error_csf/num_subjects <<
"%\t LES error: " << mean_error_les/num_subjects <<
"%";
183 std::map<uint, uint>
map;
187 raw_image = map.size() <= 7 ?
dynamic_cast<mitk::Image *
>(so[0].GetPointer()) : dynamic_cast<mitk::Image *>(so[1].GetPointer());
188 class_mask = map.size() <= 7 ?
dynamic_cast<mitk::Image *
>(so[1].GetPointer()) : dynamic_cast<mitk::Image *>(so[0].GetPointer());
190 std::map<uint, uint> merge_instructions;
191 merge_instructions[0] = 0;
192 merge_instructions[1] = 1;
193 merge_instructions[2] = 1;
194 merge_instructions[3] = 1;
195 merge_instructions[4] = 2;
196 merge_instructions[5] = 3;
197 merge_instructions[6] = 3;
200 brain_mask = class_mask->Clone();
202 merge_instructions[0] = 0;
203 merge_instructions[1] = 1;
204 merge_instructions[2] = 1;
205 merge_instructions[3] = 1;
206 merge_instructions[4] = 1;
207 merge_instructions[5] = 1;
208 merge_instructions[6] = 1;
217 std::map<unsigned int, double> volume_map;
218 itk::ImageRegionConstIterator<itk::Image<unsigned int, 3>> it(img,img->GetLargestPossibleRegion());
222 if(volume_map.find(value)== volume_map.end())
223 volume_map[value] = 0;
228 auto spacing = img->GetSpacing();
229 double volumeUnit = spacing[0] * spacing[1] * spacing[2];
232 for(
auto entry: volume_map)
233 entry.second *= volumeUnit;
static void Save(const mitk::BaseData *data, const std::string &path)
Save a mitk::BaseData instance.
itk::SmartPointer< Self > Pointer
static void itkSampleLabel(TImageType1 *image, TImageType2 *output, double acceptrate, unsigned int label)
void setContributor(std::string contributor)
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
void ReadMitkProjectImageAndMask(std::string input_file, mitk::Image::Pointer &raw_image, mitk::Image::Pointer &class_mask, Image::Pointer &brain_mask)
std::map< std::string, us::Any > parseArguments(const StringContainerType &arguments, bool *ok=nullptr)
int main(int argc, char *argv[])
static void DilateGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
DilateGrayscale.
static void FillHoleGrayscale(mitk::Image::Pointer &image, mitk::Image::Pointer &outimage)
FillHoleGrayscale.
static void ErodeGrayscale(mitk::Image::Pointer &image, unsigned int radius, mitk::CLUtil::MorphologicalDimensions d, mitk::Image::Pointer &outimage)
ErodeGrayscale.
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)
#define AccessFixedDimensionByItk_2(mitkImage, itkImageTypeFunction, dimension, arg1, arg2)
Image class for storing images.
static void ProbabilityMap(const mitk::Image::Pointer &sourceImage, double mean, double std_dev, mitk::Image::Pointer &resultImage)
ProbabilityMap.
void setCategory(std::string category)
void setArgumentPrefix(const std::string &longPrefix, const std::string &shortPrefix)
std::map< unsigned int, double > VolumeUnderMaskByLabel(mitk::Image::Pointer mask)
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
static void MergeLabels(mitk::Image::Pointer &img, const std::map< unsigned int, unsigned int > &map)
MergeLabels.
MITKMATCHPOINTREGISTRATION_EXPORT ResultImageType::Pointer map(const InputImageType *input, const RegistrationType *registration, bool throwOnOutOfInputAreaError=false, const double &paddingValue=0, const ResultImageGeometryType *resultGeometry=NULL, bool throwOnMappingError=true, const double &errorValue=0, mitk::ImageMappingInterpolator::Type interpolatorType=mitk::ImageMappingInterpolator::Linear)
static DataStorage::SetOfObjects::Pointer Load(const std::string &path, DataStorage &storage)
Load a file into the given DataStorage.
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 itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.