[vtkusers] how to write the MetaImage header

diaoxianfen diaoxianfen at asisz.com
Wed Jul 7 04:49:43 EDT 2004


I am doing some image processing things.The process is read image,select interested region of image,resample the interested region image data,then write the data as  MetaImage. But I don't know how to write the MetaImage header .The program I writed as follows,I can view the slice along z axis,but when I use the InsightSNAP to load the writed image,the dimensions and spacing are wrong.Help me!

//
 //read  "E:/cochlea/DICOM/PA1/ST1/SE1"
 DicomReader->SetDirectoryName(filepath);
 DicomReader->SetDataSpacing(0.412109,0.412109,1.250000);

 int i,j,k;
   //vtkImageExport *exporter = vtkImageExport::New();
 exporter->SetInput(DicomReader->GetOutput());
 exporter->ImageLowerLeftOn();//Lower left
  // get info from exporter and create array to hold data
 int memsize = exporter->GetDataMemorySize();
 int *dimensions = exporter->GetDataDimensions();
 // export the data into the array
 short *data = new short[memsize/sizeof(short)];
 exporter->Export(data); 

     // interested region : 310-370,190-250,16-45
  vtkImageData *interestRegion=vtkImageData::New();
  interestRegion->SetDimensions(61,61,30);//310-370,190-250,16-45
  interestRegion->SetScalarTypeToShort();//
  interestRegion->SetNumberOfScalarComponents(1);
  interestRegion->AllocateScalars();
  interestRegion->SetSpacing(0.412109,0.412109,1.25);
  interestRegion->SetOrigin(0.0,0.0,0.0);
  //310-370,190-250,16-45;61*61*30
 short *ptr =(short*)interestRegion->GetScalarPointer();
  for(i=0;i<30;i++)//z
  {
   for(j=0;j<61;j++)//y
   {
    for(k=0;k<61;k++)//x
    {
     ptr[k + 61*(j + 61*i)]=data[k+310+dimensions[0]*(j+190 + dimensions[1]*(i+16))];
    }
   }
  }
//sample
  imageResample->SetDimensionality(3);
  imageResample->SetInput(interestRegion);
  imageResample->SetAxisOutputSpacing(0,0.2);//spacing[3]
  imageResample->SetAxisOutputSpacing(1,0.2);
  imageResample->SetAxisOutputSpacing(2,0.2);
  imageResample->SetInterpolationModeToLinear();
  imageResample->SetOutputExtentToDefault();
  imageResample->ReleaseDataFlagOff();
  
  vtkImageExport *export = vtkImageExport::New();
  export->SetInput(imageResample->GetOutput());
  export->ImageLowerLeftOn();//Lower left
  // get info from exporter and create array to hold data
  int memorysize = export->GetDataMemorySize();
  int *Samdimensions = export->GetDataDimensions();
  // export the data into the array
  short *id = new short[memorysize/sizeof(short)];
  export->Export(id); 
  vtkImageImport *import = vtkImageImport::New();
  import->SetWholeExtent(1,Samdimensions[0],1,Samdimensions[1],1,Samdimensions[2]);
  import->SetDataExtentToWholeExtent();
  import->SetDataScalarTypeToShort();
  import->SetImportVoidPointer(id);
 //write to MetaImage 
  metaImageWriter->SetFileDimensionality(3);
    metaImageWriter->SetInput(import->GetOutput());
   metaImageWriter->SetFileName("Sampledimage.mhd");
  metaImageWriter->Write();  

  // view Z slice
  viewer->SetInput(imageResample->GetOutput());//interestRegion
  viewer->SetZSlice(120);
  viewer->SetColorWindow(2000);
  viewer->SetColorLevel(750);
  viewer->Render();  

  delete [] data;
  delete [] id;
  export->Delete();
  import->Delete();
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20040707/5d43c731/attachment.htm>


More information about the vtkusers mailing list