gpt4 book ai didi

c++ - ITK 从缓冲区导入图像数据

转载 作者:行者123 更新时间:2023-11-28 06:37:16 24 4
gpt4 key购买 nike

我已经编写了一种从缓冲区创建 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();
}

我没有我想要的输出:

输入: enter image description here

输出:

enter image description here

最佳答案

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

关于c++ - ITK 从缓冲区导入图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26584208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com