// Computes specificity of the model by comparing random samples of the model with the test mashes.
float specificity(Logger& logger, StatisticalModelType::Pointer model, const MeshDataList& testMeshes, unsigned numberOfSamples) {


	// draw a number of samples and compute its distance to the closest training dataset
    double accumulatedDistToClosestTrainingShape = 0;
    for (unsigned i = 0; i < numberOfSamples; i++) {
        MeshType::Pointer sample = model->DrawSample();

        double minDist = std::numeric_limits<double>::max();
        for (MeshDataList::const_iterator it = testMeshes.begin(); it != testMeshes.end(); ++it) {
            MeshType::Pointer testMesh = it->first;

            // before we compute the distances between the meshes, we normalize the scale by scaling them
            // to optimally match the mean. This makes sure that models that include scale and those that have them normalized
            // ar etreated the same.
             MeshType::Pointer sampledScaledToMean = normalizeScale(sample, model->DrawMean());
             MeshType::Pointer testScaledToMean = normalizeScale(testMesh, model->DrawMean());

             double dist = computeAverageDistance(testScaledToMean, sampledScaledToMean, ConfigParameters::numSamplingPointsSpecificity);
            logger.Get(logINFO) << "distance " << dist << std::endl;
            if (dist < minDist) {
                minDist = dist;
            }
        }
        logger.Get(logINFO) << "closest distance for sample " << i << ": " << minDist << std::endl;

        accumulatedDistToClosestTrainingShape += minDist;
    }
    double avgDist = accumulatedDistToClosestTrainingShape / numberOfSamples;
    logger.Get(logINFO) << "average distance " << avgDist << std::endl;
    return avgDist;
}
void buildImageIntensityModelOnROI(const char* referenceFilename, const char* maskFilename, const char* dir, const char* outputImageFilename) {


	typedef itk::PCAModelBuilder<RepresenterType> ModelBuilderType;
	typedef itk::StatisticalModel<RepresenterType> StatisticalModelType;
    typedef std::vector<std::string> StringVectorType;
    typedef itk::DataManager<RepresenterType> DataManagerType;

    RepresenterType::Pointer representer = RepresenterType::New();

	typedef itk::ImageFileReader< ImageType > MaskReaderType;
	MaskReaderType::Pointer maskReader = MaskReaderType::New();
	maskReader->SetFileName( maskFilename );
    maskReader->Update();

	representer->SetReference( ReadImageFromFile(referenceFilename), maskReader->GetOutput() );

    StringVectorType filenames;
    getdir(dir, filenames, ".vtk");

    DataManagerType::Pointer dataManager = DataManagerType::New();
	dataManager->SetRepresenter(representer);

    for (StringVectorType::const_iterator it = filenames.begin(); it != filenames.end(); it++) {
        std::string fullpath = (std::string(dir) + "/") + *it;

        dataManager->AddDataset( ReadImageFromFile(fullpath), fullpath.c_str());
    }

	ModelBuilderType::Pointer pcaModelBuilder = ModelBuilderType::New();
    StatisticalModelType::Pointer model = pcaModelBuilder->BuildNewModel(dataManager->GetSampleDataStructure(), 0);

    std::cout<<"dimensionality of the data: "<<model->GetDomain().GetNumberOfPoints()<<", dimension of the images: "<<(*dataManager->GetSampleDataStructure().begin())->GetSample()->GetLargestPossibleRegion().GetNumberOfPixels()<<std::endl;

    std::cout<<"writing the mean sample to a png file..."<<std::endl;
    
	typedef itk::ImageFileWriter< ImageType > ImageWriterType;
	ImageWriterType::Pointer writer = ImageWriterType::New();
	writer->SetFileName( outputImageFilename );
	writer->SetInput(model->DrawSample());
    writer->Update();

}
Example #3
0
int main(int argc, char* argv[])
{
	//const char* SSMFile = argv[1];
	const char* SSMFile = "D:\\Workspace\\ASM\\projects\\LiverSegbyASM\\experiments\\buidModel\\shape\\output_20140815\\StatisticalShapeModel_20140815.h5";
	const char* targetMeshFile = "D:\\Workspace\\ASM\\projects\\LiverSegbyASM\\experiments\\training\\shape\\output_20140815\\liverMesh.46.vtk";
	const char* configFile = "D:\\Workspace\\LiverSegByASM\\liversegbyasm-v2\\Data\\config.txt";

	KM_DEBUG_INFO("Load config file...");
	km::Config::loadConfig(configFile);

	const int Dimension = 3;
	typedef double MeshPixelType;
	typedef itk::SimplexMeshRepresenter<MeshPixelType, Dimension> RepresenterType;
	typedef itk::StatisticalModel<RepresenterType>                StatisticalModelType;
	typedef RepresenterType::MeshType                             MeshType;
	
	StatisticalModelType::Pointer model = StatisticalModelType::New();
	model->Load( SSMFile );

	MeshType::Pointer meanShape = model->DrawMean();
	MeshType::PointType centroid = km::getMeshCentroid<MeshType>(meanShape);

	typedef itk::AffineTransform<double, Dimension> RigidTransformType;
	typedef itk::StatisticalShapeModelTransform<RepresenterType, double, Dimension> ShapeTransformType;
	ShapeTransformType::Pointer shapeTransform = ShapeTransformType::New();
	shapeTransform->SetStatisticalModel(model);
	shapeTransform->SetIdentity();

	RigidTransformType::Pointer rigidTransform = RigidTransformType::New();
	rigidTransform->SetCenter(centroid);
	rigidTransform->SetIdentity();

	typedef km::SSMUtils<MeshType, StatisticalModelType, RigidTransformType, ShapeTransformType> SSMUtilsType;
	SSMUtilsType ssmUtils;
	ssmUtils.SetSSM(model);
	ssmUtils.SetRigidTransform(rigidTransform);
	ssmUtils.SetShapeTransform(shapeTransform);
	ssmUtils.SetNumberOfClusters(km::g_number_clusters);
	ssmUtils.Initialize();

	MeshType::Pointer targetMesh = km::readMesh<MeshType>(targetMeshFile);
	MeshType::Pointer outputMesh = km::cloneMesh<MeshType, MeshType>(meanShape);

	km::writeMesh<MeshType>("targetMesh.vtk", targetMesh);
	km::writeMesh<MeshType>("unfittedMesh.vtk", meanShape);

	for (int i=0;i<1;i++)
	{
		std::cout<<"****************iter: "<<i<<"**************"<<std::endl;
		ssmUtils.Update(targetMesh, outputMesh);
		char filename[1024];
		sprintf(filename, "fittedMesh-%d.vtk", i);
		km::writeMesh<MeshType>(filename, outputMesh);

		ssmUtils.PrintTransform();
	}

	//km::assigneMesh<MeshType>(meanShape, 0);
	//ssmUtils.cluster(1);

	//for (int i=0;i<meanShape->GetNumberOfPoints();i++)
	//{
	//	meanShape->SetPointData(i, ssmUtils.getShapeCluster(i)->clusterId);
	//}
	//km::writeMesh<MeshType>("clusteredMesh.vtk", meanShape);

	system("pause");
	
	return 0;
}
float compactness(Logger& logger, StatisticalModelType::Pointer model) {
     return model->GetNumberOfPrincipalComponents();
}