<div dir="ltr"><div>Hello,
<br><br>I was trying to get a matrix out of a 2D image as i need to
perform particular calculations. At first i open a 3D image and using
ExtractImageFilter i extract the 2D image collapsing the 3rd dimension.
There must be something wrong in the code as every time i try to perform
any kind of operation on the newly created matrix i get a segfault. It
might be really stupid but i haven't been able to spot it.
<br><br>Code:
<br><br> typedef float OutputPixelType;
<br><br> typedef itk::Image< OutputPixelType, 4 > InputImageType;
<br> typedef itk::Image< OutputPixelType, 3 > OutputImageType;
<br> typedef itk::Image< OutputPixelType, 2 > BidimensionalImageType;
<br><br> typedef itk::ImageFileReader< InputImageType > ReaderType;
<br> typedef itk::ImageFileReader< OutputImageType > OutputReaderType;
<br> typedef itk::ImageFileWriter< OutputImageType > WriterType;
<br><br></div> //mediaFilePath is the path to a 3D image (mha extension)<br><div><br> char* MediaFileName = (char*)mediaFilePath.c_str();
<br><br> OutputReaderType::Pointer reader = OutputReaderType::New();
<br> reader->SetFileName(MediaFileName);
<br> reader->Update();
<br><br> typedef itk::ExtractImageFilter< OutputImageType, BidimensionalImageType > BidimensionalExtractorType;
<br>
<br> BidimensionalExtractorType::Pointer extractor = BidimensionalExtractorType::New();
<br><br> extractor->InPlaceOn();
<br> extractor->SetDirectionCollapseToSubmatrix();
<br> try{
<br> reader->UpdateOutputInformation();
<br> }
<br> catch ( itk::ExceptionObject & exp ){
<br> return 0;// "Couldn't read selected Subject, format not supported.";
<br> }
<br><br> OutputImageType::RegionType inputRegion3 = reader->GetOutput()->GetLargestPossibleRegion();
<br> OutputImageType::SizeType size3 = inputRegion3.GetSize();
<br> size3[2] = 0;
<br><br> OutputImageType::IndexType start3 = inputRegion3.GetIndex();
<br> start3[2] = 0;
<br> OutputImageType::RegionType desiredRegion3;
<br> desiredRegion3.SetSize( size3 );
<br> desiredRegion3.SetIndex( start3 );
<br><br> extractor->SetExtractionRegion( desiredRegion3 );
<br><br> extractor->SetInput( reader->GetOutput() );
<br> //writer->SetInput( extractor->GetOutput() );
<br><br> int width = reader->GetOutput()->GetLargestPossibleRegion().GetSize()[0];
<br> int height = reader->GetOutput()->GetLargestPossibleRegion().GetSize()[1];
<br><br> const unsigned int rows = height;
<br> const unsigned int cols = width;
<br><br> typedef vnl_matrix_ref< OutputPixelType > MatrixType;
<br><br> BidimensionalImageType::Pointer image = extractor->GetOutput();
<br><br> MatrixType matrix( rows, cols, image->GetBufferPointer());
<br><br> matrix.data_array();
<br><br> std::cout << matrix[1][1] << std::endl;
<br><br>Thanks,
<br>Marco
</div></div>