Central image moments.
Inheritance: IMoments
        /// <summary>
        ///   Computes the center moments for the specified image.
        /// </summary>
        ///
        /// <param name="image">The image whose moments should be computed.</param>
        /// <param name="area">The region of interest in the image to compute moments for.</param>
        ///
        public override void Compute(float[,] image, Rectangle area)
        {
            RawMoments     raw    = new RawMoments(image, area, Order);
            CentralMoments center = new CentralMoments(raw);

            this.Compute(center);
        }
        public void ComputeTest()
        {
            Bitmap image = Resources.hu;

            CentralMoments target = new CentralMoments(image, order: 3);

            Assert.AreEqual(86424.0 / target.Mu00, 1);
            Assert.AreEqual(0, target.Mu01);
            Assert.AreEqual(0, target.Mu10);
            Assert.AreEqual(5.868206472635379E8 / target.Mu02, 1, 1e-2);

            Assert.AreEqual(6348920.945848465 / target.Mu11, 1, 1e-2);
            Assert.AreEqual(9.084235762166061E8 / target.Mu20, 1, 1e-3);

            Assert.AreEqual(-2.155191E9 / target.Mu12, 1, 1e-5);
            Assert.AreEqual(7.125893E8 / target.Mu21, 1, 1e-3);

            Assert.AreEqual(-1.26244547E10 / target.Mu30, 1, 1e-8);
            Assert.AreEqual(1.71818829E9 / target.Mu03, 1, 1e-8);

            SizeF size = target.GetSize();
            float angle = target.GetOrientation();

            Assert.AreEqual(410.207916f, size.Height);
            Assert.AreEqual(329.534637f, size.Width);
            Assert.AreEqual(0.0196384024f, angle);
        }
        /// <summary>
        ///   Computes the Hu moments from the specified central moments.
        /// </summary>
        ///
        /// <param name="moments">The central moments to use as base of calculations.</param>
        ///
        public void Compute(CentralMoments moments)
        {
            double inv    = 1.0 / moments.Mu00;
            double inv2   = 1.0 / (moments.Mu00 * moments.Mu00);
            double inv5d2 = Math.Sqrt(inv2 * inv2 * inv);


            float n20 = (float)(moments.Mu20 * inv2);
            float n02 = (float)(moments.Mu02 * inv2);
            float n11 = (float)(moments.Mu11 * inv2);

            float n21 = (float)(moments.Mu21 * inv5d2);
            float n12 = (float)(moments.Mu12 * inv5d2);
            float n30 = (float)(moments.Mu30 * inv5d2);
            float n03 = (float)(moments.Mu03 * inv5d2);


            //   (η20 + η02)
            I1 = (n20 + n02);


            //   (η20 − η02)²              + 4    η11²
            I2 = (n20 - n02) * (n20 - n02) + 4 * (n11 * n11);


            //   (η30 − 3   η12)²
            I3 = (n30 - 3 * n12) * (n30 - 3 * n12)

                 // + (3   η21 − η03)²
                 + (3 * n21 - n03) * (3 * n21 - n03);


            //   (η30 + η12)²              + (η21 + η03)²
            I4 = (n30 + n12) * (n30 + n12) + (n21 + n03) * (n21 + n03);


            //   (η30 − 3   η12)   (η30 + η12)   [(η30 + η12)²               −3   (η21 + η03)²             ]
            I5 = (n30 - 3 * n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03))

                 //   (3   η21 − η03)   (η21 + η03)   [3   (η30 + η12)²              − (η21 + η03)²             ]
                 + (3 * n21 - n03) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03));


            //   (η20 − η02)   [(η30 + η12)²              − (η21 + η03)²             ]
            I6 = (n20 - n02) * ((n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03))

                 //  + 4   η11   (η30 + η12)   (η21 + η03)
                 + 4 * n11 * (n30 + n12) * (n21 + n03);


            //   (3   η21 − η03)   (η30 + η12)                 [(η30 + η12)²              − 3   (η21 + η03)²             ]
            I7 = (3 * n21 - n03) * (n30 + n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03))

                 // - (η30 − 3   η12)   (η21 + η03)   [3   (η30 + η12)²              − (η21 + η03)²             ]
                 - (n30 - 3 * n12) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03));
        }
        public void ComputeTest2()
        {
            // 0 and 1 are only translated
            var cm0 = new CentralMoments(Resources.hu0, 3);
            var cm1 = new CentralMoments(Resources.hu1, 3);

            Assert.AreEqual(cm0.Mu00, cm1.Mu00);
            Assert.AreEqual(cm0.Mu01, cm1.Mu01);
            Assert.AreEqual(cm0.Mu10, cm1.Mu10);
            Assert.AreNotEqual(cm0.Mu11, cm1.Mu11);
        }
        public void ComputeTest2()
        {
            // 0 and 1 are only translated
            var cm0 = new CentralMoments(Accord.Imaging.Image.Clone(Resources.hu0), 3);
            var cm1 = new CentralMoments(Accord.Imaging.Image.Clone(Resources.hu1), 3);

            Assert.AreEqual(cm0.Mu00, cm1.Mu00);
            Assert.AreEqual(cm0.Mu01, cm1.Mu01);
            Assert.AreEqual(cm0.Mu10, cm1.Mu10);
            Assert.AreNotEqual(cm0.Mu11, cm1.Mu11);
        }
