Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将Cimg转换为ITK_C++_Itk_Cimg - Fatal编程技术网

C++ 将Cimg转换为ITK

C++ 将Cimg转换为ITK,c++,itk,cimg,C++,Itk,Cimg,我正在尝试将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< RGBPixelTy

我正在尝试将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" );
}
void Cimg_To_ITK(Cimg img)
{
常量无符号整数维=2;
typedef itk::RGBPixelRGBPixelType;
typedef itk::ImageRGBImageType;
typedef itk::ImportImageFilterImportFilterType;
ImportFilterType::指针importFilter=ImportFilterType::New();
typedef itk::ImageFileWriterWriterType;
WriterType::指针编写器=WriterType::New();
RGBImageType::SizeType imsize;
imsize[0]=img.width();
imsize[1]=img.height();
ImportFilterType::IndexType开始;
开始。填充(0);
ImportFilterType::RegionType区域;
region.SetIndex(开始);
region.SetSize(imsize);
导入过滤器->设置区域(区域);
ConstITK::SpacePrecisionType原点[Dimension]={0.0,0.0};
导入过滤器->设置原点(原点);
ConstITK::SpacePrecisionType间距[Dimension]={1.0,1.0};
导入过滤器->设置间距(间距);
常量unsigned int numberOfPixels=imsize[0]*imsize[1];
const bool importImageFilterWillOwnTheBuffer=true;
RGBPixelType*localBuffer=新的RGBPixelType[numberOfPixels];
RGBPixelType*it=localBuffer;
memcpy(*it,img.data(),numberOfPixels);
importFilter->SetImportPointer(本地缓冲区、像素数、ImportImageFilter低于缓冲区);
writer->SetFileName(“output.png”);
}
我在编译时遇到以下错误:

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


有什么问题吗?

*它是一个
RGBPixelType
,无法转换为空指针,
memcpy()
无法处理它
memcpy()
需要指向值的指针,例如
img.data()
it
。根据以下文件:

T*data()
返回指向第一个像素值的指针

ITK提供了一个如何从值缓冲区导入图像的示例。我猜这是你的出发点

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

memcpy(it, img.data(), numberOfPixels*3);
下面是一个示例,说明如何从代码开始,将未签名字符的缓冲区导入为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;
}
安装ITK并设置环境变量
ITK_DIR
后,键入
cmake.
make
以构建此示例

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})