Exemplo n.º 1
0
        public byte[] Denoise(byte[] data, int width, int height, int padding, out int it)
        {
            it          = 0;
            byte[,] arr = Utils.ByteArr1DTo2D(data, width, height);
            bool[,] vi  = (bool[, ])Array.CreateInstance(typeof(bool), height, width);

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    vi[i, j] = false;
                }
            }

            List <Point> points = new List <Point>();
            List <int>   areas  = new List <int>();

            for (int i = padding; i < height - padding; ++i)
            {
                for (int j = padding; j < width - padding; ++j)
                {
                    if (vi[i, j])
                    {
                        continue;
                    }
                    if (arr[i, j] == backgroundColor)
                    {
                        continue;
                    }
                    int sum = 0;
                    dfsSearch(arr, vi, i, j, width, height, padding, ref sum);
                    points.Add(new Point(j, i));
                    areas.Add(sum);
                }
            }

            if (points.Count > 1)
            {
                Vector2[] vs = areas.Select(i => new Vector2(i, 0)).ToArray();
                it = 0;
                KMeansCluster result = KMeansClustering.AnnealCluster(vs, 2, out it);

                int bigcluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                for (int i = 0; i < points.Count; ++i)
                {
                    if (result.ClusterIndex[i] != bigcluster)
                    {
                        //dfsColor(arr, points[i].Y, points[i].X, width, height, 1);
                        dfsClear(arr, points[i].Y, points[i].X, width, height, 1);
                    }
                }
            }
            else
            {
                dfsColor(arr, points[0].Y, points[0].X, width, height, 1);
            }

            byte[] bytes = Utils.ByteArr2DTo1D(arr);
            return(bytes);
        }
Exemplo n.º 2
0
        public Image <Gray, byte> Process(Image <Gray, byte> src)
        {
            Connectivity.Connect8 = this.Connect8;
            var cl = src.connectLevel(1, 8, 0).Domains.ToList();

            var dst = src.Clone();

            if (cl.Count > 1)
            {
                Vector2[] vs = cl.Select(i => new Vector2(i.Area, 0)).ToArray();
                KMeansClustering.MaxIterate = this.MaxIteration;
                var result = KMeansClustering.AnnealCluster(vs, 2);

                int foreCluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                for (int i = 0; i < cl.Count; ++i)
                {
                    if (result.ClusterIndex[i] != foreCluster)
                    {
                        foreach (var p in cl[i].Points)
                        {
                            dst[p] = new Gray(0);
                        }
                    }
                }
            }

            return(dst);
        }
Exemplo n.º 3
0
        public Image <Gray, Byte> Denoise(Image <Gray, Byte> img, int padding, out int it)
        {
            it = 0;
            var output = img.Clone();
            int width = img.Width, height = img.Height;

            if (padding > 0)
            {
                for (int p = 0; p < padding; ++p)
                {
                    for (int i = 0; i < height; ++i)
                    {
                        // 左右两边
                        output[i, 0 + p]         = ColorDefination.BackColor;
                        output[i, width - 1 - p] = ColorDefination.BackColor;
                    }
                    for (int j = 0; j < width; ++j)
                    {
                        // 上下两边
                        output[0 + p, j]          = ColorDefination.BackColor;
                        output[height - 1 - p, j] = ColorDefination.BackColor;
                    }
                }
            } // 清除宽为padding的边框

            bool[,] vi = (bool[, ])Array.CreateInstance(typeof(bool), height, width);
            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    vi[i, j] = false;
                }
            } // 不知道会不会自动初始化成false,如果会,这段可以去掉

            List <Point> points = new List <Point>();
            List <int>   areas  = new List <int>();

            for (int i = padding; i < height - padding; ++i)
            {
                for (int j = padding; j < width - padding; ++j)
                {
                    if (vi[i, j])
                    {
                        continue;
                    }
                    if (img[i, j].Equals(ColorDefination.BackColor))
                    {
                        continue;
                    }
                    //int sum = 0;
                    //dfsSearch(img, vi, i, j, padding, ref sum);
                    Point p   = new Point(j, i);
                    int   sum = bfsSearch(img, vi, p, padding);
                    points.Add(p);
                    areas.Add(sum);
                }
            }

            if (points.Count > 1)
            {
                //var vs = areas.Select(d => (double)d).ToArray();
                //KMeans<double> kmeans = new KMeans<double>(vs, 2,
                //    (a, b) => Math.Abs(a - b),
                //    arr => arr.Average());
                //var result = kmeans.AnnealCluster(
                //    (a, b) => a + b,
                //    (a, b) => a - b,
                //    (a, b) => a / b);
                Vector2[] vs     = areas.Select(i => new Vector2(i, 0)).ToArray();
                var       result = KMeansClustering.AnnealCluster(vs, 2, out it);

                int foreCluster = (result[0].Centroid.ModSqr() > result[1].Centroid.ModSqr()) ? 0 : 1;
                //int foreCluster = (result[0].Center > result[1].Center) ? 0 : 1;
                for (int i = 0; i < points.Count; ++i)
                {
                    if (result.ClusterIndex[i] != foreCluster)
                    {
                        bfsClear(output, points[i], padding);
                    }
                }
            }
            else
            {
                //dfsColor(output, points[0].Y, points[0].X, padding);
            }

            return(output);
        }