Answers.Body: <p>I would look at <a href="https://www.cs.cmu.edu/%7Eefros/courses/LBMV07/Papers/viola-cvpr-01.pdf" rel="nofollow noreferrer">table 1</a> of the original paper. While you're reading the alogorithm, try to really focus on Step 2 when you get to it.</p>
  <p><a href="https://i.sstatic.net/om3xF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/om3xF.png" alt="Table 1" /></a></p>
  <p>In summary, each feature is used to train it's own classifier. So in your example, the calculated features <code>X1</code>, <code>X2</code>, ... <code>Xn</code> you describe coorespond to apply some set of feature transforms <code>f_1</code>, <code>f_2</code>, ... <code>f_n</code> to a single image. This is a bit backwards from what actually happens. What the method really does is train a classifier for each feature. So if you had <code>n</code> features, you would have <code>n</code> classifiers. Then in an adaboost fashion, you upweight the classifier that performed the best. I.e, you are upweighting the classifier based soley on th best performing feature. You then repeat and re-weight all the classifiers until you reach convergence.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T18:45:55.893Z
Answers.Text: Sorry if this is stupid thing to ask - but in your answer when you say there are `X1`,...`Xn` the n features - they are coming from the same Haar featuretemplate shown in example in the question right? So when are we using the all of the haar feature templates? (Btw +1 for your effort in answering my bad question maybe)
Answers.UserDisplayName: user27286
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T19:41:27.367Z
Answers.Text: Nah, no stupid questions! Only stupid answers on my part. More concretely, each haar feature template would be used to create a single classifier. So if you have `n` haar feature templates you have `n` classifiers. For each of those `n` classifiers, you train the entire image set and use adaboost to come to a final solution.
Answers.UserDisplayName: juicedatom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T19:54:56.030Z
Answers.Text: I have n haar feature templates. So for example, one template is as shown in my question. Now if I slide this feature template over an image I will get many scalar values due to different location on which it is slid over. All I am asking this, what would I do with all these values? Do I just put them a vector and tell that it is my feature corresponding to the feature template? Also just notice, in my question I said 'X1` is the output of running this feature templated in one location. `X2` is the output of running the *same* feature template over different position.You define `X1` otherway.
Answers.UserDisplayName: user27286
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T20:23:11.610Z
Answers.Text: In regards to you saying,
  
  "Do I just put them into a vector and tell that it my feature cooresponding to the feature template"
  
  The answer is yes. The point is that you're going to have a ton of features in the begining (on the order of thousands) where most of them are actually useless and will get filtered out (or upweighted) by adaboost.
  
  I found a link to a python implementation that seems much easier to read than staring at the opencv implementation if that helps https://github.com/ZihengZZH/viola-jones/blob/master/src/adaboost.py#L44.
Answers.UserDisplayName: juicedatom
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T20:46:26.447Z
Answers.Text: I will select this as answer and I will put another answer drawing whatever you said. Thanks.
Answers.UserDisplayName: user27286
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-10T18:30:22.327Z
Answers.LastActivityDate: 2021-04-10T18:37:53.963Z
Answers.LastEditDate: 2021-04-10T18:37:53.963Z
Answers.UserDisplayName: juicedatom
Body: <p>If I have an image like this</p>
  <pre><code>1 2 3 4 5 6 7 8
  a b c d e f g h
  ...
  </code></pre>
  <p>And I apply a Haar-like feature with a template</p>
  <pre><code>1 1 1 1 
  -1 -1 -1 -1
  </code></pre>
  <p>Then in the first position we get <code>X1 = 1+2+3+4+a+b+c+d</code>. If we slide one side to the right, we again get <code>X2 = 2+3+4+5+b+c+d+e</code>.</p>
  <p>This way we will get <code>X1</code> and <code>X2</code> and <code>X3</code> and so on. Now, how are these values combined to get the feature? Because when we say a feature we are not just running that template in one place, rather we will run it over multiple places in the image. It gives lots of values like <code>X1</code>,<code>X2</code> and <code>X3</code> and so on. Now, how are those combined to get the final feature which will be passed to Adaboost?</p>
  
