/// <summary> /// Gets the sum of the areas of the rectangular features in an integral image. /// </summary> /// public double GetSum(IntegralImage2 image, int x, int y) { double sum = 0.0; if (!Tilted) { // Compute the sum for a standard feature foreach (HaarRectangle rect in Rectangles) { sum += image.GetSum(x + rect.ScaledX, y + rect.ScaledY, rect.ScaledWidth, rect.ScaledHeight) * rect.ScaledWeight; } } else { // Compute the sum for a rotated feature foreach (HaarRectangle rect in Rectangles) { sum += image.GetSumT(x + rect.ScaledX, y + rect.ScaledY, rect.ScaledWidth, rect.ScaledHeight) * rect.ScaledWeight; } } return(sum); }
public void lena_test() { string localPath = NUnit.Framework.TestContext.CurrentContext.TestDirectory; #region doc_lena // In this example, we will compute an integral image // representation of Lena Söderberg's famous picture: TestImages testImages = new TestImages(path: localPath); Bitmap lena = testImages["lena.bmp"]; // get the image // Create a new Integral Image (squared and tilted) from Lena's picture: IntegralImage2 ii = IntegralImage2.FromBitmap(lena, computeTilted: true); // Let's say we would like to get the summed area in the rectangular region // delimited by pixel (34, 50) until pixels (60, 105). This is equivalent to // the region under the rectangle (34, 50, 34+60, 50+105) = (34, 50, 94, 155): long sum = ii.GetSum(34, 50, 94, 155); // this is the sum of values (1760032) // Now let's say we would like to get the squared sum and tilted sum as well: long ssum = ii.GetSum2(34, 50, 94, 155); // this is the sum of squared values (229508896) long tsum = ii.GetSumT(34, 50, 94, 155); // this is the sum of tilted values (-593600) #endregion Assert.AreEqual(1760032, sum); Assert.AreEqual(229508896, ssum); Assert.AreEqual(-593600, tsum); }
public void GetSumTest3() { // Example from Rainer Lienhart and Jochen Maydt: // "An Extended Set of Haar-like Features for Rapid Object Detection" int x = 6, y = 2, h = 4, w = 6; byte[,] img = { // 0 1 2 3 4 5 6 7 8 9 A B C D E /*0*/ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, /*1*/ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, /*2*/ { 9, 9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9 }, /*3*/ { 9, 9, 9, 9, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9 }, /*4*/ { 9, 9, 9, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9 }, /*5*/ { 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9 }, /*6*/ { 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9 }, /*7*/ { 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9 }, /*8*/ { 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9 }, /*9*/ { 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 9, 9, 9, 9, 9 }, /*A*/ { 9, 9, 9, 9, 9, 9, 9, 1, 1, 9, 9, 9, 9, 9, 9 }, /*B*/ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, /*C*/ { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }, }; // -RSAT(x-1,y-1) = [6-1,2-1] = [ 5, 1] => [ 1, 5] OK // +RSAT(x+w-1,y+w-1) = [6+6-1,2+6-1] = [11, 7] => [ 7,11] OK // -RSAT(x+w-1-h,y+w-1+h) = [6+6-1-4,2+6-1+4] = [ 7,11] => [11, 7] OK // +RSAT(x-h-1, y+h-1) = [6-4-1,2+4-1] = [ 1, 5] => [ 5, 1] OK // int sum = -iit[5,1] + iit[11,7] - iit[7,11] + iit[1,5]; // Create integral image Bitmap bmp = Accord.Imaging.Tools.ToBitmap(img); IntegralImage2 ii = IntegralImage2.FromBitmap(bmp, 0, true); // Tilted rectangular feature long[,] iit = tiltedIntegral3(img); long expected = 48; long sum = -(-iit[5 + (1), 1 + (2)] + iit[11 + (1), 7 + (2)] - iit[7 + (1), 11 + (2)] + iit[1 + (1), 5 + (2)]); long a = iit[y - 1 + (1), x - 1 + (2)]; long b = iit[y + w - 1 + (1), x + w - 1 + (2)]; long c = iit[y + w - 1 + h + (1), x + w - 1 - h + (2)]; long d = iit[y + h - 1 + (1), x - h - 1 + (2)]; long manual = -a + b - c + d; Assert.AreEqual(expected, sum); Assert.AreEqual(expected, manual); long actual = ii.GetSumT(x, y, w, h); Assert.AreEqual(expected, actual); }
/// <summary> /// Gets the sum of the areas of the rectangular features in an integral image. /// </summary> /// public double GetSum(IntegralImage2 image, int x, int y) { double sum = 0.0; if (!Tilted) { // Compute the sum for a standard feature foreach (HaarRectangle rect in Rectangles) { sum += image.GetSum(x + rect.ScaledX, y + rect.ScaledY, rect.ScaledWidth, rect.ScaledHeight) * rect.ScaledWeight; } } else { // Compute the sum for a rotated feature foreach (HaarRectangle rect in Rectangles) { sum += image.GetSumT(x + rect.ScaledX, y + rect.ScaledY, rect.ScaledWidth, rect.ScaledHeight) * rect.ScaledWeight; } } return sum; }