如何将调色板图像读为具有ITK标量图像

How to read a palette image as a scalar image with ITK?

本文关键字:图像 ITK 标量 调色板      更新时间:2023-10-16

当我读取图像时,在此处实现的 itk::ImageIOBase时,我认为该图像具有RGB像素类型。图像的格式是tiff,但也可以是png或gif。

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

如何知道,通过itk ,图像实际上是调色板图像,标量图像以及调色板,并将图像读为标量图像 调色板?我需要检测文件中存储的索引以及文件中使用的调色板。

目前,我唯一的解决方案是使用FreeImagePlus识别和读取此类图像。我在类ImageIOBase中没有找到任何可能与此相关的功能。

任何帮助都将不胜感激,我在互联网上没有找到太多信息!

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

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

除非您需要某物的颜色pallette,否则这可能就足够了。

要回答我自己的问题,该功能现在在itk,主分支中实现,并提供PNG TIF和BMP Images的Palette的支持

在这里为那些有兴趣的人提供一个工作示例:

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