Bio-inspired features returns NaNs
Hello,
I am using bif function (bif.cpp) from the extra module 'face' of Opencv-3.0.1. When I run the function for a test image, the returned feature vector includes many NaN
values. Could you please help me understand why this is happening?
#include "opencv2/opencv.hpp"
#include "opencv2/face/bif.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv) {
cv::Mat fea;
cv::Ptr<cv::face::BIF> bif = cv::face::createBIF();
cv::Mat image(60, 60, CV_32F);
cv::theRNG().fill(image, cv::RNG::UNIFORM, 0, 1);
bif->compute(image, fea);
cout << "fea = " << endl << " " << fea << endl << endl;
return 0;
}
Thank you in advance.
EDIT:
Sorry for my delayed edit, but please let me ask something more. I have made the changes you suggested and for most of the images the NaN
value disappeared. However, the problem for some images is not fixed. I am giving an example below. Could you please provide some extra help on this?
EDIT Possible answer: The code of bif.cpp seems to implement correctly the algorithm presented in the corresponding paper, so the problem of negative values in sqrt
is not algorithmic. By debugging the code, I noticed that the negative values were very small, having order of magnitude 10^-17 and 10^-19 for the specific example image and values of order 10^-17 to 10^-21 for other images. According to this post, these very small negative numbers are smaller than the numeric limit for double values and can be casted to 0. Therefore, I suggest that the line 210 can be:
sd = sqrt((sd / area - mean mean) < 0 & abs((sd / area - mean mean)) < std::numeric_limits<double>::epsilon() ? 0 : (sd / area - mean * mean));
If (sd / area - mean mean)
is positive, nothing changes. If (sd / area - mean mean)
is negative and smaller than the numeric limit for doubles, it is set to 0. By changing line of code 210 as above, no NaN
values emerge.
hope, you don't mind my little edit, but you have to use opencv headers like above. (please adjust your local include path)
also, negative numbers would not naturally appear in an image, so better use [0..1] for the random. (it's not the cause for the NaN's , still)https://github.com/opencv/opencv_cont...