[Insight-users] Reading DICOM set and writing jpeg
Eduardo - LNCC
camargo at lncc.br
Tue Mar 24 09:16:49 EDT 2009
Hi,
I`m trying to read set of dicom files, extract one slice and write it in
jpeg file. But the jpeg file is very strange, it appears in low resolution
(or something like that).
Could anybody help me? Sorry for my english
My code
typedef itk::Image < unsigned char, 2 > WriteImageType;
typedef itk::Image < float, 3> ImageType;
typedef itk::Image < float, 2> ImageType2D;
typedef itk::ImageToVTKImageFilter <ImageType> ConnectorType;
typedef itk::ImageToVTKImageFilter <ImageType2D> ConnectorType2D;
typedef itk::ImageSeriesReader< ImageType > ReaderType;
typedef itk::ExtractImageFilter< ImageType, ImageType2D > ExtractFilterType;
typedef itk::RescaleIntensityImageFilter< ImageType2D, WriteImageType >
RescalerType;
typedef itk::ImageFileWriter< WriteImageType > WriterType;
//-----------------------------------------------------------------------------
void ExtractSlice(ReaderType *reader, ExtractFilterType *extractFilter, int
slice, int axis )
{
ImageType::RegionType inputRegion =
reader->GetOutput()->GetLargestPossibleRegion();
ImageType::SizeType size = inputRegion.GetSize();
ImageType::IndexType start = inputRegion.GetIndex();
const unsigned int sliceNumber = slice;
if((axis > 2) || (axis < 0))
{
cout << "In file imAbstractFilter.cxx at method ExtractSlice(). Variable
axis must be between 2 and 0.\n" <<endl;
}
if( (sliceNumber > size[axis]) || (sliceNumber < start[axis]) )
{
cout << "In file imAbstractFilter.cxx at method ExtractSlice(). Variable
slice must be between "
<< size[axis] << " and " << start[axis] << ".\n" <<endl;
}
size[axis] = 0; // Setando e armazenado o eixo de interesse.
start[axis] = sliceNumber; // Setando e armazenando o slice de interesse.
ImageType::RegionType desiredRegion;
desiredRegion.SetSize( size );
desiredRegion.SetIndex( start );
extractFilter->SetExtractionRegion( desiredRegion );
extractFilter->SetInput( reader->GetOutput() );
try
{
extractFilter->Update();
}
catch( itk::ExceptionObject & excp )
{
cout << "In file imAbstractFilter.cxx at method ExtractSlice()\n" << endl;
}
}
//-----------------------------------------------------------------------------
void DICOMReader_Reslice(ConnectorType *connector, bool x, bool y, bool z)
{
vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
vtkImageViewer2 * viewer = vtkImageViewer2::New();
viewer->SetupInteractor( iren );
viewer->SetInput( connector->GetOutput() );
viewer->SetColorWindow( 256 );
viewer->SetColorLevel( 144 );
viewer->SetSlice(200);
if(x)
{
viewer->SetSliceOrientationToXY(); // orientação em X
}
else if(y)
{
viewer->SetSliceOrientationToYZ(); // orientação em Y
}
else if(z)
{
viewer->SetSliceOrientationToXZ(); // orientação em Z
}
viewer->GetRenderer()->ResetCamera();
viewer->Render();
iren->Start();
iren->Delete();
viewer->Delete();
}
//-----------------------------------------------------------------------------
int main( int argc, char **argv )
{
cout << "Lendo DICOM..." << endl;
char inputDirectory[256] = {"/home/workspace2/ImageLab/data/Ricardo"};
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New();
reader->SetImageIO( dicomIO );
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();
nameGenerator->SetUseSeriesDetails( true );
nameGenerator->SetDirectory( inputDirectory );
typedef std::vector< std::string > SeriesIdContainer;
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();
SeriesIdContainer::const_iterator seriesItr = seriesUID.begin();
SeriesIdContainer::const_iterator seriesEnd = seriesUID.end();
while( seriesItr != seriesEnd )
{
seriesItr++;
}
std::string seriesIdentifier;
seriesIdentifier = seriesUID.begin()->c_str();
typedef std::vector< std::string > FileNamesContainer;
FileNamesContainer fileNames;
fileNames = nameGenerator->GetFileNames( seriesIdentifier );
reader->SetFileNames( fileNames );
reader->Update();
//######################## Filtro
cout << "Escrevendo JPG..." << endl;
ExtractFilterType::Pointer ExtractFilter = ExtractFilterType::New();
ExtractSlice(reader, ExtractFilter, 200, 0 );
RescalerType::Pointer rescaleFilter = RescalerType::New();
rescaleFilter->SetOutputMinimum(0);
rescaleFilter->SetOutputMaximum(255);
rescaleFilter->SetInput( ExtractFilter->GetOutput() );
char outputFileName[256] = {"/home/workspace2/ImageLab/data/Ricardo.png"};
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( outputFileName );
writer->SetInput( rescaleFilter->GetOutput() );
writer->Update();
//######################## Conector para o Pipeline ITK => VTK
cout << "Conectando ITK ao VTK..." <<endl;
ConnectorType::Pointer connector = ConnectorType::New();
connector->SetInput( reader->GetOutput() );
DICOMReader_Reslice(connector, false, true, false);
return 0;
}
--
Atenciosamente,
Eduardo Camargo
Analista de Sistemas
HeMoLab - http://hemolab.lncc.br/equipe/desenv/eduardo.htm
Laboratório Nacional de Computação Científica - LNCC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20090324/553a3178/attachment.htm>
More information about the Insight-users
mailing list