[ITK-users] Conversion from cv::mat to writable itk::image

Matt McCormick matt.mccormick at kitware.com
Fri Nov 25 11:28:32 EST 2016


Hi Gordian,

The runtime error is related to limited support for float pixel images
in DICOM and GDCM. Try using an itk::CastImageFilter or
itk::RescaleIntensityImageFilter to cast the written image pixel type
to something like unsigned short. Or, use an output image type that
supports floats well, like MetaImage.

Here are some more examples for working with OpenCV:

  https://itk.org/ITKExamples/src/Video/BridgeOpenCV/index.html

HTH,
Matt

On Fri, Nov 25, 2016 at 10:20 AM, Kabelitz, Gordian
<Gordian.Kabelitz at medma.uni-heidelberg.de> wrote:
> Hello,
>
> i started working on a program that uses ITK in combination with OpenCV.
> At the moment I encounter the problem with the image conversion from cv::Mat to itk::image.
> Using OpenCVImageBridge:: ITKImageToCVMat() can convert ITK images to cv::Mat and works pretty well. That part is similar to [1].
>
> A problem occurs, when I want to convert the images back to ITK images by using OpenCVImageBridge:: CVMatToITKImage ().
> Here is a code snippet (similar to [2]):
> [...]
> cv::Mat outimage = cvImage; // outimage is an image previously converted from itk::image to cv::Mat
> typedef itk::OpenCVImageBridge BridgeType;
> CVImageType::Pointer outputImage = BridgeType::CVMatToITKImage<CVImageType>(outimage);
>
> typedef itk::CastImageFilter< CVImageType, OutImage > CastFilterType;
> CastFilterType::Pointer caster = CastFilterType::New();
>  caster->SetInput(outputImage);
>
> typedef itk::ImageFileWriter<OutImage> WriterType;
> WriterType::Pointer outputWriter = WriterType::New();
> outputWriter->SetImageIO(gdcmIO);
> outputWriter->SetInput(caster->GetOutput());
> outputWriter->SetFileName(argv[2]);
> try  {
>           outputWriter->Update();
>   }   catch (itk::ExceptionObject err)  {
>           std::cout << "EXCEPTION CAUGHT!" << std::endl;
>           std::cout << err << std::endl;
>           return EXIT_FAILURE;
>   }
>
> It compiles and runs with only the following error:
> Description: itk::ERROR: GDCMImageIO(0000000003B22290): A Floating point buffer was passed but the stored pixel type was not specified. This is currently not supported.
>
> Is there an option to get a writable image? Did I make an obvious mistake?
> Is there a website to get more examples working for ITK with Opencv?
>
> Thanks in advance,
> Gordian
>
> [1]: https://github.com/InsightSoftwareConsortium/ITK-OpenCV-Bridge-Tutorial
> [2]: https://github.com/InsightSoftwareConsortium/ITK-OpenCV-Bridge-Tutorial/blob/master/Exercises/ITKOpenCVBridge/exercise2/BasicVideoFilteringITKOpenCVBridge.cxx
>
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/insight-users


More information about the Insight-users mailing list