C# 获取二值化图的骨架图

   Mat GetSkeletonsPicture(Mat src)//src为二值化图  dst为二值化图的骨架图
        {

            Matrix<Byte> xx = new Matrix<Byte>(src.Rows, src.Cols);
            src.CopyTo(xx);

            int height = xx.Height;
            int width = xx.Width;
            Matrix<float> F = new Matrix<float>(xx.Size);
            Matrix<float> G = new Matrix<float>(xx.Size);
            Matrix<float> M = new Matrix<float>(xx.Size);
            Matrix<float> dst = new Matrix<float>(xx.Size);

            for (int i = 2; i < height - 1; i++)
            {
                for (int j = 2; j < width - 1; j++)
                {
                    if (xx[i, j] == 0)
                    {
                        F[i, j] = 0;
                    }
                    else
                    {
                        float[] mi = new float[] { F[i, j - 1], F[i - 1, j - 1], F[i - 1, j], F[i - 1, j + 1] };
                        F[i, j] = mi.Min() + 1;
                    }
                }
            }
            for (int i = height - 2; i >= 2; i--)
            {
                for (int j = width - 2; j >= 2; j--)
                {
                    if (xx[i, j] == 0)
                    {
                        G[i, j] = 0;
                    }
                    else
                    {
                        float[] mi = new float[] { G[i + 1, j - 1], G[i + 1, j], G[i + 1, j + 1], G[i, j + 1] };
                        G[i, j] = mi.Min() + 1;
                    }
                }
            }
            for (int i = 2; i < height - 1; i++)
            {
                for (int j = 2; j < width - 1; j++)
                {
                    float[] mi = new float[] { F[i, j], G[i, j] };
                    M[i, j] = mi.Min();
                }
            }
            for (int i = 2; i < height - 1; i++)
            {
                for (int j = 2; j < width - 1; j++)
                {
                    float[] ma = new float[] { M[i, j - 1], M[i - 1, j - 1], M[i - 1, j], M[i - 1, j + 1],
                        M[i + 1, j - 1], M[i + 1, j], M[i + 1, j + 1], M[i, j + 1] };
                    if (M[i, j] == ma.Max())
                    {
                        dst[i, j] = M[i, j];
                    }
                    else
                    {
                        dst[i, j] = 0;
                    }
                }
            }
            return dst.Mat;

        }
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页