gpt4 book ai didi

c++ - 将 Cimg 转换为 ITK

转载 作者:行者123 更新时间:2023-11-30 03:59:58 30 4
gpt4 key购买 nike

我正在尝试将 Cimg 图像转换为 itk 图像以将其用于配准算法。 Cimg 是一个 RGB 图像,我想将它转换为 RGB itk 图像。她是我的代码:

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 ];
RGBPixelType * it = localBuffer;
memcpy(*it, img.data(), numberOfPixels);

importFilter->SetImportPointer( localBuffer, numberOfPixels,importImageFilterWillOwnTheBuffer );

writer->SetFileName( "output.png" );
}

我在编译时遇到这个错误:

错误:无法将参数“1”的“RGBPixelType {aka itk::RGBPixel}”转换为“void*”到“void* memcpy(void*, const void*, size_t)”

怎么了??

最佳答案

*it 是一个RGBPixelType,它不能被转换成一个空指针并且memcpy() 不能处理它。 memcpy() 需要指向值的指针,例如 img.data()it。根据 CImg 的文档:

T* data () Return a pointer to the first pixel value.

ITK here 提供了如何从值缓冲区导入图像的示例。 .我猜这是您的起点。

您很快就会面临的另一个问题是图像的大小:RBG 是每个像素 3 个字节,所以它应该是

memcpy(it, img.data(), numberOfPixels*3);

这是一个示例,说明如何从您的代码开始将 unsigned char 缓冲区导入为 ITK RGB 图像。随意编辑此答案并添加处理 CImg 的功能!

#include <iostream>
#include <itkImage.h>

using namespace itk;
using namespace std;

#include <itkImportImageFilter.h>
#include <itkImageFileWriter.h>

void Cimg_To_ITK (unsigned char* data)
{

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();
imsize[0] = 100;
imsize[1] = 200;

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 ];
RGBPixelType * it = localBuffer;
memcpy(it, data, numberOfPixels*3);
// no need to delete localBuffer : itk will care since importImageFilterWillOwnTheBuffer=true
importFilter->SetImportPointer( localBuffer, numberOfPixels,importImageFilterWillOwnTheBuffer );

writer->SetFileName( "output.png" );
writer->SetInput(importFilter->GetOutput() );
writer->Update();

}

int main()
{
unsigned char* data=new unsigned char[100*200*3];
for(int i=0;i<200;i++){
for(int j=0;j<100;j++){
data[(i*100+j)*3]=i;
data[(i*100+j)*3+1]=0;
data[(i*100+j)*3+2]=j;
}

}

Cimg_To_ITK (data);

delete[] data;
cout<<"running fine"<<endl;
return 0;
}

文件CMakeLists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ItkTest)

find_package(ITK REQUIRED)
include(${ITK_USE_FILE})

add_executable(MyTest main.cpp)
target_link_libraries(MyTest ${ITK_LIBRARIES})

安装 ITK 并设置环境变量 ITK_DIR 后,键入 cmake .make 以构建此示例。

关于c++ - 将 Cimg 转换为 ITK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576157/

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