将 vtkImageData 复制到 cv::Mat [英] Copy vtkImageData to cv::Mat

查看:42
本文介绍了将 vtkImageData 复制到 cv::Mat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 vtkImageData* 类复制到 cv::Mat 结构 [我的目标是将 MHD 文件读入 OpenCV].. 该文件本质上是一个 3D 矩阵,因此我想获得一个包含所有卷的切片.到目前为止,我已经想出了这段代码,

I am trying to copy a vtkImageData* class to a cv::Mat structure [my goal is to read an MHD file into OpenCV].. The file is essentially a 3D Matrix so I want to get a vector containing all the slices of the volume. So far, I have come up with this code,

reader->SetFileName(INPUT_DATA_1.c_str());
reader->Update();
imageData_1 = reader->GetOutput();
extractVOI->SetInput(imageData_1);

int dims[3];
imageData_1->GetDimensions(dims);
extractVOI->SetVOI(0, dims[0], 0, dims[1], 75, 75); // Set it to z=75
extractVOI->GetOutput()->SetScalarTypeToSignedChar();
imageExport->SetInputConnection(extractVOI->GetOutputPort());
imageExport->Update();

cv::Mat cvMat_test(dims[0], dims[1], CV_8UC1, imageExport->GetPointerToData());

尽管这是有效的,但它并没有给我预期的输出(在下面突出显示).有关此事的任何帮助都会非常有帮助..

Although this is working, it is not giving me the expected output (which are highlighted below).. Any help regarding this matter would be very helpful..

提前致谢,萨萨克

预期输出,

我现在得到的输出,

我意识到图像的大小不一样.这是因为我刚刚发布了我正在使用的查看器的数据快照.. 在 Dropbox 链接中有一个示例 这里.希望这能进一步澄清事情..

I realize that the images aren't the same size. This is because I have just posted a snapshot of the data from the viewer that I am using.. There is an example in the dropbox link given HERE. Hope this clarifies things a bit more..

TAGS: vtk opencv bridge, vtkopencv, vtk opencv集成

TAGS: vtk opencv bridge, vtkopencv, vtk opencv integrate

推荐答案

好的,那么 GetPointerToData() 函数或 GetScalarPointer() 函数直接在 vtkImageData 类不适合分配(至少不是我能弄清楚的方式).所以,我设计了另一种方法,

Okay, so the GetPointerToData() function or GetScalarPointer() function used directly on the vtkImageData class are not appropriate for allocation (at least not in a way I could figure out).. So, I devised another method,

reader->SetFileName(INPUT_DATA_1.c_str());
reader->Update();
imageData_1 = reader->GetOutput();
extractVOI->SetInput(imageData_1);

int dims[3];
imageData_1->GetDimensions(dims);
extractVOI->SetVOI(0, dims[0], 0, dims[1], 75, 75); // Set it to z=75
extractVOI->GetOutput()->SetScalarTypeToSignedChar();
cv::Mat cvMat_test(dims[0], dims[1], CV_8UC1);

for (int i=0; i<dims[0]; ++i) {
    for (int j=0; j<dims[1]; ++j) {
        cvMat_test.at<unsigned char>(cv::Point(j,i)) = *static_cast<unsigned char*>(extractVOI->GetOutput()->GetScalarPointer(i,j,vol_dim));
    }
}

我猜这个方法可以很容易地扩展到包括整个卷..

I am guessing this method can be extended to include the entire volume pretty easily..

我扩展了我的代码,以便在 vtkImageDatacv::Mat 和/或 cv::gpu::Mat 之间进行转换..我已将代码上传到此处vtkOpenCVBridge.

I extended my code to do conversions to and from vtkImageData and cv::Mat and/or cv::gpu::Mat.. I have uploaded to the code here as vtkOpenCVBridge.

干杯!

这篇关于将 vtkImageData 复制到 cv::Mat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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