from vtk to OpenCV

619 views
Skip to first unread message

Xianjie Li

unread,
Apr 29, 2015, 2:44:30 AM4/29/15
to vmtk-...@googlegroups.com
Now I want convert vtkImageData to OpenCV Mat/IplImage.but the image that I processed is not expected.
I think it should be the data type,I try every type,but it doesn't work.
Can somebody help me?
IMG-0001-00001.jpg
opencv.bmp

Xianjie Li

unread,
Apr 29, 2015, 2:49:51 AM4/29/15
to vmtk-...@googlegroups.com
code is here:        
        vtkDICOMImageReader*reader = vtkDICOMImageReader::New();
reader->SetFileName("D:\\DicomData\\SE6\\IM24");
reader->Update();
vtkImageData *imageData_1 = vtkImageData::New();
imageData_1->DeepCopy(reader->GetOutput());

vtkImageCast*caster = vtkImageCast::New();
caster->SetInput(imageData_1);
caster->SetOutputScalarTypeToUnsignedChar();
caster->Update();

vtkExtractVOI*extractVOI = vtkExtractVOI::New();
extractVOI->SetInput(caster->GetOutput());

int dims[3];
imageData_1->GetDimensions(dims);
extractVOI->SetVOI(0, dims[0], 0, dims[1], 0, 0); // Set it to z=75
extractVOI->GetOutput()->SetScalarTypeToDouble();
cv::Mat image(dims[0], dims[1], CV_8UC1);
extractVOI->Update();
for (int i=0; i<dims[0]; ++i) 
{
for (int j=0; j<dims[1]; ++j) 
{
image.at<unsigned char>(cv::Point(j,i)) = *static_cast<unsigned char*>(extractVOI->GetOutput()->GetScalarPointer(i,j,0));
}
}

在 2015年4月29日星期三 UTC+8下午2:44:30,Xianjie Li写道:

Luca Antiga

unread,
May 1, 2015, 9:42:53 AM5/1/15
to Xianjie Li, vmtk-...@googlegroups.com
Hello Xianjie,
the problem here is that your original pixel type is not 8-bit (very likely 16-bit),
so its range will be well outside 0-255.
vtkImageCast is only changing the numeric type of the data array, but it won’t 
rescale your original values to the 0-255 range, you have to do it before casting.
So, what happens is that your values wrap around (i.e. 256->0, 257->1, ...), resulting
in the funny image you see in OpenCV).

If you want to stay in vtk-land, you can use vtkImageShiftScale instead of vtkImageCast


// first define window (the width of the intensity range you want to map to 0-255) and level (the center of the intensity range you want to map to 0-255)
double window = 500.0;
double level = 50.0;

vtkImageShiftScale* shiftScale = vtkImageShiftScale::New();
shiftScale->SetInput(imageData_1);
shiftScale->SetShift(-(level - 0.5*window));
shiftScale->SetScale(255.0/window);
shiftScale->SetOutputScalarTypeToUnsignedChar();
shiftScale->ClampOverflowOn();
shiftScale->Update();

vtkExtractVOI*extractVOI = vtkExtractVOI::New();
extractVOI->SetInput(shiftScale->GetOutput());

… 

Hope this helps

Luca
--
You received this message because you are subscribed to the Google Groups "vmtk-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vmtk-users+...@googlegroups.com.
To post to this group, send email to vmtk-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
-- 
Luca Antiga, PhD
Co-founder and Principal Scientist, OROBIX Srl
via Gabriele Camozzi 144, 24121 Bergamo, Italy

twitter: @lantiga
mobile: +39.347.43.16.596
office: +39 035.027.37.86

"This message originates from OROBIX Srl and its contents and attachments are privileged and confidential and are intended only for the individual(s) or entity(ies) named above. This message should not be forwarded, distributed or disclosed. Any unauthorized use, dissemination and duplication is strictly prohibited and may be unlawful. All personal messages express views solely of the sender, which are not to be attributed to OROBIX Srl, and may not be copied or distributed without this disclaimer. If you are not the intended recipient or received this message in error, please delete this message and notify the sender by reply e-mail. Opinions, conclusions and other information in this message that do not relate to the official business of OROBIX Srl shall be understood as neither given nor endorsed by it."

Xianjie Li

unread,
May 3, 2015, 9:43:14 PM5/3/15
to vmtk-...@googlegroups.com, lxj...@gmail.com
yeah,it works,Thank you Luca!

在 2015年5月1日星期五 UTC+8下午9:42:53,Luca Antiga写道:
Reply all
Reply to author
Forward
0 new messages