Copy vtkImageData to cv :: Mat

I am trying to copy the vtkImageData * class to the cv :: Mat structure [my goal is to read the MHD file in OpenCV]. The file is essentially a 3D matrix, so I want to get a vector containing all the pieces of 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 works, it does not give me the expected result (which is highlighted below). Any help on this would be very helpful.

Thanks in advance, Sarthak

Expected Result,

expected_output

The output I get right now

actual_output

EDIT: I understand that the images are not the same size. This is because I just posted a snapshot of the data from the viewer that I am using. The following link gives an example: HERE . Hope this clarifies the situation a bit more.

TAGS: vtk opencv bridge, vtkopencv, vtk opencv

+4
3

, GetPointerToData() GetScalarPointer(), vtkImageData, ( , , ). , ,

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

, , .

EDIT:

, vtkImageData​​strong > cv:: Mat / cv:: gpu:: Mat.. vtkOpenCVBridge.

.

+3

-, . , . , dims [0] dims [1] .

-, , vtk- unsigned char? , , ints .

-, , vtk- ( )?

VTK, - ...

0

, , , , . . VTK, , .

0

All Articles