Ejemplo n.º 1
0
        public void meanshift_new_method_no_ctor_args()
        {
            #region doc_sample1
            // Use a fixed seed for reproducibility
            Accord.Math.Random.Generator.Seed = 0;

            // Declare some data to be clustered
            double[][] input =
            {
                new double[] { -5, -2, -4 },
                new double[] { -5, -5, -6 },
                new double[] {  2,  1,  1 },
                new double[] {  1,  1,  2 },
                new double[] {  1,  2,  2 },
                new double[] {  3,  1,  2 },
                new double[] { 11,  5,  4 },
                new double[] { 15,  5,  6 },
                new double[] { 10,  5,  6 },
            };

            // Create a new Mean-Shift algorithm for 3 dimensional samples
            MeanShift meanShift = new MeanShift()
            {
                // Use a uniform kernel density
                Kernel    = new UniformKernel(),
                Bandwidth = 2.0
            };

            // Learn a data partitioning using the Mean Shift algorithm
            MeanShiftClusterCollection clustering = meanShift.Learn(input);

            // Predict group labels for each point
            int[] labels = clustering.Decide(input);

            // As a result, the first two observations should belong to the
            //  same cluster (thus having the same label). The same should
            //  happen to the next four observations and to the last three.
            #endregion

            Assert.AreEqual(labels[0], labels[1]);

            Assert.AreEqual(labels[2], labels[3]);
            Assert.AreEqual(labels[2], labels[4]);
            Assert.AreEqual(labels[2], labels[5]);

            Assert.AreEqual(labels[6], labels[7]);
            Assert.AreEqual(labels[6], labels[8]);

            Assert.AreNotEqual(labels[0], labels[2]);
            Assert.AreNotEqual(labels[2], labels[6]);
            Assert.AreNotEqual(labels[0], labels[6]);


            int[] labels2 = meanShift.Clusters.Decide(input);
            Assert.IsTrue(labels.IsEqual(labels2));

            Assert.AreEqual(3 / 9.0, meanShift.Clusters.Proportions[labels[6]], 1e-6);
            Assert.AreEqual(2 / 9.0, meanShift.Clusters.Proportions[labels[0]], 1e-6);
            Assert.AreEqual(4 / 9.0, meanShift.Clusters.Proportions[labels[2]], 1e-6);
        }
Ejemplo n.º 2
0
        public static List <List <Vector2> > ClusterPoints(List <Vector2> points)//delete "outliers"
        {
            double[][] input = new double[points.Count][];
            for (int i = 0; i < points.Count; i++)
            {
                input[i] = new double[] { points[i].x, points[i].y };
            }
            UniformKernel kernel    = new UniformKernel();
            MeanShift     meanShift = new MeanShift(dimension: 2, kernel: kernel, bandwidth: 1e-2);
            MeanShiftClusterCollection clustering = meanShift.Learn(input);

            int[] labels = clustering.Decide(input);

            List <List <Vector2> > classedPoints = new List <List <Vector2> >();

            for (int i = 0; i <= Mathf.Max(labels); i++)
            {
                List <Vector2> iClass = new List <Vector2>();
                foreach (var p in points)
                {
                    iClass.Add(p);
                }
                classedPoints.Add(iClass);
            }
            return(classedPoints);
        }
