【问题标题】:ITK Importing Image Data from a BufferITK 从缓冲区导入图像数据
【发布时间】:2014-10-27 09:11:31
【问题描述】:

我编写了一种方法来从缓冲区创建 Itk 图像(在我的例子中,它是 Cimg 图像类型)。这是算法:

void Cimg_To_ITK (CImg<uchar> img)
{

    const unsigned int Dimension = 2;
    typedef itk::RGBPixel< unsigned char > RGBPixelType;
    typedef itk::Image< RGBPixelType, Dimension > RGBImageType;
    typedef itk::ImportImageFilter< RGBPixelType, Dimension > ImportFilterType;
    ImportFilterType::Pointer importFilter = ImportFilterType::New();
    typedef itk::ImageFileWriter<  RGBImageType  > WriterType;
    WriterType::Pointer writer = WriterType::New();


    RGBImageType::SizeType imsize;
    imsize[0] = img.width();
    imsize[1] = img.height();

    ImportFilterType::IndexType start;
    start.Fill( 0 );
    ImportFilterType::RegionType region;
    region.SetIndex( start );
    region.SetSize( imsize );
    importFilter->SetRegion( region );

    const itk::SpacePrecisionType origin[ Dimension ] = { 0.0, 0.0 };
    importFilter->SetOrigin( origin );

    const itk::SpacePrecisionType spacing[ Dimension ] = { 1.0, 1.0 };
    importFilter->SetSpacing( spacing );

    const unsigned int numberOfPixels = imsize[0] * imsize[1];
    const bool importImageFilterWillOwnTheBuffer = true;

    RGBPixelType * localBuffer = new RGBPixelType[ numberOfPixels ];
    memcpy(localBuffer->GetDataPointer(), img.data(), numberOfPixels);
    importFilter->SetImportPointer( localBuffer, numberOfPixels,importImageFilterWillOwnTheBuffer );
    writer->SetInput( importFilter->GetOutput() );
    writer->SetFileName( "output.png" );
    writer->Update();
}

我没有我想要的输出:

输入:

输出:

【问题讨论】:

    标签: c++ buffer itk cimg


    【解决方案1】:

    CImg 将不同的 RGB 像素存储为单独的组件

    您必须准备一个 RGBPixel 缓冲区并遍历图像并保存到缓冲区:

     RGBPixelType *buffer=new RGBPixelType[img.width()*img.height()];
     cimg_for(img,x,y)
     {
              // Now allign three colors
              buffer[x+y*img.width()]= RGBPixelType({img(x,y,0,0),img(x,y,0,1),img(x,y,0,2)});
     }
     importImageFilterWillOwnTheBuffer=true; // To avoid leaks
    

    【讨论】:

      【解决方案2】:

      检查 Cimg 如何存储图像的像素以及它是否与 RGBPixelType 不同可能也是一个好主意。我怀疑 RGBPixelType 数组的像素存储在 rgbrgbrgbrgb 中,而另一个可能以某种 rrrrggggbbbb 样式格式存储它们。或者,正如已经暗示的那样,如果输入是具有单通道的灰度图像,则必须复制每个 rgb 值的值(或者如果两者都是 rgb,则必须从所有三个通道复制数据)...

      【讨论】:

        【解决方案3】:

        这可能是因为您只是每个像素复制一个字节。每个 RGBPixelType 的大小可能是几个字节:

        memcpy(localBuffer->GetDataPointer(), img.data(), numberOfPixels * sizeof(RGBPixelType));
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-08
        • 2020-06-12
        • 2019-02-08
        相关资源
        最近更新 更多