簡體   English   中英

如何使用ITK將調色板圖像讀取為標量圖像?

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

當我讀的圖像時, itk::ImageIOBase ,為實現 ,TELS我,圖像具有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;

除非您需要彩色調色板,否則就足夠了。

為了回答我自己的問題,該功能現在在ITK的master分支中實現,並為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()); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM