Exemplo n.º 1
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);
        }
        internal override void Compute(IImage image)
        {
            RawMoments raw = new RawMoments(Order);

            raw.Compute(image);

            CentralMoments center = new CentralMoments(raw);

            this.Compute(center);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Computes moments for the provided image.
        /// </summary>
        /// <param name="image">Image.</param>
        /// <param name="area">Area</param>
        public void Compute(Gray <float>[,] image, Rectangle area)
        {
            RawMoments raw = new RawMoments(Order);

            raw.Compute(image, area);

            CentralMoments center = new CentralMoments(raw);

            this.Compute(center);
        }
Exemplo n.º 4
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
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        private static Rectangle process(Gray <byte>[,] probabilityMap, Rectangle roi, TermCriteria termCriteria, out CentralMoments centralMoments)
        {
            Rectangle imageArea = new Rectangle(0, 0, probabilityMap.Width(), probabilityMap.Height());

            Rectangle  searchWindow = roi;
            RawMoments moments      = new RawMoments(order: 1);

            // Mean shift with fixed number of iterations
            int    i     = 0;
            double shift = Byte.MaxValue;

            while (termCriteria.ShouldTerminate(i, shift) == false && !searchWindow.IsEmptyArea())
            {
                // Locate first order moments
                moments.Compute(probabilityMap, searchWindow);

                int shiftX = (int)(moments.CenterX - searchWindow.Width / 2f);
                int shiftY = (int)(moments.CenterY - searchWindow.Height / 2f);

                // Shift the mean (centroid)
                searchWindow.X += shiftX;
                searchWindow.Y += shiftY;

                // Keep the search window inside the image
                searchWindow.Intersect(imageArea);

                shift = System.Math.Abs((double)shiftX) + System.Math.Abs((double)shiftY); //for term criteria only
                i++;
            }

            if (searchWindow.IsEmptyArea() == false)
            {
                // Locate second order moments and perform final shift
                moments.Order = 2;
                moments.Compute(probabilityMap, 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.Intersect(imageArea);
            }

            centralMoments = new CentralMoments(moments); // moments to be used by camshift
            return(searchWindow);
        }
Exemplo n.º 7
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);
        }
Exemplo n.º 8
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 static CentralMoments Calculate(RawMoments moments)
        {
            float x = moments.CenterX;
            float y = moments.CenterY;

            return(new()
            {
                Mu00 = moments.M00,
                Mu10 = 0,
                Mu01 = 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
            });
        }
        internal override void Compute(IImage image)
        {
            RawMoments raw = new RawMoments(Order);
            raw.Compute(image);

            CentralMoments center = new CentralMoments(raw);
            this.Compute(center);
        }