0

自 1 或 2 周前以来,我一直在尝试解决这个非常奇怪的问题。

我正在尝试使用 ITK 读取 DICOM 图像,然后进行一些处理。

问题是,我想独立于像素类型读取图像。为此,我必须找出像素类型并在运行时实例化 itkImage。

因此,当我读取图像文件时,我必须将它从 itkImage 转换为 itkImageBase(在 itkImage.h 中它被声明为类 itkImage:public itkImageBase)。

不幸的是,当我在 if 块内将它从一种类型转换为另一种类型后,我无法在 if 块之外的任何地方访问存储在此变量上的值。这是代码:

*对不起葡萄牙语中的变量和方法名称。这里有一点帮助:

leitor是读者的意思

TipoPixel 表示像素类型

abreImagem(templated method) 表示 openImage

iniciaAbrirImagem 表示initialOpenImage

//here is io.h
#ifndef IO_H
#define IO_H

#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkGDCMImageIO.h"
#include "itkMetaDataDictionary.h"
#include "itkImageBase.h"
#include "iobase.h"

typedef itk::GDCMImageIO GDCM;
typedef itk::Image<float, 3> TipoImagemIO;
typedef itk::MetaDataDictionary TipoDicionario;
typedef itk::MetaDataObject<std::string> MetaDataString;
typedef itk::ImageBase<3> ImageBase; //Typedef for the imagebase

class IO : public IOBase
{
private:

    ImageBase* imagemB; //This is the variable giving me some trouble
    ImageBase* imagemB2;

    GDCM::Pointer gdcm;

    std::string tipoPixel;
public:
    IO();
    template<typename TLeitor>void abreImagem(std::string s, TLeitor leitor);
    void abreDiretorio(std::string s);
    void iniciaGravarImagem(std::string s, ImageBase* imgNova);
    ImageBase* GetOutput();
    void extraiTags();
    void iniciaAbrirImagem(std::string s);
    template<typename TGravador, typename TImagem>void gravaImagem(TGravador gravador, TImagem img, std::string s);
};
#endif  


//here is io.cpp
#include "io.h"
#include "vtkKWImage.h"
#include "vtkKWImageIO.h"
#include "imagem.h"
#include <typeinfo>

IO::IO()
{
imagemB = 0;
imagemB2 = 0;
gdcm = GDCM::New();
}

template<typename TLeitor>
void IO::abreImagem(std::string s, TLeitor leitor)
{
leitor->SetImageIO(gdcm);
leitor->SetFileName(s);

try
{
    leitor->Update();
}
catch(itk::ExceptionObject &ex)
{
    std::cerr<<ex<<std::endl;
    exit(1);
}

try
{
    imagemB = static_cast<ImageBase*>(leitor->GetOutput());
}
catch(std::bad_cast &ex)
{
    std::cout<<"EXCECAO";
    exit(1);
}
std::cout<<imagemB->GetNameOfClass(); //This one is ok
}

void IO::iniciaAbrirImagem(std::string s)
{
extraiTipoDimensoes(s); 
tipoPixel = GetTipo();  

if(tipoPixel.compare("short") == 0)
{
    typedef itk::Image<short, 3> itkImg;
    typedef itk::ImageSeriesReader<itkImg> TipoLeitor;
    TipoLeitor::Pointer leitor = TipoLeitor::New();

    abreImagem<TipoLeitor::Pointer>(s, leitor);

std::cerr<<"Trying to call a method inside IF: "<<imagemB->GetNameOfClass()<<std::endl;  //This one is ok too
}
//...doing the same things for unsigned short, int, float, double, etc...
std::cerr<<"trying to call a method outside IF: ";  
std::cerr<<imagemB->GetNameOfClass()<<std::endl; //Here i get this weird segmentation fault
}

ImageBase* IO::GetOutput()
{
std::cerr<<"JUST ENTERED GETOUTPUT()";  
std::cout<<imagemB->GetNameOfClass()<<std::endl; //If i remove the last method call of this variable, i get a segfault here
std::cerr<<std::endl<<"HOORAY, ITS GETTING OUT OF GETOUPUT"<<std::endl;
return imagemB;
}

正如您所看到的(或者至少我希望代码足够清晰,以便您可以看到它),我声明了一个指向 itk::ImageBase<3> 的指针并在类构造函数上使用值 0 启动它(已经尝试过不初始化,初始化使用null,没有任何效果......)。

之后,我调用其他类的一些方法来找出输入图像的像素类型。然后,我进入 if 块,在其中实例化一个 itk::ImageFileReader 和一个与输入图像具有相同像素类型的 itkImage。

然后,我将 TipoLeitor 类型的变量(或 ReaderType,如你所愿)发送到模板函数,在那里我实际读取 try/catch 块内的图像文件。然后,在下一次尝试中,我将阅读器的输出(一个 itkImage)转换为 itkImageBase 并将其放入 imagemB 变量中。

如果我尝试调用该类的一些随机方法,例如模板化方法中的 GetNameOfClass,那没关系,一切都按预期进行。如果我在调用模板化方法的 if 块中做同样的事情,那也没关系。但是,如果我在 if 块之后尝试在函数内部做同样的事情,它就不起作用。如果我尝试在 GetOutput() 方法上执行此操作,也会发生同样的情况。

就像变量 imagemB 在 if 块中初始化一样,当它退出块时,它被设置为未初始化(这个词甚至存在吗)。

没有详细的错误消息,只是“分段错误”。

感谢您的帮助,并对糟糕的英语和长文本感到抱歉。

4

2 回答 2

1

当您说“独立于像素类型读取图像”时,如果您谈论的是磁盘上图像的像素类型,则无需经过: ImageSeriesReader 将在内部从磁盘上的像素类型进行转换到您作为 ImageSeriesReader 模板放置的图像的像素类型。请注意,ImageSeriesReader 的模板是输出中图像的类型,而不是输入

如果您真的想在运行时控制输出类型,您可以使用模板函数:

template<typename OutputPixelType> void my_process() {
}

并将其用于:

switch (type)
  {
  case 1:
    my_process<unsigned char>(parseResult);
    break;
  case 2:
    my_process<short int>(parseResult);
    break;
  ...
  }

完整示例在这里 http://hg.orfeo-toolbox.org/OTB-Applications/file/1c193d45e483/Utils/otbConvert.cxx

于 2011-08-20T18:07:10.330 回答
0

ImageBase 类是抽象的,没有适合保存图像的完整实现。您必须实例化一个(或多个)模板类型以保存实际图像。

于 2011-09-24T12:27:12.333 回答