OpenCV x ITK image conversion using OpenCVImageBridge

Hi everyone,

I would like to convert simpleITK images to OpenCV images and vice versa.

The conversion from OpenCV to ITK is working fine like the example below:

cv::Mat movingImage = cv::imread(moving_image_path);
cv::Mat fixedImage = cv::imread(fixed_image_path);

cvMovingImage.convertTo(cvMovingImage, CV_32FC1, 1.0/255.0);

cvFixedImage.convertTo(cvFixedImage, CV_32FC1, 1.0/255.0);

typedef itk::Image<float, 2> ImageType;

ImageType::Pointer itkMovingImage = ImageType::New();
ImageType::Pointer itkFixedImage = ImageType::New();

itkMovingImage = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(cvMovingImage);
itkFixedImage = itk::OpenCVImageBridge::CVMatToITKImage<ImageType>(cvFixedImage);

m_fixedImage = sitk::Image(itkFixedImage);
m_movingImage = sitk::Image(itkMovingImage);

Then, I would like to convert the output from the sitk::CheckerBoard function to OpenCV image and at this moment I am getting some errors.

auto image = sitk::CheckerBoard(m_fixedImage, m_movingImage, {10,10,4});
sitk::Show(image, "SimpleITK Fusion", false);

// Convert ITK image image to opencv image
cv::Mat resultImage = itk::OpenCVImageBridge::ITKImageToCVMat(&image);
cv::imshow("ITK conversion", resultImage);
cv::waitKey(0);

This is the error that I am getting:

ITK/Modules/Video/BridgeOpenCV/include/itkOpenCVImageBridge.hxx:278:9: error: no type named ‘PixelType’ in ‘using ImageType = class itk::simple::Image’ {aka ‘class itk::simple::Image’}
    using InputPixelType = typename ImageType::PixelType;
             ^~~~~~~~~~~~~~
ITK/Modules/Video/BridgeOpenCV/include/itkOpenCVImageBridge.hxx:279:9: error: no type named ‘PixelType’ in ‘class itk::simple::Image’
   using ValueType = typename itk::NumericTraits<InputPixelType>::ValueType;
                   ^~~

What is wrong with this portion of the code?

Thanks :slight_smile:

itk::Image and itk::simple::Image are two distinct classes, and the latter does not declare a public type called PixelType.

There is a way to direct convert itk::Image to its::simple::Image? If there is no way I will probably implement everything with ITK.

Hello,

Here is an example that using ITK and SimpleITK:
https://simpleitk.readthedocs.io/en/master/link_ITKIntegration_docs.html

The sitk::Image::GetITKImage return value can be cast to the proper itk::Image type.

Please let us know if you need more specific help.

Thanks @blowekamp! In the end, I dropped simpleITK and now, I am using only ITK with openCV.