Ejemplo n.º 3
0
        public void RunProcess(double[][] inputDataMS, bool displayResult = false)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();

            MeanShift clusterMS = new MeanShift(dataDimension, new UniformKernel(), msSearchRadius);

            clusterMS.Distance = new myDistanceClass();

            MeanShiftClusterCollection clustering = clusterMS.Learn(inputDataMS);

            pointLabels = clustering.Decide(inputDataMS);

            clusteringPlaneRec = new List <pointPlaneClass>();
            for (int i = 0; i < clustering.Count; i++)
            {
                clusteringPlaneRec.Add(new pointPlaneClass(i, 0));
            }
            for (int i = 0; i < h * w; i++)
            {
                MyVector3 vector3T = new MyVector3(inputDataMS[i][6], inputDataMS[i][7], inputDataMS[i][8]);
                if (vector3T.x == 0 && vector3T.y == 0 && vector3T.z == 0)
                {
                    continue;
                }

                int idx = pointLabels[i];
                clusteringPlaneRec[idx].Points.Add(vector3T);
                clusteringPlaneRec[idx].PointsIdx.Add(i);
                clusteringPlaneRec[idx].Value++;
            }
            clusteringPlaneRec.Sort((x, y) => y.Value.CompareTo(x.Value));
            #region visualization
            if (displayResult)
            {
                int loop = 0;
                Image <Bgr, byte> image2 = new Image <Bgr, byte>(w, h);
                image2.SetZero();
                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        if (pointLabels[loop] >= 0)
                        {
                            byte r = (byte)(Utils.ColorMall[pointLabels[loop] % 30].R);
                            byte g = (byte)(Utils.ColorMall[pointLabels[loop] % 30].G);
                            byte b = (byte)(Utils.ColorMall[pointLabels[loop] % 30].B);
                            image2[i, j] = new Bgr(b, g, r);
                        }
                        loop++;
                    }
                }
                new ImageViewer(image2, "2 - MeanShiftClustering").Show();
            }
            #endregion
            sw.Stop();
            Console.WriteLine(clusteringPlaneRec.Count + " labels\tin" + sw.ElapsedMilliseconds / 1000 + "s");
            sw.Restart();

            // extract planes from clustered data
            SceondPlaneExtraction();
            #region visualization
            if (displayResult)
            {
                int loop = 0;
                Image <Bgr, byte> image3 = new Image <Bgr, byte>(w, h);
                image3.SetZero();
                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        if (pointLabels[loop] >= 0)
                        {
                            byte r = (byte)(Utils.ColorMall[pointLabels[loop] % 30].R);
                            byte g = (byte)(Utils.ColorMall[pointLabels[loop] % 30].G);
                            byte b = (byte)(Utils.ColorMall[pointLabels[loop] % 30].B);
                            image3[i, j] = new Bgr(b, g, r);
                        }
                        loop++;
                    }
                }
                new ImageViewer(image3, "3 - PlaneExtraction").Show();
            }
            #endregion
            sw.Stop();
            Console.WriteLine(extractionPlaneRec.Count + " labels\tin" + sw.ElapsedMilliseconds / 1000 + "s");
            sw.Restart();

            // merge planes if necessary
            MergePlanes();
            #region visualization
            if (displayResult)
            {
                int loop = 0;
                Image <Bgr, byte> image4 = new Image <Bgr, byte>(w, h);
                image4.SetZero();
                for (int i = 0; i < h; i++)
                {
                    for (int j = 0; j < w; j++)
                    {
                        if (pointLabels[loop] >= 0)
                        {
                            byte r = (byte)(Utils.ColorMall[pointLabels[loop] % 30].R);
                            byte g = (byte)(Utils.ColorMall[pointLabels[loop] % 30].G);
                            byte b = (byte)(Utils.ColorMall[pointLabels[loop] % 30].B);
                            image4[i, j] = new Bgr(b, g, r);
                        }
                        loop++;
                    }
                }
                new ImageViewer(image4, "4 - MergedPlanes").Show();
            }
            #endregion

            sw.Stop();
            Console.WriteLine(mergedPlaneRec.Count + " labels\tin" + sw.ElapsedMilliseconds / 1000 + "s");
        }
    private List <Line2> Skeletonize(out bool iscurve)
    {
        Image <Gray, byte> img2   = body_img.Copy();
        Image <Gray, byte> eroded = new Image <Gray, byte>(img2.Size);
        Image <Gray, byte> temp   = new Image <Gray, byte>(img2.Size);
        Image <Gray, byte> skel   = new Image <Gray, byte>(img2.Size);

        body_img.Save("test.png");


        #region with matlab
        string argument1 = "\"" + "test.png" + "\"";
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.FileName               = System.Environment.CurrentDirectory + "\\Assets\\frommatlab\\skeleton.exe";
        process.StartInfo.Arguments              = argument1;
        process.StartInfo.UseShellExecute        = false;
        process.StartInfo.CreateNoWindow         = true;
        process.StartInfo.RedirectStandardOutput = true;
        //启动
        process.Start();
        process.WaitForExit();
        #endregion

        skel          = new Image <Gray, byte>("prune.png");
        ori_thin_img  = new Image <Gray, byte>("thin.png");
        ori_prune_img = skel;

        #region thining - comment
        //skel.SetValue(0);
        //CvInvoke.Threshold(img2, temp, 127, 256, 0);
        //var element = CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(-1, -1));
        //bool done = false;

        ////skeleton
        //int itr = 0;
        //while (!done)
        //{
        //    CvInvoke.Erode(img2, eroded, element, new Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
        //    CvInvoke.Dilate(eroded, temp, element, new Point(-1, -1), 1, BorderType.Reflect, default(MCvScalar));
        //    CvInvoke.Subtract(img2, temp, temp);
        //    CvInvoke.BitwiseOr(skel, temp, skel);
        //    eroded.CopyTo(img2);
        //    itr++;
        //    if (CvInvoke.CountNonZero(img2) == 0) done = true;
        //}
        //Image<Gray, Byte> cannyimg = body_img.Canny(60, 100);
        //CvInvoke.Dilate(cannyimg, cannyimg, element, new Point(-1, -1), 3, BorderType.Reflect, default(MCvScalar));
        //CvInvoke.Subtract(skel, cannyimg, skel);
        //ori_skel_img = skel.Copy();

        ////thinning
        //if (!noface)
        //{
        //    #region thinning
        //    List<Mat> cs = new List<Mat>();
        //    List<Mat> ds = new List<Mat>();
        //    for (int i = 0; i < 8; i++)
        //    {
        //        cs.Add(CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(-1, -1)));
        //        ds.Add(CvInvoke.GetStructuringElement(ElementShape.Cross, new Size(3, 3), new Point(-1, -1)));
        //    }

        //    cs[0].SetTo(new int[] { 0, 0, 0, 0, 1, 0, 1, 1, 1 });
        //    cs[1].SetTo(new int[] { 1, 0, 0, 1, 1, 0, 1, 0, 0 });
        //    cs[2].SetTo(new int[] { 1, 1, 1, 0, 1, 0, 0, 0, 0 });
        //    cs[3].SetTo(new int[] { 0, 0, 1, 0, 1, 1, 0, 0, 1 });

        //    ds[0].SetTo(new int[] { 1, 1, 1, 0, 0, 0, 0, 0, 0 });
        //    ds[1].SetTo(new int[] { 0, 0, 1, 0, 0, 1, 0, 0, 1 });
        //    ds[2].SetTo(new int[] { 0, 0, 0, 0, 0, 0, 1, 1, 1 });
        //    ds[3].SetTo(new int[] { 1, 0, 0, 1, 0, 0, 1, 0, 0 });

        //    cs[4].SetTo(new int[] { 0, 0, 0, 1, 1, 0, 1, 1, 0 });
        //    cs[5].SetTo(new int[] { 1, 1, 0, 1, 1, 0, 0, 0, 0 });
        //    cs[6].SetTo(new int[] { 0, 1, 1, 0, 1, 1, 0, 0, 0 });
        //    cs[7].SetTo(new int[] { 0, 0, 0, 0, 1, 1, 0, 1, 1 });

        //    ds[4].SetTo(new int[] { 0, 1, 1, 0, 0, 1, 0, 0, 0 });
        //    ds[5].SetTo(new int[] { 0, 0, 0, 0, 0, 1, 0, 1, 1 });
        //    ds[6].SetTo(new int[] { 0, 0, 0, 1, 0, 0, 1, 1, 0 });
        //    ds[7].SetTo(new int[] { 1, 1, 0, 1, 0, 0, 0, 0, 0 });

        //    Image<Gray, byte> img3 = skel.Copy();
        //    Image<Gray, byte> temp2 = skel.CopyBlank();
        //    Image<Gray, byte> lastimg3 = skel.Copy();

        //    done = false;
        //    while (!done)
        //    {
        //        for (int i = 0; i < 8; i++)
        //        {
        //            temp = this.HitOrMiss(img3, cs[i], ds[i]);
        //            CvInvoke.Subtract(img3, temp, img3);
        //        }

        //        CvInvoke.Subtract(lastimg3, img3, temp2);
        //        lastimg3 = img3.Copy();
        //        if (CvInvoke.CountNonZero(temp2) == 0) done = true;
        //    }

        //    //img3.Save("thining.png");
        //    #endregion
        //    skel = img3.Copy();
        //    ori_thinning_img = img3.Copy();
        //}
        ////// remove noise
        ////for (int i = 0; i < img3.Height; i++)
        ////{
        ////    for (int j = 0; j < img3.Width; j++)
        ////    {
        ////        if (img3[i, j].Equals(new Gray(255)))
        ////        {
        ////            bool change = false;
        ////            for (int pad = 1; pad < 3; pad++)
        ////            {
        ////                if (i >= pad && i < img3.Height - pad && j >= pad && j < img3.Width - pad)
        ////                {
        ////                    if (img3[i - pad, j].Equals(new Gray(0)) &&
        ////                        img3[i - pad, j - pad].Equals(new Gray(0)) &&
        ////                        img3[i - pad, j + pad].Equals(new Gray(0)) &&
        ////                        img3[i + pad, j].Equals(new Gray(0)) &&
        ////                        img3[i + pad, j - pad].Equals(new Gray(0)) &&
        ////                        img3[i + pad, j + pad].Equals(new Gray(0)) &&
        ////                        img3[i, j - pad].Equals(new Gray(0)) &&
        ////                        img3[i, j + pad].Equals(new Gray(0)))
        ////                        change = true;
        ////                }
        ////            }
        ////            if (change)
        ////                img3[i, j] = new Gray(0);
        ////        }
        ////    }
        ////}
        ////img3.Save("thiningdenoise.png");
        #endregion

        // get line
        // consider both straight line and curve
        LineSegment2D[] lines = skel.HoughLinesBinary(
            1,               //Distance resolution in pixel-related units
            Math.PI / 180.0, //Angle resolution measured in radians.
            3,               //threshold
            4,               //min Line width
            1                //gap between lines
            )[0];            //Get the lines from the first channel

        Image <Gray, byte> lineimg    = skel.CopyBlank();
        List <Line2>       skel_lines = new List <Line2>();
        foreach (LineSegment2D line in lines)
        {
            //remove image boundaries
            //if (line.P1.X > 10 && line.P1.Y > 10 && line.P1.X < body_img.Height - 10 && line.P1.Y < body_img.Width &&
            //   line.P2.X > 10 && line.P2.Y > 10 && line.P2.X < body_img.Height - 10 && line.P2.Y < body_img.Width - 10)
            //{
            skel_lines.Add(new Line2(new Vector2(line.P1.X, line.P1.Y), new Vector2(line.P2.X, line.P2.Y)));
            lineimg.Draw(line, new Gray(255), 2);
            //}
        }
        if (debug)
        {
            lineimg.Save("skel-line.png");
        }


        // cluster according to direction and relative distance
        // too many cluster means curve axis
        IMGSIZE = Math.Min(body_img.Width, body_img.Height);
        if (skel_lines.Count > 0)
        {
            double[][] xy = new double[skel_lines.Count][];
            for (int i = 0; i < skel_lines.Count; i++)
            {
                xy[i] = new double[] { skel_lines[i].start.x, skel_lines[i].start.y,
                                       skel_lines[i].end.x, skel_lines[i].end.y };
            }

            MeanShift clusterMS = new MeanShift(4, new UniformKernel(), 0.02);
            clusterMS.Distance = new myDistanceClass();
            MeanShiftClusterCollection clustering = clusterMS.Learn(xy);
            var lineLabels   = clustering.Decide(xy);
            int clustercount = lineLabels.DistinctCount();
            //Debug.Log("cluster count: " + clustercount);

            if (debug)
            {
                Image <Rgb, byte> lineimg_rgb = lineimg.Convert <Rgb, byte>();
                System.Random     rnd         = new System.Random();
                Rgb[]             colortable  = new Rgb[clustering.Count];
                for (int i = 0; i < clustering.Count; i++)
                {
                    colortable[i] = new Rgb(rnd.Next(255), rnd.Next(255), rnd.Next(255));
                }

                for (int i = 0; i < skel_lines.Count; i++)
                {
                    int label = lineLabels[i];
                    lineimg_rgb.Draw(skel_lines[i].ToLineSegment2D(), colortable[label], 2);
                }
                lineimg_rgb.Save("skel-line-cluster.png");
            }


            if (noface)
            {
                thred = 2;   // 2
            }
            if (clustercount > thred)
            {
                iscurve = true;
            }
            else
            {
                iscurve = false;
            }
        }
        else
        {
            iscurve = false;
            NumericalRecipes.RansacLine2d rcl = new NumericalRecipes.RansacLine2d();
            List <Vector2> linepoints         = new List <Vector2>();
            linepoints = IExtension.GetMaskPoints(skel);
            Line2 bestline = rcl.Estimate(linepoints);
            skel_lines.Add(bestline);
        }

        return(skel_lines);
    }