// 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;
}
Esempio n. 2
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;
}