Ask Your Question

Bio-inspired features returns NaNs

asked 2016-10-05 15:02:06 -0500

EvaPant gravatar image

updated 2016-10-17 06:26:56 -0500


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?

image description

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.

edit retag flag offensive close merge delete


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)

berak gravatar imageberak ( 2016-10-05 23:02:15 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-10-06 02:14:18 -0500

berak gravatar image

updated 2016-10-11 01:28:08 -0500


so, problem solved ! (laughing too early here ..)

the sum Mat in bif.cpp:204 should be accessed like<double>(y1,x1) , not float.

so: get updates with: git pull upstream master

and rebuild (cmake, make make install)

edit flag offensive delete link more


pr got merged, - just update opencv_contrib, and rebuild ;)

berak gravatar imageberak ( 2016-10-06 09:55:51 -0500 )edit

@atv, bif + (some feature reduction, like dct to 6k) + MLP seriously beats fisherfaces on att, check it out ! ;)

berak gravatar imageberak ( 2016-10-06 10:56:27 -0500 )edit

i'll certainly have a look. i haven't done much with mlp yet.

any proof of concept code i can take a peek at?

atv gravatar imageatv ( 2016-10-07 05:46:45 -0500 )edit

@berak Thank you for your solution and your quick response.

EvaPant gravatar imageEvaPant ( 2016-10-10 07:38:14 -0500 )edit

ohh, i absolutely forgot to ask about the test !

which part of it failed ? the count ? or did it throw ?

just could not reproduce it on my win/x64 box ;(

berak gravatar imageberak ( 2016-10-10 07:40:16 -0500 )edit

@berak Are you addressing this question to me? I am not sure what you mean.

EvaPant gravatar imageEvaPant ( 2016-10-10 07:59:27 -0500 )edit

apologies, strike that.

(mistook, who wrote the comment)

berak gravatar imageberak ( 2016-10-10 08:09:46 -0500 )edit

@berak hello again. Sorry for taking back the acceptance and making edits on my post, but I am still having issues on the output values of bif->compute(image, fea);. The NaN values still insist for some images. Is it possible to give a little more help?

EvaPant gravatar imageEvaPant ( 2016-10-10 15:49:24 -0500 )edit

well, you're right. atm. , the problem seems to be here, if sd/area < mean*mean , we get negative numbers, and the sqrt of that yields nan. still inquiring why, and how to fix it.

also, any comment / help / finding from you highly appreciated ! (i don't care if the question is accepted or not, what matters is, if there is a bug (and where))

berak gravatar imageberak ( 2016-10-11 01:33:29 -0500 )edit

hello @berak, I have found a possible reason why NaN values still appear. Any comments on that are highly appreciated.

EvaPant gravatar imageEvaPant ( 2016-10-17 06:32:03 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-10-05 14:56:08 -0500

Seen: 444 times

Last updated: Oct 17 '16