Example #6
0
        /// <summary>
        /// Process a new video frame.
        /// </summary>
        public void ProcessFrame(UnmanagedImage frame)
        {
            filterImage = filter.Apply(frame);

            Blob blob = extractBlob();

            if (blob == null)
            {
                trackingObject.Reset();
                return;
            }


            trackingObject.Rectangle = blob.Rectangle;
            trackingObject.Center = (IntPoint)blob.CenterOfGravity;

            if (rotation)
            {
                // Locate moments
                CentralMoments moments = new CentralMoments();
                moments.Compute(filterImage, blob.Rectangle);
                trackingObject.Angle = moments.GetOrientation();
            }


            if (extract)
            {
                blobCounter.ExtractBlobsImage(filterImage, blob, false);
                trackingObject.Image = blob.Image;
            }
        }
Example #7
0
 /// <summary>
 ///   Computes the center moments for the specified image.
 /// </summary>
 /// 
 /// <param name="image">The image whose moments should be computed.</param>
 /// <param name="area">The region of interest in the image to compute moments for.</param>
 /// 
 public override void Compute(float[,] image, Rectangle area)
 {
     RawMoments raw = new RawMoments(image, area, Order);
     CentralMoments center = new CentralMoments(raw);
     this.Compute(center);
 }
Example #8
0
        /// <summary>
        ///   Computes the Hu moments from the specified central moments.
        /// </summary>
        /// 
        /// <param name="moments">The central moments to use as base of calculations.</param>
        /// 
        public void Compute(CentralMoments moments)
        {
            double inv = 1.0 / moments.Mu00;
            double inv2 = 1.0 / (moments.Mu00 * moments.Mu00);
            double inv5d2 = Math.Sqrt(inv2 * inv2 * inv);


            float n20 = (float)(moments.Mu20 * inv2);
            float n02 = (float)(moments.Mu02 * inv2);
            float n11 = (float)(moments.Mu11 * inv2);

            float n21 = (float)(moments.Mu21 * inv5d2);
            float n12 = (float)(moments.Mu12 * inv5d2);
            float n30 = (float)(moments.Mu30 * inv5d2);
            float n03 = (float)(moments.Mu03 * inv5d2);


            //   (η20 + η02)
            I1 = (n20 + n02);


            //   (η20 − η02)²              + 4    η11²
            I2 = (n20 - n02) * (n20 - n02) + 4 * (n11 * n11);


            //   (η30 − 3   η12)²
            I3 = (n30 - 3 * n12) * (n30 - 3 * n12)

            // + (3   η21 − η03)²
               + (3 * n21 - n03) * (3 * n21 - n03);


            //   (η30 + η12)²              + (η21 + η03)²
            I4 = (n30 + n12) * (n30 + n12) + (n21 + n03) * (n21 + n03);


            //   (η30 − 3   η12)   (η30 + η12)   [(η30 + η12)²               −3   (η21 + η03)²             ]
            I5 = (n30 - 3 * n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03))

            //   (3   η21 − η03)   (η21 + η03)   [3   (η30 + η12)²              − (η21 + η03)²             ]
               + (3 * n21 - n03) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03));


            //   (η20 − η02)   [(η30 + η12)²              − (η21 + η03)²             ]
            I6 = (n20 - n02) * ((n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03))

            //  + 4   η11   (η30 + η12)   (η21 + η03)
                + 4 * n11 * (n30 + n12) * (n21 + n03);


            //   (3   η21 − η03)   (η30 + η12)                 [(η30 + η12)²              − 3   (η21 + η03)²             ]
            I7 = (3 * n21 - n03) * (n30 + n12) * (n30 + n12) * ((n30 + n12) * (n30 + n12) - 3 * (n21 + n03) * (n21 + n03))

            // - (η30 − 3   η12)   (η21 + η03)   [3   (η30 + η12)²              − (η21 + η03)²             ]
               - (n30 - 3 * n12) * (n21 + n03) * (3 * (n30 + n12) * (n30 + n12) - (n21 + n03) * (n21 + n03));
        }