将Cimg转换为ITK [英] Convert Cimg to ITK

查看:511
本文介绍了将Cimg转换为ITK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Cimg图像转换为itk图像,以将其用于注册算法。 Cimg是一个RGB图像,我想将其转换为RGB itk图像。她是我的代码:

  void Cimg_To_ITK(CImg< uchar> img)
{
$ b b 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)



有什么错误

解决方案

code>是一个 RGBPixelType ,它不能转换为void指针和 memcpy()可以不处理它。 memcpy()需要指向值的指针,例如 img.data() it 。根据 CImg 的文档:



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



如何从值缓冲区导入图像的示例由ITK 此处。我猜这是你的起点。



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

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

这是一个如何将unsigned char的缓冲区导入ITK RGB图像的示例,从你的代码。随意编辑此答案并添加处理CImg的函数。

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

使用命名空间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);
//不需要删除localBuffer:itk会关心,因为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



<$ c $ p> cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ItkTest)

find_package (ITK需求)
include($ {ITK_USE_FILE})

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

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


I'm trying to convert a Cimg image to itk image to use it for registration algorithm. The Cimg is a RGB image and i want to convert it to RGB itk image. Her is my code :

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

I get this error at compilation :

error: cannot convert 'RGBPixelType {aka itk::RGBPixel}' to 'void*' for argument '1' to 'void* memcpy(void*, const void*, size_t)

What is wrong??

解决方案

*it is a RGBPixelType, it can't be converted to a void pointer and memcpy() can't handle it. memcpy() needs pointers to values, such as img.data() or it. According to the documentation of CImg :

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

An example of how to import an image from a buffer of values is provided by ITK here . My guess is that it is your starting point.

The other issue that you will soon face is the size of the image : RBG is 3 bytes per pixel, so it should be

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

Here is an example of how to import a buffer of unsigned char as an ITK RGB Image, starting from your code. Feel free to edit this answer and add the function to handle a 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;
}

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

After installing ITK and setting the environment variable ITK_DIR, type cmake . and make to build this example.

这篇关于将Cimg转换为ITK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