[Insight-users] ASM question?

Peter Noel question.to.peter at gmail.com
Thu Sep 18 10:12:01 EDT 2008


Hello,

I'm trying to use the active shape model implementations which is part of
ITK. In the later part of my email I attached my code, which compiles fine
and runs with our crashed, but I do not get any usable results back. Which
means the code does return a shape but it does not match anything in the
image (wich is basically an unkown instance).  For the training part, it
does return a valid mean shape. I was hopping that somebody maybe can help
here, who may know more about the itk asm implmentation.

Thank You Peter


#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif


#include "itkActiveShapeModelCalculator.h"
#include "itkActiveShapeModelGradientSearchMethod.h"

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesReader.h"
#include "itkNumericSeriesFileNames.h"
#include "itkPNGImageIO.h"
#include "itkArray.h"

#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <fstream>

using namespace std;

// ASM Calculator
// input: binary volume containing the training shapes
// output: mean shape, eigenvectors, eigenvalues

int main( int argc, char * argv[] )
{

    typedef itk::Image< unsigned char, 3 >      ImageType;
    typedef itk::ImageFileReader< ImageType >   ReaderType;

    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( argv[1] );

    typedef itk::ActiveShapeModelCalculator< ImageType >ASMCalculatorType;
    ASMCalculatorType::Pointer asm_calculator = ASMCalculatorType::New();

    asm_calculator->SetImage( reader->GetOutput() );
    std::cout << "Tolerance " << asm_calculator->GetTolerance() <<
std::endl;
    //asm_calculator->SetTolerance(0);
    try
    {
        asm_calculator->GenerateData();
    }
    catch ( itk::ExceptionObject & err )
    {
        std::cerr << "Exception Object caught!" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_FAILURE;
    }

    std::cout<<std::endl;
    asm_calculator->Print( std::cout );
    std::cout<<std::endl;

    itk::Array<double> meanShape;
    meanShape = asm_calculator->GetMeanShape();
    std::cout << "MeanShape " << std::endl;
    std::cout << "NumberofElements " << meanShape.GetNumberOfElements() <<
std::endl;
    for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i++ )
    {
        std::cout << i << ": " << meanShape.GetElement(i) << std::endl;
    }

    double **meanShapeIM = new double *[256];
    for(int i=0; i < 256; i++){
        meanShapeIM[i] = new double [256];
        for(int j=0; j < 256; j++){
            meanShapeIM[i][j] = 0;
        }
    }

    for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i = i + 2 )
    {

meanShapeIM[(int)meanShape.GetElement(i)][(int)meanShape.GetElement(i+1)] =
255;
    }

    fstream out("meanShape.raw", ios::out | ios::binary);
    for(int x=0;x<256;x++){
        for(int z=0;z<256;z++){
            out.write((char*)&meanShapeIM[z][x], sizeof(double));
        }
    }
    out.close();

    itk::Array<double> Eigenvalues;
    Eigenvalues = asm_calculator->GetEigenvalues();
    std::cout << "Eigenvalues " << std::endl;
    std::cout << "NumberofElements " << Eigenvalues.GetNumberOfElements() <<
std::endl;
    for ( unsigned int i=0; i<Eigenvalues.GetNumberOfElements(); i++ )
    {
        std::cout << i << ": " << Eigenvalues.GetElement(i) << std::endl;
    }
    typedef vnl_matrix<double> MatrixOfDoubleType;
    MatrixOfDoubleType Eigenvector = asm_calculator->GetEigenvector();
    std::cout << "Eigenvectors: " << std::endl;
    for ( unsigned int i = 0; i < Eigenvalues.GetNumberOfElements(); ++i )
    {
        std::cout << Eigenvector.get_row( i ) << std::endl;
    }
getchar();
    typedef itk::Image< char, 2 >      Image2D;
    typedef itk::ActiveShapeModelGradientSearchMethod< Image2D
>ASMFinderType;
    ASMFinderType::Pointer asm_finder = ASMFinderType::New();

    typedef itk::ImageFileReader< Image2D >   Reader2DType;

    Reader2DType::Pointer reader2D = Reader2DType::New();
    reader2D->SetFileName( argv[2] );
    reader2D->Update();

    asm_finder->SetImage( reader2D->GetOutput() );

    asm_finder->SetMeanShape( asm_calculator->GetMeanShape() );
    asm_finder->SetEigenValues( asm_calculator->GetEigenvalues() );
    asm_finder->SetEigenVectors( asm_calculator->GetEigenvector() );

    asm_finder->Modified();
    std::cout<<std::endl;
    asm_finder->Print( std::cout );
    std::cout<<std::endl;

    asm_finder->GenerateData();

    asm_finder->Print( std::cout );

    itk::Array<double> Shape;
    Shape = asm_finder->GetNewShape();
    for ( unsigned int i=0; i<Shape.GetNumberOfElements(); i++ )
    {
        //std::cout << i << ": " << Shape.GetElement(i) << std::endl;
    }

    char **ShapeIM = new char *[256];
    for(int i=0; i < 256; i++){
        ShapeIM[i] = new char [256];
        for(int j=0; j < 256; j++){
            ShapeIM[i][j] = 0;
        }
    }

    for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i = i + 2 )
    {
        ShapeIM[(int)Shape.GetElement(i)][(int)Shape.GetElement(i+1)] = 128;
    }

    fstream out1("finalShape.raw", ios::out | ios::binary);
    for(int x=0;x<256;x++){
        for(int z=0;z<256;z++){
            out1.write((char*)&ShapeIM[z][x], sizeof(char));
        }
    }
    out1.close();


    return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20080918/665ec156/attachment.htm>


More information about the Insight-users mailing list