简体   繁体   English

如何使用ITK将调色板图像读取为标量图像?

[英]How to read a palette image as a scalar image with ITK?

When I'm reading an image, the itk::ImageIOBase , as implemented here , tels me that the image has a RGB pixel type. 当我读的图像时, itk::ImageIOBase ,为实现 ,TELS我,图像具有RGB像素类型。 The format of the image is TIFF, but can be png or gif as well. 图像格式为TIFF,但也可以为png或gif。

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

How to know, through ITK , whether the image is actually a palette image, ie scalar image along with a color palette, and read the image as a scalar image + palette ? 如何通过ITK知道图像是否实际上是调色板图像( 标量图像和调色板),并将图像读取为标量图像+调色板? I need to retrive the index, as stored in the file as well as the color palette used in the file. 我需要检索存储在文件中的索引以及文件中使用的调色板。

For now, my only solution is to use freeImagePlus to identify and read this type of image. 目前,我唯一的解决方案是使用freeImagePlus识别并读取这种类型的图像。 I haven't found any function in the class ImageIOBase that could relate to that. 我没有在类ImageIOBase中找到任何与此相关的功能。

Any help would be appreciated, I haven't found much information on this on the internet ! 任何帮助将不胜感激,我还没有在互联网上找到太多的信息!

Have you tried to read it as grayscale image? 您是否尝试将其读取为灰度图像? What result does this reader produce without explicitly setting an IO on it? 该阅读器在未显式设置IO的情况下会产生什么结果?

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

Unless you need the color pallette for something, this could be enough. 除非您需要彩色调色板,否则就足够了。

To answer my own question, the feature is now implemented in ITK, in the master branch, and offer the support of palette for png tif and bmp images 为了回答我自己的问题,该功能现在在ITK的master分支中实现,并为png tif和bmp图像提供调色板支持

Here a working example, for those who are interested : 这是一个对感兴趣的人的工作示例:

#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()); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM