Raw 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);
        }
Beispiel #2
0
        /// <summary>
        ///   Computes the center moments for the specified image.
        /// </summary>
        ///
        /// <param name="image">The image.</param>
        /// <param name="area">The region of interest in the image to compute moments for.</param>
        ///
        public unsafe void Compute(float[,] image, Rectangle area)
        {
            RawMoments raw = new RawMoments();

            raw.Compute(image, area, true);
            this.Compute(raw);
        }
Beispiel #3
0
        /// <summary>
        ///   Computes the center moments for the specified image.
        /// </summary>
        ///
        /// <param name="image">The image.</param>
        /// <param name="area">The region of interest in the image to compute moments for.</param>
        ///
        public unsafe void Compute(UnmanagedImage image, Rectangle area)
        {
            RawMoments raw = new RawMoments();

            raw.Compute(image, area, true);
            this.Compute(raw);
        }
Beispiel #4
0
        public void RawMomentsConstructorTest2()
        {
            var hu = Accord.Imaging.Image.Clone(Resources.hu);

            float[,] image;
            
            new ImageToMatrix()
            {
                Min = 0,
                Max = 255
            }.Convert(hu, out image);

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

            Assert.AreEqual(86424.0 / target.M00, 1);
            Assert.AreEqual(1.177254E+7 / target.M01, 1, 1e-10);
            Assert.AreEqual(2.19032653E+9 / target.M02, 1, 1e-8);

            Assert.AreEqual(1.5264756E7 / target.M10, 1, 1e-10);
            Assert.AreEqual(2.08566813E9 / target.M11, 1, 1e-6);
            Assert.AreEqual(3.8643911413E11 / target.M12, 1, 1e-5);
            Assert.AreEqual(3.604560164E9 / target.M20, 1, 1e-3);
            Assert.AreEqual(4.9401212329E11 / target.M21, 1, 1e-3);

            Assert.AreEqual(9.451364E+11 / target.M30, 1, 1e-8);
            Assert.AreEqual(4.599169E+11 / target.M03, 1, 1e-8);
        }
Beispiel #5
0
        /// <summary>
        ///   Computes the center moments from the specified raw moments.
        /// </summary>
        ///
        /// <param name="moments">The raw moments to use as base of calculations.</param>
        ///
        public void Compute(RawMoments moments)
        {
            Mu00 = moments.M00;
            Mu01 = Mu10 = 0;

            Mu20 = moments.M20 - moments.M10 * moments.CenterX;
            Mu02 = moments.M02 - moments.M01 * moments.CenterY;
            Mu11 = moments.M11 - moments.M01 * moments.CenterX;

            invM00 = moments.InvM00;
        }
Beispiel #6
0
        public void RawMomentsConstructorTest()
        {
            Bitmap image = Resources.hu;

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

            Assert.AreEqual(86424.0 / target.M00, 1);
            Assert.AreEqual(1.177254E+7 / target.M01, 1, 1e-10);
            Assert.AreEqual(2.19032653E+9 / target.M02, 1, 1e-8);

            Assert.AreEqual(1.5264756E7 / target.M10, 1, 1e-10);
            Assert.AreEqual(2.08566813E9 / target.M11, 1, 1e-6);
            Assert.AreEqual(3.8643911413E11 / target.M12, 1, 1e-5);
            Assert.AreEqual(3.604560164E9 / target.M20, 1, 1e-3);
            Assert.AreEqual(4.9401212329E11 / target.M21, 1, 1e-3);

            Assert.AreEqual(9.451364E+11 / target.M30, 1, 1e-8);
            Assert.AreEqual(4.599169E+11 / target.M03, 1, 1e-8);
        }
Beispiel #7
0
        /// <summary>
        ///   Computes the center moments from the specified raw moments.
        /// </summary>
        ///
        /// <param name="moments">The raw moments to use as base of calculations.</param>
        ///
        public void Compute(RawMoments moments)
        {
            float x = moments.CenterX;
            float y = moments.CenterY;

            Mu00 = moments.M00;

            Mu01 = Mu10 = 0;
            Mu11 = moments.M11 - moments.M01 * x;

            Mu20 = moments.M20 - moments.M10 * x;
            Mu02 = moments.M02 - moments.M01 * y;

            Mu21 = moments.M21 - 2 * x * moments.M11 - y * moments.M20 + 2 * x * x * moments.M01;
            Mu12 = moments.M12 - 2 * y * moments.M11 - x * moments.M02 + 2 * y * y * moments.M10;

            Mu30 = moments.M30 - 3 * x * moments.M20 + 2 * x * x * moments.M10;
            Mu03 = moments.M03 - 3 * y * moments.M02 + 2 * y * y * moments.M01;

            invM00 = moments.InvM00;
        }
Beispiel #8
0
 /// <summary>
 ///   Computes the center moments for the specified image.
 /// </summary>
 /// 
 /// <param name="image">The image.</param>
 /// <param name="area">The region of interest in the image to compute moments for.</param>
 /// 
 public unsafe void Compute(float[,] image, Rectangle area)
 {
     RawMoments raw = new RawMoments();
     raw.Compute(image, area, true);
     this.Compute(raw);
 }
