Win10, CMake 3.18, VS 2017, opencv 4.4.0, ITK fe92a3aa8d6d304b2636f4ba90aa6b8b24f66974.
Step to reproduce:
Make clean VS project using CMake with Opencv and ITK.
After including #include <itkOpenCVImageBridge.h> received build errors:
````
1>E:\Lib_prebuild\ITK\build_x64\include\ITK-5.2\itkOpenCVImageBridge.h(154): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>E:\Lib_prebuild\ITK\build_x64\include\ITK-5.2\itkOpenCVImageBridge.h(154): error C2988: unrecognizable template declaration/definition
1>E:\Lib_prebuild\ITK\build_x64\include\ITK-5.2\itkOpenCVImageBridge.h(154): error C2143: syntax error: missing ',' before '&'
````
@PierreWargnier would you please take a look?
As far as I remember this bug did not appear in ITK 5.1.
I took a look at it. I think the problem comes from the function declaration. On line 154 of itkOpenCVImageBridge.h, "const Mat & in" should be "const cv::Mat & in". For some reason it compiled on Linux with the test program. Could you please tell me if this fixes the issue? If so, I can submit a new pull request to fix the bug but it would save me some time if I do not need to try it myself.
Could you please tell me if this fixes the issue?
Yes, this change fix this issue.
Also we need to change
# include "opencv2/core.hpp" in line 30 to
# include "opencv2/imgproc.hpp"
In order not to open a new issue, I will write here. I try to convert opencv image to float ITK image
````
constexpr unsigned int ImageDimension = 2;
using PixelType = float;
using FixedImageType = itk::Image
cv::Mat inputImage = cv::imread(ssFixedPath, CV_32F);
using BridgeType = itk::OpenCVImageBridge;
FixedImageType::ConstPointer fixedImage = BridgeType::CVMatToITKImage< FixedImageType >(inputImage);
```
, but receive exceptionitkGenericExceptionMacro("OpenCV to ITK conversion pixel type mismatch");in line 223 ofitkOpenCVImageBridge.h`.
Is this a bug in ITK or a bug in my code?
For your other question, I do not think that is a bug. You are actually trying to convert mismatching types, since CV_32F is not a valid argument for the imread method (see documentation here https://docs.opencv.org/4.4.0/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56), therefore, I do not think that this code produces a Mat of floats. You can check if the Mat you are trying to convert is actually of type float using
bool isFloat = inputImage.depth() == cv::CV_32F;
It is actually surprising that your call to imread does not cause a crash, since CV_32F evaluates to 5 and 5 is not a valid imread flag.
Please make sure you call imread with a valid flag to correctly load your input image and if you want it as float, use
cv::Mat floatImage;
inputImage.convertTo(floatImage, CV_32F);
Hi @Bleach665 ,
I have submited a PR to fix this compile error. It was merged in master. You can now update ITK to the current version and close this issue if it is satisfactory.
Best,
Pierre
Hi @PierreWargnier ,
Thanks! I can't test all build in the near future, but with the changes in this PR, everything worked well for me.