According to the github issue, a workaround is to use 255 - (mat == mat)
instead of mat != mat
(this should work in general, but is a bug currently).
rayryeng's double-patch approach is more robust, but brings a significant performance drawback.
Here a minimal working example:
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
cv::Mat1b is_nan(cv::Mat mat)
{
return 255 - (mat == mat);
}
int main()
{
cv::Mat mat(5, 7, CV_32FC1);
cv::randu(mat, cv::Scalar(-1), cv::Scalar(1));
mat.at<float>(0,0) = std::nan("");
mat.at<float>(4,5) = std::nan("");
std::cout << mat << std::endl;
std::cout << is_nan(mat) << std::endl;
return 0;
}
output
[nan, -0.60148162, -0.19788112, 0.62877017, -0.12573405, -0.5024206, 0.54621011;
0.52418745, -0.38441104, 0.40486339, -0.043105587, 0.58438003, -0.82831377, -0.8498795;
-0.67315322, -0.40044156, 0.81130785, 0.41937166, -0.70057499, 0.53087986, -0.75143719;
-0.9925428, 0.10302717, 0.99639863, -0.68201572, -0.6776439, -0.92362136, -0.14827734;
0.69225526, 0.77520895, -0.47057521, -0.4584339, 0.9053328, nan, 0.62043273]
[255, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 255, 0]