Beispiel #9
0
        /// <summary>
        ///   Computes the center moments from the specified raw moments.
        /// </summary>
        /// 
        /// <param name="moments">The raw moments to use as base of calculations.</param>
        /// 
        public void Compute(RawMoments moments)
        {
            Mu00 = moments.M00;
            Mu01 = Mu10 = 0;

            Mu20 = moments.M20 - moments.M10 * moments.CenterX;
            Mu02 = moments.M02 - moments.M01 * moments.CenterY;
            Mu11 = moments.M11 - moments.M01 * moments.CenterX;

            invM00 = moments.InvM00;
        }
Beispiel #10
0
        /// <summary>
        ///   Mean shift algorithm
        /// </summary>
        /// 
        private CentralMoments meanShift(UnmanagedImage frame)
        {
            // Variable initialization
            int width = frame.Width;
            int height = frame.Height;

            // (assume all frames have equal dimensions)
            if (map == null) map = new float[height, width];


            // Grab the current region of interest in the current frame
            Rectangle roi = conservative ? searchWindow : new Rectangle(0, 0, frame.Width, frame.Height);

            // Compute the histogram for the current frame
            float[] currentHistogram = createHistogram(frame, roi);

            // Use the previous histogram to compute a ratio histogram (storing in current)
            computeHistogramRatio(originalHistogram, currentHistogram, currentHistogram);

            // Compute the back-projection map using the ratio histogram
            lock (sync) generateBackprojectionMap(frame, currentHistogram);


            RawMoments moments = new RawMoments(1);

            // Mean shift with fixed number of iterations
            for (int i = 0; i < MAX_ITERATIONS - 1; i++)
            {
                // Locate first order moments
                moments.Compute(map, searchWindow);

                // Shift the mean (centroid)
                searchWindow.X += (int)(moments.CenterX - searchWindow.Width / 2f);
                searchWindow.Y += (int)(moments.CenterY - searchWindow.Height / 2f);
            }

            // Locate second order moments and perform final shift
            moments.Order = 2; moments.Compute(map, searchWindow);
            searchWindow.X += (int)(moments.CenterX - searchWindow.Width / 2f);
            searchWindow.Y += (int)(moments.CenterY - searchWindow.Height / 2f);

            // Keep the search window inside the image
            searchWindow.X = Math.Max(0, Math.Min(searchWindow.X, width));
            searchWindow.Y = Math.Max(0, Math.Min(searchWindow.Y, height));

            return new CentralMoments(moments); // moments to be used by Camshift
        }
Beispiel #11
0
        /// <summary>
        ///   Computes the center moments from the specified raw moments.
        /// </summary>
        /// 
        /// <param name="moments">The raw moments to use as base of calculations.</param>
        /// 
        public void Compute(RawMoments moments)
        {
            float x = moments.CenterX;
            float y = moments.CenterY;

            Mu00 = moments.M00;

            Mu01 = Mu10 = 0;
            Mu11 = moments.M11 - moments.M01 * x;

            Mu20 = moments.M20 - moments.M10 * x;
            Mu02 = moments.M02 - moments.M01 * y;

            Mu21 = moments.M21 - 2 * x * moments.M11 - y * moments.M20 + 2 * x * x * moments.M01;
            Mu12 = moments.M12 - 2 * y * moments.M11 - x * moments.M02 + 2 * y * y * moments.M10;

            Mu30 = moments.M30 - 3 * x * moments.M20 + 2 * x * x * moments.M10;
            Mu03 = moments.M03 - 3 * y * moments.M02 + 2 * y * y * moments.M01;

            invM00 = moments.InvM00;
        }
Beispiel #12
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="CentralMoments"/> class.
 /// </summary>
 /// 
 /// <param name="moments">The raw moments to construct central moments.</param>
 /// 
 public CentralMoments(RawMoments moments)
     : base(moments.Order)
 {
     Compute(moments);
 }
Beispiel #13
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);
 }
Beispiel #14
0
        public void RawMomentsConstructorTest3()
        {
            float[,] img = 
            {
                { 1, 2, 3, 4, 5 },
                { 6, 7, 8, 9, 10 },
                { 11, 12, 13, 14, 15 },
                { 16, 17, 18, 19, 20 },
            };

            double sum = img.Sum().Sum();

            RawMoments raw = new RawMoments(img);

            Assert.AreEqual(sum, raw.M00);
        }
Beispiel #15
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="CentralMoments"/> class.
 /// </summary>
 ///
 /// <param name="moments">The raw moments to construct central moments.</param>
 ///
 public CentralMoments(RawMoments moments)
     : base(moments.Order)
 {
     Compute(moments);
 }
Beispiel #16
0
 /// <summary>
 ///   Computes the center moments for the specified image.
 /// </summary>
 /// 
 /// <param name="image">The image.</param>
 /// <param name="area">The region of interest in the image to compute moments for.</param>
 /// 
 public unsafe void Compute(UnmanagedImage image, Rectangle area)
 {
     RawMoments raw = new RawMoments();
     raw.Compute(image, area, true);
     this.Compute(raw);
 }
Beispiel #17
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="CentralMoments"/> class.
 /// </summary>
 /// 
 /// <param name="moments">The raw moments to construct central moments.</param>
 /// 
 public CentralMoments(RawMoments moments)
 {
     Compute(moments);
 }
Beispiel #18
0
 /// <summary>
 ///   Initializes a new instance of the <see cref="CentralMoments"/> class.
 /// </summary>
 ///
 /// <param name="moments">The raw moments to construct central moments.</param>
 ///
 public CentralMoments(RawMoments moments)
 {
     Compute(moments);
 }