将vtkImageData复制到cv :: Mat [英] Copy vtkImageData to 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..
预先感谢, 萨尔塔克
预期的输出
我现在得到的输出,
我意识到图像大小不一样.这是因为我刚刚从正在使用的查看器中发布了数据快照..在保管箱链接中有一个示例,给出了
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..
标记:vtk opencv网桥,vtkopencv,vtk opencv集成
TAGS: vtk opencv bridge, vtkopencv, vtk opencv integrate
推荐答案
好的,因此直接在vtkImageData
类上使用的GetPointerToData()
函数或GetScalarPointer()
函数不适合分配(至少不是这样)我可以弄清楚)..因此,我设计了另一种方法,
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..
我扩展了代码,以便与 vtkImageData 和 cv :: 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屋!