2

I'm trying to modify OpenCV's haar Cascade Classifier into specific purpose and looked into its source code from up to down. But I stuck on understanding very final part which is calculating feature value in line 361 of cascadedetect.hpp

return optfeaturesPtr[featureIdx].calc(pwin) * varianceNormFactor;

and its sub part for (a) calculating each rectangles weighted sum in line 398-407 of cascadedetect.hpp

inline float HaarEvaluator::OptFeature :: calc( const int* ptr ) const
{
    float ret = weight[0] * CALC_SUM_OFS(ofs[0], ptr) +
            weight[1] * CALC_SUM_OFS(ofs[1], ptr);

    if( weight[2] != 0.0f )
        ret += weight[2] * CALC_SUM_OFS(ofs[2], ptr);

    return ret;
}

and (b) calculating varianceNormFactor in line 691-697 and 701 of cascadedetect.cpp

pwin = &sbuf.at<int>(pt) + s.layer_ofs;
const int* pq = (const int*)(pwin + sqofs);
int valsum = CALC_SUM_OFS(nofs, pwin);
unsigned valsqsum = (unsigned)(CALC_SUM_OFS(nofs, pq));

double area = normrect.area();
double nf = area * valsqsum - (double)valsum * valsum;

line:701 varianceNormFactor = (float)(1./nf);

From my little knowledge I guess 'pwin' represents actual window which is currently in progress, but (Q1) What does pq mean? I couldn't find any line which gives value into sbuf variable (which somehow connected to everything above)

According to Rainer Lienhart's paper which was mentioned by OpenCV's team they used this equation for light correction. But in this paper they said we can calculate σ just by looking at 4 values in integral image from each pixel's square. (Q2) But aren't we supposed to subtract average from each pixels BEFORE taking sum of squares in order to calculate standard deviation or σ in this paper represent something different?

And from source I think equation they used must be similar to this instead. (Q3) So If possible, Where can I get maths behind these codes or detailed reference material for this part? I've red OpenCV's reference manual but didn't find anything about it.

Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
BilguunCH
  • 51
  • 3

1 Answers1

0
(Q1) What does pq mean:

I think it may implies "Pointer to Square"; OpenCV may calculate the Square of all pixels and append it to a buffer B. So when do normalization, it can quickly refer to it by:

  1. the offset of current window in B, which is pwin.

  2. the offset of square value for each pixel within current window in B based on pwin, which is sqofs.

(Q2) But aren't we supposed to subtract average from each pixels BEFORE taking sum of squares in order to calculate standard deviation > or σ in this paper represent something different?

I have checked it on OpenCV 3.4.4 and, yes, it does NOT subtract the mean. I guess since for Haar pattern is symmetry, eg for Horizontal 2 one region subtract a region with the same area. Hence the effect sums up to zero and we can ignore it.

chang jc
  • 489
  • 8
  • 16