<div dir="ltr">Hello,<br><br>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.<br>
<br>Thank You Peter<br><br><br>#if defined(_MSC_VER)<br>#pragma warning ( disable : 4786 )<br>#endif<br><br>#ifdef __BORLANDC__<br>#define ITK_LEAN_AND_MEAN<br>#endif<br><br><br>#include "itkActiveShapeModelCalculator.h"<br>
#include "itkActiveShapeModelGradientSearchMethod.h"<br><br>#include "itkImage.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br>#include "itkImageSeriesReader.h"<br>
#include "itkNumericSeriesFileNames.h"<br>#include "itkPNGImageIO.h"<br>#include "itkArray.h"<br><br>#include <stdio.h><br>#include <math.h><br>#include <cstdlib> <br>#include <ctime> <br>
#include <iostream><br>#include <fstream><br><br>using namespace std;<br><br>// ASM Calculator<br>// input: binary volume containing the training shapes<br>// output: mean shape, eigenvectors, eigenvalues<br><br>
int main( int argc, char * argv[] )<br>{<br><br> typedef itk::Image< unsigned char, 3 > ImageType;<br> typedef itk::ImageFileReader< ImageType > ReaderType;<br><br> ReaderType::Pointer reader = ReaderType::New();<br>
reader->SetFileName( argv[1] );<br><br> typedef itk::ActiveShapeModelCalculator< ImageType >ASMCalculatorType;<br> ASMCalculatorType::Pointer asm_calculator = ASMCalculatorType::New();<br><br> asm_calculator->SetImage( reader->GetOutput() );<br>
std::cout << "Tolerance " << asm_calculator->GetTolerance() << std::endl;<br> //asm_calculator->SetTolerance(0);<br> try<br> {<br> asm_calculator->GenerateData();<br>
}<br> catch ( itk::ExceptionObject & err )<br> {<br> std::cerr << "Exception Object caught!" << std::endl;<br> std::cerr << err << std::endl;<br> return EXIT_FAILURE;<br>
}<br><br> std::cout<<std::endl;<br> asm_calculator->Print( std::cout );<br> std::cout<<std::endl;<br><br> itk::Array<double> meanShape;<br> meanShape = asm_calculator->GetMeanShape();<br>
std::cout << "MeanShape " << std::endl;<br> std::cout << "NumberofElements " << meanShape.GetNumberOfElements() << std::endl;<br> for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i++ )<br>
{<br> std::cout << i << ": " << meanShape.GetElement(i) << std::endl;<br> }<br><br> double **meanShapeIM = new double *[256];<br> for(int i=0; i < 256; i++){<br> meanShapeIM[i] = new double [256];<br>
for(int j=0; j < 256; j++){<br> meanShapeIM[i][j] = 0;<br> }<br> }<br><br> for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i = i + 2 )<br> {<br> meanShapeIM[(int)meanShape.GetElement(i)][(int)meanShape.GetElement(i+1)] = 255;<br>
}<br><br> fstream out("meanShape.raw", ios::out | ios::binary);<br> for(int x=0;x<256;x++){<br> for(int z=0;z<256;z++){<br> out.write((char*)&meanShapeIM[z][x], sizeof(double));<br>
}<br> }<br> out.close();<br><br> itk::Array<double> Eigenvalues;<br> Eigenvalues = asm_calculator->GetEigenvalues();<br> std::cout << "Eigenvalues " << std::endl;<br> std::cout << "NumberofElements " << Eigenvalues.GetNumberOfElements() << std::endl;<br>
for ( unsigned int i=0; i<Eigenvalues.GetNumberOfElements(); i++ )<br> {<br> std::cout << i << ": " << Eigenvalues.GetElement(i) << std::endl;<br> }<br> typedef vnl_matrix<double> MatrixOfDoubleType;<br>
MatrixOfDoubleType Eigenvector = asm_calculator->GetEigenvector();<br> std::cout << "Eigenvectors: " << std::endl;<br> for ( unsigned int i = 0; i < Eigenvalues.GetNumberOfElements(); ++i )<br>
{<br> std::cout << Eigenvector.get_row( i ) << std::endl;<br> }<br>getchar();<br> typedef itk::Image< char, 2 > Image2D;<br> typedef itk::ActiveShapeModelGradientSearchMethod< Image2D >ASMFinderType;<br>
ASMFinderType::Pointer asm_finder = ASMFinderType::New();<br><br> typedef itk::ImageFileReader< Image2D > Reader2DType;<br><br> Reader2DType::Pointer reader2D = Reader2DType::New();<br> reader2D->SetFileName( argv[2] );<br>
reader2D->Update();<br><br> asm_finder->SetImage( reader2D->GetOutput() );<br><br> asm_finder->SetMeanShape( asm_calculator->GetMeanShape() );<br> asm_finder->SetEigenValues( asm_calculator->GetEigenvalues() );<br>
asm_finder->SetEigenVectors( asm_calculator->GetEigenvector() );<br> <br> asm_finder->Modified();<br> std::cout<<std::endl;<br> asm_finder->Print( std::cout );<br> std::cout<<std::endl;<br>
<br> asm_finder->GenerateData();<br><br> asm_finder->Print( std::cout );<br><br> itk::Array<double> Shape;<br> Shape = asm_finder->GetNewShape();<br> for ( unsigned int i=0; i<Shape.GetNumberOfElements(); i++ )<br>
{<br> //std::cout << i << ": " << Shape.GetElement(i) << std::endl;<br> }<br><br> char **ShapeIM = new char *[256];<br> for(int i=0; i < 256; i++){<br> ShapeIM[i] = new char [256];<br>
for(int j=0; j < 256; j++){<br> ShapeIM[i][j] = 0;<br> }<br> }<br><br> for ( unsigned int i=0; i<meanShape.GetNumberOfElements(); i = i + 2 )<br> {<br> ShapeIM[(int)Shape.GetElement(i)][(int)Shape.GetElement(i+1)] = 128;<br>
}<br><br> fstream out1("finalShape.raw", ios::out | ios::binary);<br> for(int x=0;x<256;x++){<br> for(int z=0;z<256;z++){<br> out1.write((char*)&ShapeIM[z][x], sizeof(char));<br>
}<br> }<br> out1.close();<br><br><br> return EXIT_SUCCESS;<br>}<br><br></div>