Tags: <computer-vision><object-detection><image-processing>
Title: Viola-Jones algorithm: Haar-like features, how are the features extracted?
UserDisplayName: user27286
ThreadText: # Viola-Jones algorithm: Haar-like features, how are the features extracted?
  
  (Asked by: username_0 on 2021-04-10)
  
  If I have an image like this
  
  ```
  1 2 3 4 5 6 7 8
  a b c d e f g h
  ...
  
  ```
  
  And I apply a Haar-like feature with a template
  
  ```
  1 1 1 1 
  -1 -1 -1 -1
  
  ```
  
  Then in the first position we get `X1 = 1+2+3+4+a+b+c+d`. If we slide one side to the right, we again get `X2 = 2+3+4+5+b+c+d+e`.
  
  This way we will get `X1` and `X2` and `X3` and so on. Now, how are these values combined to get the feature? Because when we say a feature we are not just running that template in one place, rather we will run it over multiple places in the image. It gives lots of values like `X1`,`X2` and `X3` and so on. Now, how are those combined to get the final feature which will be passed to Adaboost?
  
  **Tags:** computer-vision, object-detection, image-processing
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-04-10. Score: 2 | Accepted answer
  
  I would look at [table 1](https://www.cs.cmu.edu/%7Eefros/courses/LBMV07/Papers/viola-cvpr-01.pdf) of the original paper. While you're reading the alogorithm, try to really focus on Step 2 when you get to it.
  
  [![Table 1](https://i.sstatic.net/om3xF.png)](https://i.sstatic.net/om3xF.png)
  
  In summary, each feature is used to train it's own classifier. So in your example, the calculated features `X1`, `X2`, ... `Xn` you describe coorespond to apply some set of feature transforms `f_1`, `f_2`, ... `f_n` to a single image. This is a bit backwards from what actually happens. What the method really does is train a classifier for each feature. So if you had `n` features, you would have `n` classifiers. Then in an adaboost fashion, you upweight the classifier that performed the best. I.e, you are upweighting the classifier based soley on th best performing feature. You then repeat and re-weight all the classifiers until you reach convergence.
  
  > **Comments:**
  > **username_0**: Sorry if this is stupid thing to ask - but in your answer when you say there are `X1`,...`Xn` the n features - they are coming from the same Haar featuretemplate shown in example in the question right? So when are we using the all of the haar feature templates? (Btw +1 for your effort in answering my bad question maybe)  
  > **username_1**: Nah, no stupid questions! Only stupid answers on my part. More concretely, each haar feature template would be used to create a single classifier. So if you have `n` haar feature templates you have `n` classifiers. For each of those `n` classifiers, you train the entire image set and use adaboost to come to a final solution.  
  > **username_0**: I have n haar feature templates. So for example, one template is as shown in my question. Now if I slide this feature template over an image I will get many scalar values due to different location on which it is slid over. All I am asking this, what would I do with all these values? Do I just put them a vector and tell that it is my feature corresponding to the feature template? Also just notice, in my question I said 'X1` is the output of running this feature templated in one location. `X2` is the output of running the *same* feature template over different position.You define `X1` otherway.  
  > **username_1**: In regards to you saying,  "Do I just put them into a vector and tell that it my feature cooresponding to the feature template"  The answer is yes. The point is that you're going to have a ton of features in the begining (on the order of thousands) where most of them are actually useless and will get filtered out (or upweighted) by adaboost.  I found a link to a python implementation that seems much easier to read than staring at the opencv implementation if that helps https://github.com/ZihengZZH/viola-jones/blob/master/src/adaboost.py#L44.  
  > **username_0**: I will select this as answer and I will put another answer drawing whatever you said. Thanks.  
  
  ---
  
  