Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

Commit ebdb1722 authored by soniayukiselmi's avatar soniayukiselmi
Browse files

FIXED Delete the nifti component

git-svn-id: svn+ssh://scm.forge.imag.fr/var/lib/gforge/chroot/scmrepos/svn/camitk/trunk/camitk@543 ec899d31-69d1-42ba-9299-647d76f65fb3
parent 63515fb0
# nifti component just needs to use the component_extension macro
camitk_extension(COMPONENT_EXTENSION NEEDS_ITK)
\ No newline at end of file
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include "NiftiImageComponent.h"
using namespace camitk;
//-- Qt
#include <QString>
#include <QTextStream>
#include <QFileInfo>
#include <QMessageBox>
//-- Vtk
#include <vtkImageData.h>
//-- Itk
#include <itkImageFileReader.h>
#include <itkOrientImageFilter.h>
#include <itkImageToVTKImageFilter.h>
#include <itkNiftiImageIO.h>
#include <itkImage.h>
// -------------------- constructor --------------------
NiftiImageComponent::NiftiImageComponent(const QString& fileName) throw(AbortException)
: ImageComponent(fileName) {
createComponent(fileName);
}
// -------------------- createComponent --------------------
void NiftiImageComponent::createComponent(const QString & filename) {
if (!filename.isEmpty()) {
vtkSmartPointer<vtkImageData> img = readVolume(filename);
if (img) {
setImageData(img, false);
// use file basename as default name
setName(QFileInfo(filename).baseName());
//setName(filename);
}
else {
throw (AbortException("Could not open file\n"));
}
}
else {
throw (AbortException("No filename found\n"));
}
}
// -------------------- readVolume --------------------
vtkSmartPointer<vtkImageData> NiftiImageComponent::readVolume(const QString& filename) {
typedef short PixelType;
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageToVTKImageFilter< ImageType > ConnectorType;
// To copy data
int dims[3];
int extent[6];
double origin[3];
double spacing[3];
itk::NiftiImageIO::Pointer l_NiftiIO = itk::NiftiImageIO::New();
bool IsNativeImageNifti = l_NiftiIO->itk::NiftiImageIO::CanReadFile(filename.toStdString().c_str());
if (IsNativeImageNifti){
vtkSmartPointer<vtkImageData> oldPointer = NULL;
vtkSmartPointer<vtkImageData> newImage = NULL;
ReaderType::Pointer reader = ReaderType::New();
// To convert itkImages to vtkImages
ConnectorType::Pointer connector = ConnectorType::New();
reader->SetFileName( filename.toStdString().c_str() );
reader->SetImageIO(l_NiftiIO);
reader->Update();
connector->SetInput(reader->GetOutput());
try {
connector->Update();
// Copy the output image because the filters will be removed
// and we want to keep the image for other vtk pipelines
oldPointer = connector->GetOutput();
newImage = vtkSmartPointer<vtkImageData>::New();
oldPointer->GetDimensions(dims);
oldPointer->GetExtent(extent);
oldPointer->GetOrigin(origin);
oldPointer->GetSpacing(spacing);
newImage->SetDimensions(dims);
newImage->SetExtent(extent);
newImage->SetOrigin(origin);
newImage->SetSpacing(spacing);
newImage->DeepCopy(oldPointer);
// set the original slices. Other orientation are build when/if required only.
newImage->Update();
} catch ( itk::ExceptionObject & err ) {
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return NULL;
}
oldPointer = NULL;
return newImage;
}
return 0;
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#ifndef NIFTI_IMAGES_MANAGER_H
#define NIFTI_IMAGES_MANAGER_H
#include <vtkImageData.h>
#include <vtkSmartPointer.h>
#include <ImageComponent.h>
/**This component manages nifti images (using ITK I/O helper class).
*
* For more info about image data format: http://nifti.nimh.nih.gov/nifti-1/
*/
class NiftiImageComponent : public camitk::ImageComponent {
Q_OBJECT
public:
/// default constructor: give it the name of the file containing the data
NiftiImageComponent(const QString&) throw (camitk::AbortException);
/// needed for deleting
virtual ~NiftiImageComponent() {};
virtual void createComponent(const QString&);
protected:
vtkSmartPointer<vtkImageData> readVolume(const QString& filename);
};
#endif // NIFTI_IMAGES_MANAGER_H
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include "NiftiImageComponentExtension.h"
#include "NiftiImageComponent.h"
using namespace camitk;
// save to itk
#include <itkImageFileWriter.h>
#include <itkVTKImageToImageFilter.h>
//-- Qt
#include <QMessageBox>
// --------------- declare the plugin -------------------
Q_EXPORT_PLUGIN2(NiftiImageComponentExtension, NiftiImageComponentExtension);
// --------------- getName -------------------
QString NiftiImageComponentExtension::getName() const {
return "NiftiImages Component";
}
// --------------- getDescription -------------------
QString NiftiImageComponentExtension::getDescription() const {
return QString("Manage any file type supported by itk and not by vtk in <b>CamiTK</b>.<br/>")
+ "For more info about image data format: <a href=\"http://nifti.nimh.nih.gov/nifti-1/\">http://nifti.nimh.nih.gov/nifti-1/</a><br/><ul>"
+ "<li>NIfTI-1 is adapted from the widely used ANALYZE 7.5 file format.</li>"
+ "<li>hdr is the Analyse header file format</li>"
+ "(c)TIMC-IMAG 2009-2012";
}
// --------------- getFileExtensions -------------------
QStringList NiftiImageComponentExtension::getFileExtensions() const {
QStringList ext;
ext << "hdr" ;
return ext;
}
// --------------- open -------------------
Component * NiftiImageComponentExtension::open(const QString & fileName) throw (AbortException) {
NiftiImageComponent * res = NULL;
try {
res = new NiftiImageComponent(fileName);
}
catch (AbortException e) {
throw e;
}
return res;
}
// --------------- save -------------------
bool NiftiImageComponentExtension::save(Component * c) const {
bool succeed = false;
ImageComponent *img = dynamic_cast<ImageComponent *>(c);
if (!img)
return succeed;
// Instanciate the template function saveImage
// according to the input image type.
// Here by default, the Ouput pixel type corresponds to the input pixel type.
int * dims = img->getImageData()->GetDimensions();
int dim = 0;
if (dims[2] == 0)
dim = 2;
else
dim = 3;
vtkSmartPointer<vtkImageData> data = img->getImageData();
int scalarType = data->GetScalarType();
switch (scalarType) {
case VTK_CHAR :
if (dim == 2) {
typedef itk::Image<char, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<char, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_UNSIGNED_CHAR :
if (dim == 2) {
typedef itk::Image<unsigned char, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<unsigned char, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_SIGNED_CHAR :
if (dim == 2) {
typedef itk::Image<signed char, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<signed char, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_SHORT :
if (dim == 2) {
typedef itk::Image<short, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<short, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_UNSIGNED_SHORT :
if (dim == 2) {
typedef itk::Image<unsigned short, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<unsigned short, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_INT :
if (dim == 2) {
typedef itk::Image<int, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<int, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_UNSIGNED_INT :
if (dim == 2) {
typedef itk::Image<unsigned int, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<unsigned int, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_LONG :
if (dim == 2) {
typedef itk::Image<long, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<long, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_UNSIGNED_LONG :
if (dim == 2) {
typedef itk::Image<unsigned long, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<unsigned long, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_FLOAT :
if (dim == 2) {
typedef itk::Image<float, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<float, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
case VTK_DOUBLE :
if (dim == 2) {
typedef itk::Image<double, 2> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
else {// if dim == 3
typedef itk::Image<double, 3> ImageType;
succeed = NiftiImageComponentExtension::saveImage<ImageType>(img);
}
break;
default :
break;
}
return succeed;
}
// --------------- saveImage -------------------
template<class ImageType> bool NiftiImageComponentExtension::saveImage(ImageComponent * img) {
typedef itk::VTKImageToImageFilter<ImageType> VtkToItkFilterType;
typedef itk::ImageFileWriter< ImageType > WriterType;
QString filename = img->getFileName();
if (filename.isEmpty())
return false;
vtkSmartPointer<vtkImageData> vtkImage = img->getImageData();
typename VtkToItkFilterType::Pointer vtkToItkFilter = VtkToItkFilterType::New();
typename WriterType::Pointer writer = WriterType::New();
vtkToItkFilter->SetInput(vtkImage);
writer->SetFileName( filename.toUtf8() );
writer->SetInput( vtkToItkFilter->GetOutput() );
try {
writer->Update();
}
catch (itk::ExceptionObject & err ) {
QMessageBox::warning(NULL,"Saving Error", "Problem while saving to file:<br/>" + filename +"<br/>ITK Exception:<br/>"+ err.what());
return false;
}
return true;
}
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2013 UJF-Grenoble 1, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#ifndef NIFTI_IMAGES_COMPONENT_H
#define NIFTI_IMAGES_COMPONENT_H
#include <QObject>
#include <ImageComponentExtension.h>
/** This NiftiImages ComponentExtension allows you to manipulate nifti format.
*/
class NiftiImageComponentExtension : public camitk::ImageComponentExtension {
Q_OBJECT
Q_INTERFACES(camitk::ComponentExtension);
public:
/// the constructor (do nothing really)
NiftiImageComponentExtension() : ImageComponentExtension() {};
/// get the plugin name
virtual QString getName() const;
/// get the plugin description (can be html)
virtual QString getDescription() const;
/// get the list of managed extensions (each file with an extension in the list can be loaded by this Extension
virtual QStringList getFileExtensions() const;
/// get a new instance from data stored in a file (this is the most important method to redefine in your subclass)
virtual camitk::Component * open(const QString &) throw(camitk::AbortException);
/** save a given Component (does not have to be top-level) into one of the currently managed format.
*
* @return false if the operation was not performed properly or not performed at all.
*/
virtual bool save(camitk::Component * component) const;
protected:
/// the destructor
virtual ~NiftiImageComponentExtension() {};
template <class ImageType> static bool saveImage(camitk::ImageComponent * img);
};
#endif // NIFTI_IMAGES_COMPONENT_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment