2016-12-24 58 views
0

当我在阅读图像时,itk::ImageIOBase(如here)所示,表明图像具有RGB像素类型。图像的格式是TIFF,但也可以是png或gif。如何用ITK将调色板图像作为标量图像读取?

itk::ImageIOBase::Pointer imageIO = 
    itk::ImageIOFactory::CreateImageIO(
     fileName, itk::ImageIOFactory::ReadMode); 

如何知道,通过ITK,图像是否实际上是一个调色板图像,标图像与调色板一起,并读取图像作为标图像+调色板?我需要检索存储在文件中的索引以及文件中使用的调色板。

现在,我唯一的解决方案是使用freeImagePlus来识别和读取这种类型的图像。我还没有发现类ImageIOBase可能与此有关的任何功能。

任何帮助将不胜感激,我还没有在互联网上找到这方面的很多信息!

回答

0

要回答我的问题,该功能现在在ITK实现,在主分支,并提供了对PNG TIF和BMP图像

这里工作的例子,对于那些有兴趣谁调色板的支持:

#include "itkImage.h" 
#include <iostream> 
#include <string> 

#include "itkPNGImageIOFactory.h" 
#include "itkImageFileReader.h" 
#include "itkPNGImageIO.h" 

int main() 
{ 
    std::string filename("testImage_palette.png"); 

    auto io = itk::PNGImageIO::New(); 

    // tell the reader not to expand palette to RGB, if possible 
    io->SetExpandRGBPalette(false); 

    typedef unsigned short PixelType; 
    typedef itk::Image<PixelType, 2> imageType; 
    typedef itk::ImageFileReader<imageType> ReaderType; 
    ReaderType::Pointer reader = ReaderType::New(); 

    reader->SetFileName(filename); 
    reader->SetImageIO(io); 

    try { 
     reader->Update(); 
    } catch (itk::ExceptionObject &err) { 
     std::cerr << "ExceptionObject caught !" << std::endl; 
     std::cerr << err << std::endl; 
     return EXIT_FAILURE; 
    } 

    std::cout<< std::endl << "IsReadAsScalarPlusPalette:" <<io->GetIsReadAsScalarPlusPalette() << std::endl; 

    if (io->GetIsReadAsScalarPlusPalette()) { 
     auto palette(io->GetColorPalette()); 
     std::cout<< "palette (size="<< palette.size()<<"):"<< std::endl; 
     auto m(std::min(static_cast<size_t>(10),palette.size())); 
     for (size_t i=0; i<m;++i) { 
      std::cout << "["<<palette[i]<< "]"<< std::endl; 
     } 
     if (m< palette.size()) 
      std::cout<< "[...]"<< std::endl; 
    } 
    // if io->GetIsReadAsScalarPlusPalette() im will be the index of the palette image 
    auto im(reader->GetOutput()); 
} 
0

您是否尝试将其读取为灰度图像?读者在没有明确设置IO的情况下产生什么结果?

typedef itk::Image<unsigned char, 2> uc2Type; 
typedef itk::ImageFileReader<uc2Type> ReaderType; 

除非你需要调色板的东西,这可能就足够了。

+0

感谢您的帮助!获得每个像素使用的确切索引以及关联的颜色映射对我来说非常重要。不幸的是,在测试您的代码示例之后,根据其调色板中提供的RGB值将图像转换为灰色。因此,我无法找到索引... – beesleep

+0

ITK可能不是最好的库。你是否尝试过直接使用libpng或libtiff? –

+0

谢谢。我真的注意到了!但是我正在开发一个注册程序,因此无论如何我都需要将图像转换为ITK。目前,我正在使用freeImage来读取这些图像并将内存复制到itk。整合在ITK中的东西会更方便。 – beesleep