有没有办法将Eigen :: Matrix转换回itk :: image? [英] Is there any way to convert an Eigen::Matrix back to itk::image?

查看:219
本文介绍了有没有办法将Eigen :: Matrix转换回itk :: image?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Eigen库将几个itk :: image图像转换为矩阵,并对它们进行密集的线性代数计算。最后,我将输出作为矩阵,但我需要它以itk :: image形式。有没有办法做到这一点?

I used Eigen library to convert several itk::image images into matrices, and do some dense linear algebra computations on them. Finally, I have the output as a matrix, but I need it in itk::image form. Is there any way to do this?

    const unsigned int numberOfPixels = importSize[0] * importSize[1];
    float* array1 = inverseU.data();
    float* localBuffer = new float[numberOfPixels];
    std::memcpy(localBuffer, array1, numberOfPixels);
    const bool importImageFilterWillOwnTheBuffer = true;
    importFilter->SetImportPointer(localBuffer,numberOfPixels,importImageFilterWillOwnTheBuffer);
    importFilter->Update();

inverseU是特征库矩阵(float),importSize是此矩阵的大小。当我提供importFilter-> GetOutput()并将结果写入文件时,我得到的图像就像这个,这是不正确的。

inverseU is the Eigen library matrix (float), importSize is the size of this matrix. When I give importFilter->GetOutput(), and write the result to file, the image I get is like this, which is not correct.

这是矩阵inverseU。
https://drive.google.com/file/d / 0B3L9EtRhN11QME16SGtfSDJzSWs / view?usp = sharing 。它应该以图像形式给出视网膜眼底图像,我在进行去模糊后得到了矩阵。

This is the matrix inverseU. https://drive.google.com/file/d/0B3L9EtRhN11QME16SGtfSDJzSWs/view?usp=sharing . It is supposed to give a retinal fundus image in image form, I got the matrix after doing deblurring.

推荐答案

看看itk的 ImportImageFilter 。特别是,它可以用于从C风格的数组( itk :: Image Wiki / ITK / Examples / IO / ImportImageFilterrel =nofollow noreferrer> example )。

Take a look at the ImportImageFilter of itk. In particular, it may be used to build an itk::Image starting from a C-style array (example).

最近有人问过如何将CImg图像转换为ITK图像。我的答案可能是一个起点...

Someone recently asked how to convert a CImg image to ITK image. My answer might be a starting point...

从矩阵中获取数组的方法 A 来自可以在这里找到Eigen:

A way to get the array out of a matrix A from Eigen may be found here :

double* array=A.data();  

编辑:这是一段代码,用于将浮动矩阵转换为使用ITK保存的png图像。首先,矩阵转换为浮动的itk图像。然后,使用 RescaleIntensityImageFilter 将此图像重新调整为强制转换为无符号字符的图像,如这里。最后,图像以png格式保存。

EDIT : here is a piece of code to turn a matrix of float into a png image saved with ITK. First, the matrix is converted to an itk Image of float. Then, this image is rescaled an cast to a image of unsigned char, using the RescaleIntensityImageFilter as explained here. Finally, the image is saved in png format.

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

using namespace itk;
using namespace std;

#include <Eigen/Dense>
using Eigen::MatrixXf;

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

void eigen_To_ITK (MatrixXf mat)
{

    const unsigned int Dimension = 2;

    typedef itk::Image<unsigned char, Dimension>  UCharImageType;
    typedef itk::Image< float, Dimension > FloatImageType;
    typedef itk::ImportImageFilter< float, Dimension > ImportFilterType;
    ImportFilterType::Pointer importFilter = ImportFilterType::New();

    typedef itk::RescaleIntensityImageFilter< FloatImageType, UCharImageType > RescaleFilterType;
    RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();

    typedef itk::ImageFileWriter<  UCharImageType  > WriterType;
    WriterType::Pointer writer = WriterType::New();


    FloatImageType::SizeType imsize;
    imsize[0] = mat.rows();
    imsize[1] = mat.cols();

    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;

    float * localBuffer = new float[ numberOfPixels ];
    float * it = localBuffer;

    memcpy(it, mat.data(), numberOfPixels*sizeof(float));
    importFilter->SetImportPointer( localBuffer, numberOfPixels,importImageFilterWillOwnTheBuffer );

    rescaleFilter ->SetInput(importFilter->GetOutput());
    rescaleFilter->SetOutputMinimum(0);
    rescaleFilter->SetOutputMaximum(255);


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

}

int main()
{

    const int rows = 42;
    const int cols = 90;
    MatrixXf mat1(rows, cols);
    mat1.topLeftCorner(rows/2, cols/2) = MatrixXf::Zero(rows/2, cols/2);
    mat1.topRightCorner(rows/2, cols/2) = MatrixXf::Identity(rows/2, cols/2);
    mat1.bottomLeftCorner(rows/2, cols/2) = -MatrixXf::Identity(rows/2, cols/2);
    mat1.bottomRightCorner(rows/2, cols/2) = MatrixXf::Zero(rows/2, cols/2);

    mat1+=0.1*MatrixXf::Random(rows,cols);

    eigen_To_ITK (mat1);

    cout<<"running fine"<<endl;
    return 0;
}

程序是使用CMake构建的。这是CMakeLists.txt:

The program is build using CMake. Here is the CMakeLists.txt :

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(ItkTest)

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

# to include eigen. This path may need to be changed
include_directories(/usr/local/include/eigen3)

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

这篇关于有没有办法将Eigen :: Matrix转换回itk :: image?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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