Ask Your Question
0

problem with compatreHist function need help

asked 2017-01-12 17:32:20 -0600

azdoud.y gravatar image

Dear OpenCV Community,

I had tried to work with compareHist built-in function using CV_64F to compare a personalized MatND variables as follow

MatND matTest(Size(1,8), CV_64F, Scalar(0));
  for(int i= 0; i < 8 ;i++){
       matTest.at<double>(i,0) = exp(i);
  }
cout <<compareHist( matTest, matTest,3)<<endl;

I got this

  OpenCV Error: Assertion failed (H1.type() == H2.type() && H1.type() == CV_32F) in cv::compareHist, file C:\buildslave64\win64_amdocl\2_4_PackSlave-win64-vc11-shared\opencv\modules\imgproc\src\histogram.cpp, line 1985

however, CV_32F changes my the results the exponential function exp(); that s why I'm using CV_64F

I need to fix this issue, glad for help

edit retag flag offensive close merge delete

Comments

You can cast to float after the exp() function. That should cut down on the error.

Tetragramm gravatar imageTetragramm ( 2017-01-12 20:19:51 -0600 )edit

unrelated, but rather use plain cv::Mat for clarity. MatND is just another typedef for Mat.

berak gravatar imageberak ( 2017-01-13 02:16:45 -0600 )edit

Yes @berak I knew that MatND is another kind of Mat, however, by using Mat CV_32F I lose data, regarding the Size(1,8) is just an example, actually, I'm dealing with lot of numbers

maybe compareHist() accepts only CV_32F, which causes me to lose data

7.3890562;
20.085537;
54.598148;
148.41316;
1096.6332;
8103.084;
22026.465;
59874.141;
162754.8;
442413.41;
1202604.3;
3269017.3;
1.#INF;
1.#INF;

but when I try with Mat CV_64F it works by saving data, but using compareHist() gives the above OpenCV Error

7.38906
20.0855
54.5982
148.413
1096.63
8103.08
22026.5
59874.1
162755
442413
1.2026e+006 
3.26902e+006
azdoud.y gravatar imageazdoud.y ( 2017-01-13 04:09:05 -0600 )edit

i understand your problem, unfortunately the internal code for BHATTACHARYYA is sse optimized for 32bit floats only.

berak gravatar imageberak ( 2017-01-13 04:19:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-01-13 04:36:32 -0600

berak gravatar image

in the end, if you need to keep your double data, you cannot use compareHist(), have to forsake all opencv optimization, and roll your own:

double BHATTACHARYYA(const Mat &h1, const Mat &h2) {
    CV_Assert(h1.type() == h2.type() == CV_64F);
    double result = 0;
    double s1=0, s2=0;
    for (int j=0; j<h1.rows; j++) {
        double a = h1.at<double>(j);
        double b = h2.at<double>(j);
        result += std::sqrt(a*b);
        s1 += a;
        s2 += b;
    }
    s1 *= s2;
    s1 = fabs(s1) > FLT_EPSILON ? 1./std::sqrt(s1) : 1.;
    result = std::sqrt(std::max(1. - result*s1, 0.));
    return result;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-01-12 17:32:20 -0600

Seen: 223 times

Last updated: Jan 13 '17