// itk
#include <itkExtractImageFilter.h>
-#include <itkImageToVTKImageFilter.h>
//------------------------------------------------------------------------------
/**Converts the itk image to vv, handling the 4D problem
if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
{
- typedef itk::Image< PixelType, Dim - 1 > ConnectorImageType;
- typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
- typedef itk::ExtractImageFilter<InputImageType,ConnectorImageType> FilterType;
-
- typename FilterType::Pointer filter = FilterType::New();
- typename ConnectorType::Pointer connector = ConnectorType::New();
+ typedef itk::Image< PixelType, Dim - 1 > ItkImageType;
+ typedef itk::ExtractImageFilter<InputImageType, ItkImageType> FilterType;
//extract the 3D slices and put them in a std::vector<vtkImageData*>
typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
typename InputImageType::SizeType inputSize = inputRegion.GetSize();
-
+ typename InputImageType::IndexType start = inputRegion.GetIndex();
typename InputImageType::SizeType extractedRegionSize = inputSize;
typename InputImageType::RegionType extractedRegion;
extractedRegionSize[Dim - 1] = 0;
extractedRegion.SetSize(extractedRegionSize);
- filter->SetInput(input);
- connector->SetInput(filter->GetOutput());
-
- typename InputImageType::IndexType start = inputRegion.GetIndex();
-
for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
start[Dim - 1] = i;
extractedRegion.SetIndex(start);
+
+ typename FilterType::Pointer filter = FilterType::New();
filter->SetExtractionRegion(extractedRegion);
+ filter->SetInput(input);
+ filter->ReleaseDataFlagOn();
+
try {
filter->Update();
}
<< " " << err << std::endl;
return vv_image;
}
- try {
- connector->Update();
- }
- catch ( itk::ExceptionObject & err ) {
- std::cerr << "Error while setting vvImage from ITK (Dim==4) [Connect phase]"
- << " " << err << std::endl;
- return vv_image;
- }
- vtkImageData *image = vtkImageData::New();
- image->DeepCopy(connector->GetOutput());
- vv_image->AddImage(image);
+ vv_image->AddItkImage<ItkImageType>(filter->GetOutput());
}
vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]);
- vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
+ vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]);
}
else //Dim == 1,2,3 and not time_sequence
{
- typedef itk::Image< PixelType, Dim > ConnectorImageType;
- typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
- typename ConnectorType::Pointer connector = ConnectorType::New();
- connector->SetInput(input);
-
- try {
- connector->Update();
- }
- catch ( itk::ExceptionObject & err ) {
- std::cerr << "Error while setting vvImage from ITK (Dim==3)"
- << " " << err << std::endl;
- return vv_image;
- }
- vtkImageData *image = vtkImageData::New();
- image->DeepCopy(connector->GetOutput());
- vv_image->AddImage(image);
+ vv_image->AddItkImage<InputImageType>(input);
}
return vv_image;
}
//--------------------------------------------------------------------
void vvImage::Reset()
{
- for (unsigned int i = 0; i < mVtkImages.size(); i++)
-#ifdef NO_RESLICE
- mVtkImages[i]->Delete();
-#else
- mVtkImageReslice[i]->GetInput()->Delete();
-#endif
-
mVtkImages.resize(0);
mVtkImageReslice.resize(0);
}
//--------------------------------------------------------------------
-//--------------------------------------------------------------------
-void vvImage::SetImage(std::vector< vtkImageData* > images)
-{
- Reset();
- for (unsigned int i = 0; i < images.size(); i++)
- AddImage(images[i]);
-}
-//--------------------------------------------------------------------
-
-
-//--------------------------------------------------------------------
-void vvImage::AddImage(vtkImageData* image)
-{
-#ifdef NO_RESLICE
- mVtkImages.push_back(image);
- return;
-#endif
-
- mVtkImageReslice.push_back(vtkSmartPointer<vtkImageReslice>::New());
- mVtkImageReslice.back()->SetInterpolationModeToLinear();
- mVtkImageReslice.back()->AutoCropOutputOn();
- mVtkImageReslice.back()->SetBackgroundColor(-1000,-1000,-1000,1);
- mVtkImageReslice.back()->SetResliceTransform(mTransform);
- mVtkImageReslice.back()->SetInput(0, image);
- mVtkImageReslice.back()->Update();
- mVtkImages.push_back( mVtkImageReslice.back()->GetOutput(0) );
-}
-//--------------------------------------------------------------------
-
//--------------------------------------------------------------------
int vvImage::GetNumberOfSpatialDimensions()
{
}
//--------------------------------------------------------------------
-
-//--------------------------------------------------------------------
-vtkImageData * CopyAndCastToFloatFrom(vtkImageData * input)
-{
- vtkImageData * p = vtkImageData::New();
- p->SetExtent(input->GetExtent ()); // Only first ! could not be 4D
- p->SetScalarTypeToFloat();
- p->AllocateScalars();
- p->CopyAndCastFrom(input, input->GetExtent());
- return p;
-}
-//--------------------------------------------------------------------
-
-
#endif // VVIMAGE_CXX
#include <iostream>
#include <vector>
#include <itkObjectFactory.h>
+#include <itkProcessObject.h>
#include <vtkSmartPointer.h>
class vtkImageData;
public :
typedef vvImage Self;
typedef itk::SmartPointer<Self> Pointer;
+ typedef itk::ProcessObject::Pointer ConverterPointer;
itkNewMacro(Self);
void Init();
void Reset();
- void SetImage(std::vector<vtkImageData*> images);
- void AddImage(vtkImageData* image);
+ template<class TItkImageType> void AddItkImage(TItkImageType *input);
const std::vector<vtkImageData*>& GetVTKImages();
vtkImageData* GetFirstVTKImageData();
int GetNumberOfDimensions() const;
vvImage();
~vvImage();
+ std::vector< ConverterPointer > mItkToVtkConverters;
std::vector<vtkImageData*> mVtkImages;
std::vector< vtkSmartPointer<vtkImageReslice> > mVtkImageReslice;
vtkSmartPointer<vtkTransform> mTransform;
};
//------------------------------------------------------------------------------
-//------------------------------------------------------------------------------
-vtkImageData * CopyAndCastToFloatFrom(vtkImageData * p);
-//------------------------------------------------------------------------------
+#include "vvImage.txx"
#endif
--- /dev/null
+#include <itkImageToVTKImageFilter.h>
+
+//--------------------------------------------------------------------
+template<class TItkImageType>
+void vvImage::AddItkImage(TItkImageType *input)
+{
+ typedef itk::ImageToVTKImageFilter <TItkImageType> ConverterType;
+ typename ConverterType::Pointer converter = ConverterType::New();
+ converter->SetInput(input);
+
+ try {
+ converter->Update();
+ }
+ catch ( itk::ExceptionObject & err ) {
+ std::cerr << "Error while setting vvImage from ITK"
+ << " " << err << std::endl;
+ }
+ mItkToVtkConverters.push_back(dynamic_cast< itk::ProcessObject *>(converter.GetPointer()));
+ mVtkImages.push_back(converter->GetOutput());
+
+ //mVtkImageReslice.push_back(vtkSmartPointer<vtkImageReslice>::New());
+ //mVtkImageReslice.back()->SetInterpolationModeToLinear();
+ //mVtkImageReslice.back()->AutoCropOutputOn();
+ //mVtkImageReslice.back()->SetBackgroundColor(-1000,-1000,-1000,1);
+ //mVtkImageReslice.back()->SetResliceTransform(mTransform);
+ //mVtkImageReslice.back()->SetInput(0, image);
+ //mVtkImageReslice.back()->Update();
+ //mVtkImages.push_back( mVtkImageReslice.back()->GetOutput(0) );
+}
+//--------------------------------------------------------------------
+
// one at the time to avoid excessive
// memory use
{
- typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
- typedef itk::ImageFileReader<InputImageType> ReaderType;
- typename ReaderType::Pointer reader = ReaderType::New();
- typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
- typename ConnectorType::Pointer connector = ConnectorType::New();
- connector->SetInput(reader->GetOutput());
- mImage=vvImage::New();
- for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
+ mImage=vvImage::New();
+ for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
+ typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
+ typedef itk::ImageFileReader<InputImageType> ReaderType;
+ typename ReaderType::Pointer reader = ReaderType::New();
+ reader->ReleaseDataFlagOn();
std::cout << (*i) << std::endl;
reader->SetFileName(*i);
try {
mLastError = error.str();
return;
}
- try {
- connector->Update();
- } catch ( itk::ExceptionObject & err ) {
- std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
- << " " << err << std::endl;
- }
- vtkImageData *image = vtkImageData::New();
- image->ShallowCopy(connector->GetOutput());
- mImage->AddImage(image);
+ mImage->AddItkImage<InputImageType>(reader->GetOutput());
}
} else {
if (mInputFilenames.size() > 1) {
#ifndef VVTOOLINPUTSELECTORWIDGET_CXX
#define VVTOOLINPUTSELECTORWIDGET_CXX
+// clitk
+#include <clitkImageCommon.h>
+
// vv
#include "vvLabelImageLoaderWidget.h"
#include "vvSlicerManager.h"
"",Extensions); //mMainWindow->GetInputPathName()
if (filename == "") return; // nothing to do
+ itk::ImageIOBase::Pointer header = clitk::readImageHeader(filename.toStdString());
+
// Open Image
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
- vvImageReader * mReader = new vvImageReader;
- mReader->SetInputFilename(filename.toStdString());
- mReader->Update(IMAGE);
- if (mReader->GetLastError().size() != 0) {
+ if (!header) {
std::cerr << "Error while reading " << filename.toStdString() << std::endl;
- QString error = "Cannot open file \n";
- error += mReader->GetLastError().c_str();
+ QString error = QString("Cannot open file %1\n").arg(filename);
QMessageBox::information(this,tr("Reading problem"),error);
- delete mReader;
return;
}
// Create output pointer
- m_Output = vvImage::New();
-
- // Check type and convert if needed
- vvImage::Pointer temp = mReader->GetOutput();
-
- if (temp->GetNumberOfDimensions() != 3) {
+ if (header->GetNumberOfDimensions() != 3) {
std::cerr << "Error while reading " << filename.toStdString() << std::endl;
QString error;
error = QString("Cannot open file %1 because it is not 3D\n").arg(filename);
QMessageBox::information(this,tr("Reading problem"),error);
- delete mReader;
return;
}
- if (temp->GetScalarTypeAsITKString() != "unsigned_char") {
- vtkImageData * p = vtkImageData::New();
- p->SetExtent(temp->GetFirstVTKImageData()->GetExtent ()); // Only first ! could not be 4D
- p->SetScalarTypeToUnsignedChar();
- p->AllocateScalars ();
- p->CopyAndCastFrom(temp->GetFirstVTKImageData(), temp->GetFirstVTKImageData()->GetExtent ());
- m_Output->AddImage(p);
- vvImageWriter * writer = new vvImageWriter;
- writer->SetOutputFileName("a.mhd");
- writer->SetInput(m_Output);
- writer->Update();
- }
- else {
- m_Output = temp;
- }
-
+ // Convert to unsigned char while reading (if not already)
+ typedef itk::ImageFileReader< itk::Image< unsigned char, 3 > > ReaderType;
+ ReaderType::Pointer reader = ReaderType::New();
+ reader->SetFileName(filename.toStdString());
+ reader->Update();
+
+ // Create vv image
+ m_Output = vvImage::New();
+ m_Output->AddItkImage( reader->GetOutput() );
+
// Set GUI
mLabelInputInfo->setText(vtksys::SystemTools::GetFilenameName(filename.toStdString()).c_str());
QApplication::restoreOverrideCursor();