我正在尝试用C ++实现P. Viola和M. Jones检测框架(一开始,只是序列分类器 - 不是级联版本)。我想我已经设计了所有必需的类和模块(例如Integral images,Haar features),尽管有一个 - 最重要的:AdaBoost核心算法。
我读过P. Viola和M. Jones的原始论文和许多其他出版物。不幸的是,我仍然不明白我应该如何找到一个弱分类器的最佳阈值?我发现只有小的参考“加权中位数”和“高斯分布”算法和许多数学公式......
我曾尝试使用OpenCV Train Cascade模块源作为模板,但它非常全面,因此对代码进行逆向工程非常耗时。我还编写了自己的简单代码来理解Adaptive Boosting的想法。
问题是:你能解释一下计算一个弱分类器最佳阈值的最佳方法吗?
下面我将介绍AdaBoost伪代码,该代码是从Google中的样本重写的,但我不相信它是否正确接近。计算一个弱分类器非常慢(几个小时),我对计算最佳阈值的方法有所怀疑。
(1) AdaBoost::FindNewWeakClassifier
(2) AdaBoost::CalculateFeatures
(3) AdaBoost::FindBestThreshold
(4) AdaBoost::FindFeatureError
(5) AdaBoost::NormalizeWeights
(6) AdaBoost::FindLowestError
(7) AdaBoost::ClassifyExamples
(8) AdaBoost::UpdateWeights
DESCRIPTION (1)
-Generates all possible arrangement of features in detection window and put to the vector
DO IN LOOP
-Runs main calculating function (2)
END
DESCRIPTION(2)
-Normalizes weights (5)
DO FOR EACH HAAR FEATURE
-Puts sequentially next feature from list on all integral images
-Finds the best threshold for each feature (3)
-Finds the error for each the best feature in current iteration (4)
-Saves errors for each the best feature in current iteration in array
-Saves threshold for each the best feature in current iteration in array
-Saves the threshold sign for each the best feature in current iteration in array
END LOOP
-Finds for classifier index with the lowest error selected by above loop (6)
-Gets the value of error from the best feature
-Calculates the value of the best feature in the all integral images (7)
-Updates weights (8)
-Adds new, weak classifier to vector
DESCRIPTION (3)
-Calculates an error for each feature threshold on positives integral images - seperate for "+" and "-" sign (4)
-Returns threshold and sign of the feature with the lowest error
DESCRIPTION(4)
- Returns feature error for all samples, by calculating inequality f(x) * sign < sign * threshold
DESCRIPTION (5)
-Ensures that samples weights are probability distribution
DESCRIPTION (6)
-Finds the classifier with the lowest error
DESCRIPTION (7)
-Calculates a value of the best features at all integral images
-Counts false positives number and false negatives number
DESCRIPTION (8)
-Corrects weights, depending on classification results
感谢您的帮助
答案 0 :(得分:14)
在最初的中提琴 - 琼斯论文here中,3.1学习讨论(准确地说,第4段),您将找到找到最佳阈值的程序。
我将在下面快速总结一下这个方法。
每个特征的最佳阈值取决于样本权重,因此在adaboost的非常迭代中计算。如伪代码中所述,保存最佳弱分类器的阈值。
在每一轮中,对于每个弱分类器,您必须根据特征值排列N个训练样本。设置阈值将分为两部分。两个部分都将具有大多数的正样本或负样本以及一些其他类型的样本。
T+
:正样本权重的总和T-
:负样品重量的总和S+
:低于阈值的正样本权重之和S-
:低于阈值的负样本权重之和此特定阈值的错误是 -
e = MIN((S+) + (T-) - (S-), (S-) + (T+) - (S+))
为什么最低限度?这是一个例子:
如果样本和阈值是这样的 -
+ + + + + - - | + + - - - - -
在第一轮中,如果所有权重相等(= w),则取最小值将导致错误为4*w
,而不是10*w
。
您为所有N种分离样品的方法计算此错误
最小误差将为您提供阈值范围。实际阈值可能是相邻特征值的平均值(我不确定,对此进行一些研究)
这是您DO FOR EACH HAAR FEATURE
循环中的第二步
与OpenCV一起提供的级联由Rainer Lienhart创建,我不知道他使用了什么方法。
您可以密切关注OpenCV源代码,以进一步改进此过程。