Repository: opencv
Issue: Traincascade: grabbing negative window samples is seriously broken
Labels: bug,affected: 3.4,category: apps

### Please state the information for your system
- OpenCV version: 3.x --> latest master branch
- Host OS: Linux (Ubuntu 14.04 & 16.04) 
### In which part of the OpenCV library you got the issue?

apps --> opencv_traincascade process
### Expected behaviour

Providing more then enough negative samples is a common approach in computer vision. This makes sure that through the rescaling and subsampling windows, the train cascade application succeeds in grabbing multiple millions of windows from a set of larger images.

However this is not the case IF you supply a limited set of data.
### Actual behaviour & problem description

Imagine the following setup (just confirmed on a detector setup I have here)
- Given 2500 positive windows of 19x14 pixels
- Given 1 negative image of 66x28 pixels, from which negative windows can be grabbed
- Training LBP feature cascade classifier with `-numStages 25 -w 19 -h 14`
### Generated output

```
spu@TOBCAT:/data/zonnepanelen_test$ opencv_traincascade -data cascade/ -vec data.vec -bg negatives.txt -numPos 2500 -numNeg 10000 -numStages 25 -precalcValBufSize 8000 -precalcIdxBufSize 8000 -w 19 -h 14 -featureType LBP -minHitRate 0.999
PARAMETERS:
cascadeDirName: cascade/
vecFileName: data.vec
bgFileName: negatives.txt
numPos: 2500
numNeg: 10000
numStages: 25
precalcValBufSize[Mb] : 8000
precalcIdxBufSize[Mb] : 8000
acceptanceRatioBreakValue : -1
stageType: BOOST
featureType: LBP
sampleWidth: 19
sampleHeight: 14
boostType: GAB
minHitRate: 0.999
maxFalseAlarmRate: 0.5
weightTrimRate: 0.95
maxDepth: 1
maxWeakCount: 100
Number of unique features given windowSize [19,14] : 1710

===== TRAINING 0-stage =====
<BEGIN
POS count : consumed   2500 : 2500
NEG count : acceptanceRatio    10000 : 1
Precalculation time: 0
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|        1|
+----+---------+---------+
|   4|   0.9996|   0.1268|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 3 seconds.

===== TRAINING 1-stage =====
<BEGIN
POS count : consumed   2500 : 2501
NEG count : acceptanceRatio    10000 : 0.127781
Precalculation time: 0
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|        1|        1|
+----+---------+---------+
|   3|        1|        1|
+----+---------+---------+
|   4|   0.9996|   0.0994|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 8 seconds.

===== TRAINING 2-stage =====
<BEGIN
POS count : consumed   2500 : 2502
NEG count : acceptanceRatio    10000 : 0.0126893
Precalculation time: 0
+----+---------+---------+
|  N |    HR   |    FA   |
+----+---------+---------+
|   1|        1|        1|
+----+---------+---------+
|   2|   0.9992|        0|
+----+---------+---------+
END>
Training until now has taken 0 days 0 hours 0 minutes 33 seconds.

===== TRAINING 3-stage =====
<BEGIN
POS count : consumed   2500 : 2504
```
### Remarks on the generated output
1. How is it possible that from a single window of 66x18 pixels, more then 3 stages of 10.000 negatives can be grabbed? Imagining that the second stages only takes negatives that are not classified as negatives by the previous cascade, this can grow easily up to 40.000 - 50.000 samples just for those 3 stages trained. 
2. A known problem is that the training hangs once it cannot find new negative samples. But in my opinion in this case it hangs way to late, and should rather drop an error message saying, `oh hello, I cannot find any more negative windows, do provide me more if you want to continue training`