Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:52:18+00:00 2026-05-31T00:52:18+00:00

I’m trying to implement the Viola Johns face detection algorithm on Cuda platform (I’m

  • 0

I’m trying to implement the Viola Johns face detection algorithm on Cuda platform (I’m aware that openCV already did that, I do that for my school).

My first phase is to implement the algorithm on CPU.

I’m using openCV library, I know openCV knows how to do face detection, In order to understand, I would like to get back to basic and do it my own way.

I created the integral sum representation, and the squere sum integral representation using openCV function.

I iterated through the cascade. iterated through the stages, classfiers and rects. Normalized each window, calculated the sum of each classifer and compared to the threshold, Sadly it’s seems like I’m missing something. because I can’t detect faces.

It seems like I need to get better understanding of the the cascade XML file.

Here is an example:

          <!-- tree 158 -->
      <_>
        <!-- root node -->
        <feature>
          <rects>
            <_>3 6 2 2 -1.</_>
            <_>3 6 1 1 2.</_>
            <_>4 7 1 1 2.</_></rects>
          <tilted>0</tilted></feature>
        <threshold>2.3729570675641298e-003</threshold>
        <left_val>0.4750812947750092</left_val>
        <right_val>0.7060170769691467</right_val></_></_>
    <_>
      <!-- tree 159 -->

          <!-- tree 159 -->
      <_>
        <!-- root node -->
        <feature>
          <rects>
            <_>16 6 3 2 -1.</_>
            <_>16 7 3 1 2.</_></rects>
          <tilted>0</tilted></feature>
        <threshold>-1.4541699783876538e-003</threshold>
        <left_val>0.3811730146408081</left_val>
        <right_val>0.5330739021301270</right_val></_></_></trees>
  <stage_threshold>79.2490768432617190</stage_threshold>
  <parent>16</parent>
  <next>-1</next></_>
<_>

I’d like to understand what is the meaning of the left_val and the right_val? What is the meaning of the parent, next values? How to calculate each classifier normalized sum? Is there anything I’m doing wrong here?

See my code attached.

int RunHaarClassifierCascadeSum(CascadeClassifier * face_cascade, CvMat*  image , CvMat* sum , CvMat* sqsum,
                            CvMat* tilted,CvSize *scaningWindowSize, int iteratorRow, int iteratorCol )
{

// Normalize the current scanning window - Detection window
// Variance(x) = E(x^2) - (E(x))^2 = detectionWindowSquereExpectancy - detectionWindowExpectancy^2 
// Expectancy(x) = E(x) = sum_of_pixels / size_of_window

double detectionWindowTotalSize = scaningWindowSize->height * scaningWindowSize->width;

// calculate the detection Window Expectancy , e.g the E(x) 
double sumDetectionWindowPoint1,sumDetectionWindowPoint2,sumDetectionWindowPoint3,sumDetectionWindowPoint4;     //  ______________________
sumDetectionWindowPoint1 = cvGetReal2D(sum,iteratorRow,iteratorCol);                                            //  |R1                R2|
sumDetectionWindowPoint2 = cvGetReal2D(sum,iteratorRow+scaningWindowSize->width,iteratorCol);                   //  |                    |   Sum = R4-R2-R3+R1
sumDetectionWindowPoint3 = cvGetReal2D(sum,iteratorRow,iteratorCol+scaningWindowSize->height);                  //  |R3________________R4|
sumDetectionWindowPoint4 = cvGetReal2D(sum,iteratorRow+scaningWindowSize->width,iteratorCol+scaningWindowSize->height);
double detectionWindowSum = calculateSum(sumDetectionWindowPoint1,sumDetectionWindowPoint2,sumDetectionWindowPoint3,sumDetectionWindowPoint4);
const double detectionWindowExpectancy = detectionWindowSum / detectionWindowTotalSize;     // E(x) 

// calculate the Square detection Window Expectancy , e.g the E(x^2) 
double squareSumDetectionWindowPoint1,squareSumDetectionWindowPoint2,squareSumDetectionWindowPoint3,squareSumDetectionWindowPoint4;     //  ______________________
squareSumDetectionWindowPoint1 = cvGetReal2D(sqsum,iteratorRow,iteratorCol);                                            //  |R1                R2|
squareSumDetectionWindowPoint2 = cvGetReal2D(sqsum,iteratorRow+scaningWindowSize->width,iteratorCol);                   //  |                    |   Sum = R4-R2-R3+R1
squareSumDetectionWindowPoint3 = cvGetReal2D(sqsum,iteratorRow,iteratorCol+scaningWindowSize->height);                  //  |R3________________R4|
squareSumDetectionWindowPoint4 = cvGetReal2D(sqsum,iteratorRow+scaningWindowSize->width,iteratorCol+scaningWindowSize->height);
double detectionWindowSquareSum = calculateSum(squareSumDetectionWindowPoint1,squareSumDetectionWindowPoint2,squareSumDetectionWindowPoint3,squareSumDetectionWindowPoint4);
const double detectionWindowSquareExpectancy = detectionWindowSquareSum / detectionWindowTotalSize;     // E(x^2)

const double detectionWindowVariance = detectionWindowSquareExpectancy - std::pow(detectionWindowExpectancy,2);  // Variance(x) = E(x^2) - (E(x))^2 
const  double detectionWindowStandardDeviation = std::sqrt(detectionWindowVariance);

if (detectionWindowVariance<=0)
    return -1 ; // Error 

// Normalize the cascade window to the normal scale window
double normalizeScaleWidth = double(scaningWindowSize->width / face_cascade->oldCascade->orig_window_size.width);
double normalizeScaleHeight = double(scaningWindowSize->height / face_cascade->oldCascade->orig_window_size.height);

// Calculate the cascade for each one of the windows
for( int stageIterator=0; stageIterator< face_cascade->oldCascade->count; stageIterator++ )      // Stage iterator
{

    CvHaarStageClassifier* pCvHaarStageClassifier = face_cascade->oldCascade->stage_classifier + stageIterator;
    for (int CvHaarStageClassifierIterator=0;CvHaarStageClassifierIterator<pCvHaarStageClassifier->count;CvHaarStageClassifierIterator++)    // Classifier iterator
    {
        CvHaarClassifier* classifier = pCvHaarStageClassifier->classifier + CvHaarStageClassifierIterator;
        float classifierSum=0.;

        for( int CvHaarClassifierIterator = 0; CvHaarClassifierIterator < classifier->count;CvHaarClassifierIterator++ )    // Feature iterator
        {
            CvHaarFeature * pCvHaarFeature = classifier->haar_feature;

            // Remark
            if (pCvHaarFeature->tilted==1)
                break; 
            // Remark

            for( int CvHaarFeatureIterator = 0; CvHaarFeatureIterator< CV_HAAR_FEATURE_MAX; CvHaarFeatureIterator++ )   // 3 Features iterator 
            {
                CvRect * currentRect = &(pCvHaarFeature->rect[CvHaarFeatureIterator].r);
                // Normalize the rect to the scaling window scale
                CvRect normalizeRec;
                normalizeRec.x = (int)(currentRect->x*normalizeScaleWidth); 
                normalizeRec.y = (int)(currentRect->y*normalizeScaleHeight);
                normalizeRec.width = (int)(currentRect->width*normalizeScaleWidth); 
                normalizeRec.height = (int)(currentRect->height*normalizeScaleHeight); 
                
                double sumRectPoint1,sumRectPoint2,sumRectPoint3,sumRectPoint4;                             //  ______________________
                sumRectPoint1 = cvGetReal2D(sum,normalizeRec.x,normalizeRec.y);                             //  |R1                R2|
                sumRectPoint2 = cvGetReal2D(sum,normalizeRec.x+normalizeRec.width,normalizeRec.y);          //  |                    |   Sum = R4-R2-R3+R1
                sumRectPoint3 = cvGetReal2D(sum,normalizeRec.x,normalizeRec.y+normalizeRec.height);         //  |R3________________R4|
                sumRectPoint4 = cvGetReal2D(sum,normalizeRec.x+normalizeRec.width,normalizeRec.y+normalizeRec.height);

                double nonNormalizeRect = calculateSum(sumRectPoint1,sumRectPoint2,sumRectPoint3,sumRectPoint4);        //
                double sumMean = detectionWindowExpectancy*(normalizeRec.width*normalizeRec.height);                    // sigma(Pi) = normalizeRect  = (sigma(Pi- rect) - sigma(mean)) / detectionWindowStandardDeviation
                double normalizeRect = (nonNormalizeRect - sumMean)/detectionWindowStandardDeviation;                   //
                    
                classifierSum += (normalizeRect*(pCvHaarFeature->rect[CvHaarFeatureIterator].weight));
            }
        }
 //             if (classifierSum > (*(classifier->threshold)) )
 //                 return 0;       // That's not a face !  
        if (classifierSum > ((*(classifier->threshold))*detectionWindowStandardDeviation) )
            return -stageIterator;      // That's not a face !  , failed on stage number 

    }
}
return 1;   // That's a face 
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:52:19+00:00Added an answer on May 31, 2026 at 12:52 am

    You need to make some big changes. First of all classifier->threshold is a threshold for each feature. classifier->alpha points to an array made of 2 elements – left_val and right_val(to my understanding). You should put something like this after the classifier loop-

    a = classifier->alpha[0]
    b = classifier->alpha[1]
    t = *(classifier->threshold)
    stage_sum += classifierSum < t ? a : b
    

    then compare stage_sum with CvHaarStageClassifier::threshold which is the stage threshold, loop through stage_classifiers[i] .if it passes all of them then its a face!
    ‘parent’ and ‘next’ are useless here if you use haarcascade_frontalface_alt.xml, it is just a stump based cascade and not a tree based.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.