public void TestGetFeatureValue1() { double eps = 0.000001; Feature feature1 = new Feature(FeatureType.type1, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature2 = new Feature(FeatureType.type2, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature3 = new Feature(FeatureType.type3, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature4 = new Feature(FeatureType.type4, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); double f1 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature1); double f2 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature2); double f3 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature3); double f4 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature4); double f1ref = DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y + DebugInfo2.h, DebugInfo2.w, DebugInfo2.h).getSum(); double f2ref = DebugInfo1.im.getSubMatrix(DebugInfo2.x + DebugInfo2.w, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum(); double f3ref = DebugInfo1.im.getSubMatrix(DebugInfo2.x + DebugInfo2.w, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x + 2 * DebugInfo2.w, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum(); double f4ref = DebugInfo1.im.getSubMatrix(DebugInfo2.x + DebugInfo2.w, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() + DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y + DebugInfo2.h, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h).getSum() - DebugInfo1.im.getSubMatrix(DebugInfo2.x + DebugInfo2.w, DebugInfo2.y + DebugInfo2.h, DebugInfo2.w, DebugInfo2.h).getSum(); Assert.IsTrue(Math.Abs(f1 - f1ref) < eps); Assert.IsTrue(Math.Abs(f2 - f2ref) < eps); Assert.IsTrue(Math.Abs(f3 - f3ref) < eps); Assert.IsTrue(Math.Abs(f4 - f4ref) < eps); }
public static Matrix getAllFeatureValues(Matrix[] integral_images, Feature[] allFeatures) { Matrix result = new Matrix(integral_images.Length, allFeatures.Length); for (int i = 1; i <= allFeatures.Length; i++) { result.setRow(i, getFeatureValues(integral_images, allFeatures[i-1])); } return result; }
public static AdaBoostRespons[] executeAdaBoost(Matrix[] integralImages, Feature[] allFeatures, Matrix isFaceList, int nrNegative, int nrWeakClassifiers) { FeatureType[] featureTypes = (from feature in allFeatures select feature.type).Distinct().ToArray(); InitiateFeatureValues(integralImages, allFeatures, featureTypes); AdaBoostRespons[] adaBoostRespons = new AdaBoostRespons[nrWeakClassifiers]; Matrix weights = getInitializedWeights(isFaceList.nr_vals - nrNegative, nrNegative); for (int t = 0; t < nrWeakClassifiers; t++) { weights = weights / weights.getSum(); FeatureIndex bestFeatureIndex = new FeatureIndex(FeatureType.type1, int.MinValue); WeakClassifier bestClassifier = new WeakClassifier(int.MaxValue, double.MaxValue, double.MaxValue); foreach (FeatureType featureType in featureTypes) { Matrix allFeatureValuesOfSameType = Matrix.DeserializeFromXML("FeatureValues_" + featureType.ToString() + ".xml"); for (int j = 1; j <= allFeatureValuesOfSameType.nr_rows; j++) { WeakClassifier weakClassifier = learnWeakClassifier(allFeatureValuesOfSameType, weights, isFaceList, j); if (weakClassifier.error < bestClassifier.error) { bestClassifier = weakClassifier; bestFeatureIndex.featureType = featureType; bestFeatureIndex.j = j; } } } double beta = bestClassifier.error / (1 - bestClassifier.error); double alpha = Math.Log(1 / beta); Matrix featureValuesOfSameType = Matrix.DeserializeFromXML("FeatureValues_" + bestFeatureIndex.featureType.ToString() + ".xml"); for (int i = 1; i <= featureValuesOfSameType.nr_cols; i++) { int classification = getWeakClassification(featureValuesOfSameType[i, bestFeatureIndex.j], bestClassifier.parity, bestClassifier.threshold); weights[i] = Math.Abs(classification - isFaceList[i]) == 0 ? weights[i] * beta : weights[i]; } adaBoostRespons[t] = new AdaBoostRespons(bestClassifier, alpha, bestFeatureIndex); } return adaBoostRespons; }
public void TestGetFeatureValue2() { double eps = 0.000001; Feature feature1 = new Feature(FeatureType.type1, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature2 = new Feature(FeatureType.type2, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature3 = new Feature(FeatureType.type3, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); Feature feature4 = new Feature(FeatureType.type4, DebugInfo2.x, DebugInfo2.y, DebugInfo2.w, DebugInfo2.h); double f1 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature1); double f2 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature2); double f3 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature3); double f4 = Feature.getFeatureValue(DebugInfo1.ii_mm, feature4); Assert.IsTrue(Math.Abs(DebugInfo2.f1 - f1) < eps); Assert.IsTrue(Math.Abs(DebugInfo2.f2 - f2) < eps); Assert.IsTrue(Math.Abs(DebugInfo2.f3 - f3) < eps); Assert.IsTrue(Math.Abs(DebugInfo2.f4 - f4) < eps); }
public Classifier(double[] alphas, double[] thetas, Feature[] features, Feature[] all_features) { this.alphas = alphas; this.thetas = thetas; this.features = features; this.all_features = all_features; }
private static void InitiateFeatureValues(Matrix[] integralImages, Feature[] allFeatures, FeatureType[] featureTypes) { foreach (FeatureType featureType in featureTypes) { Feature[] allFeaturesOfSameType = (from feature in allFeatures where feature.type.Equals(featureType) select feature).ToArray(); Matrix allFeatureValuesOfSameType = Feature.getAllFeatureValues(integralImages, allFeaturesOfSameType); allFeatureValuesOfSameType.saveToXML("FeatureValues_" + featureType.ToString()); } }
/// CHECKED /// <summary> /// /// </summary> /// <param name="feature"></param> /// <param name="featureWidth"></param> /// <param name="featureHeight"></param> /// <param name="filename"></param> public static void saveFeatureImage(Feature feature, int featureWidth, int featureHeight, string filename) { int k = 5; int x = feature.x * k; int y = feature.y * k; int w = feature.w * k; int h = feature.h * k; int width = featureWidth * k; int height = featureHeight * k; Bitmap bitMapImage = new Bitmap(width, height); switch (feature.type) { case FeatureType.type1: fillRectangle(ref bitMapImage, x, y, w, h, Color.Red); fillRectangle(ref bitMapImage, x, y + h, w, h, Color.Green); break; case FeatureType.type2: fillRectangle(ref bitMapImage, x, y, w, h, Color.Green); fillRectangle(ref bitMapImage, x + w, y, w, h, Color.Red); break; case FeatureType.type3: fillRectangle(ref bitMapImage, x, y, w, h, Color.Green); fillRectangle(ref bitMapImage, x + w, y, w, h, Color.Red); fillRectangle(ref bitMapImage, x + w + w, y, w, h, Color.Green); break; case FeatureType.type4: fillRectangle(ref bitMapImage, x, y, w, h, Color.Green); fillRectangle(ref bitMapImage, x + w, y, w, h, Color.Red); fillRectangle(ref bitMapImage, x, y + h, w, h, Color.Red); fillRectangle(ref bitMapImage, x + w, y + h, w, h, Color.Green); break; } EncoderParameters encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); bitMapImage.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParameters); }
/// CHECKED /// <summary> /// /// </summary> /// <param name="integral_images"></param> /// <param name="feature"></param> /// <returns></returns> public static Matrix getFeatureValues(Matrix[] integral_images, Feature feature) { double[] values = (from integral_image in integral_images select getFeatureValue(integral_image, feature)).ToArray(); return new Matrix(values.Length, 1, values); }
/// CHECKED /// <summary> /// /// </summary> /// <param name="integral_image"></param> /// <param name="feature"></param> /// <returns></returns> public static double getFeatureValue(Matrix integral_image, Feature feature) { double val = 0; int x = feature.x; int y = feature.y; int w = feature.w; int h = feature.h; switch (feature.type) { case FeatureType.type1: val = getBoxSum(integral_image, x, y, w, h); val -= getBoxSum(integral_image, x, y + h, w, h); break; case FeatureType.type2: val = getBoxSum(integral_image, x + w, y, w, h); val -= getBoxSum(integral_image, x, y, w, h); break; case FeatureType.type3: val = getBoxSum(integral_image, x + w, y, w, h); val -= getBoxSum(integral_image, x, y, w, h); val -= getBoxSum(integral_image, x + 2 * w, y, w, h); break; case FeatureType.type4: val = getBoxSum(integral_image, x + w, y, w, h); val += getBoxSum(integral_image, x, y + h, w, h); val -= getBoxSum(integral_image, x, y, w, h); val -= getBoxSum(integral_image, x + w, y + h, w, h); break; } return val; }
public void TestSaveFeatureImage() { Feature feature1 = new Feature(FeatureType.type1, 2, 2, 3, 4); Feature.saveFeatureImage(feature1, 19, 19, "test1.jpg"); Feature feature2 = new Feature(FeatureType.type4, 5, 5, 5, 5); Feature.saveFeatureImage(feature2, 19, 19, "test2.jpg"); Assert.IsFalse(false); }