Exemplo n.º 1
0
        /// <summary>
        /// Y方向切割图片
        /// </summary>
        /// <param name="x"></param>
        /// <param name="s"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public static EffImage CutV(EffImage x, int s, int e)
        {
            if (e - s <= 0)
            {
                return(null);
            }
            if (s > 1)
            {
                s -= 1;
            }
            if (e < x.Height - 1)
            {
                e += 1;
            }


            EffImage rt = White(x.Width, e - s);

            for (int j = s; j < e; j++)
            {
                for (int i = 0; i < x.Width; i++)
                {
                    rt.Set(i, j - s, x.At(i, j));
                }
            }
            return(rt);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 切割图片 X坐标
        /// </summary>
        /// <param name="x">图片</param>
        /// <param name="s">X开始坐标</param>
        /// <param name="e">X结束坐标</param>
        /// <returns></returns>
        public static EffImage CutH(EffImage x, int s, int e)
        {
            if (e - s <= 0)
            {
                return(null);
            }
            if (s > 1)
            {
                s -= 1;
            }
            if (e < x.Width - 1)
            {
                e += 1;
            }

            EffImage rt = White(e - s, x.Height);

            for (int i = s; i < e; i++)
            {
                for (int j = 0; j < x.Height; j++)
                {
                    rt.Set(i - s, j, x.At(i, j));
                }
            }
            return(rt);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 某论文上的细化算法OpenCV改EffImage版
        /// </summary>
        /// <param name="iter"></param>
        private static void thiniter(byte iter, EffImage img)
        {
            int convertColor(Color c)
            {
                return(c.R < 100 ? 1 : 0);
            }

            for (int i = 1; i < img.Width - 1; i++)
            {
                for (int j = 1; j < img.Height - 1; j++)
                {
                    int p2 = convertColor(img.At(i - 1, j));
                    int p3 = convertColor(img.At(i - 1, j + 1));
                    int p4 = convertColor(img.At(i, j + 1));
                    int p5 = convertColor(img.At(i + 1, j + 1));
                    int p6 = convertColor(img.At(i + 1, j));
                    int p7 = convertColor(img.At(i + 1, j - 1));
                    int p8 = convertColor(img.At(i, j - 1));
                    int p9 = convertColor(img.At(i - 1, j - 1));

                    int A = Convert.ToInt32((p2 == 0 && p3 == 1)) + Convert.ToInt32((p3 == 0 && p4 == 1)) +
                            Convert.ToInt32((p4 == 0 && p5 == 1)) + Convert.ToInt32((p5 == 0 && p6 == 1)) +
                            Convert.ToInt32((p6 == 0 && p7 == 1)) + Convert.ToInt32((p7 == 0 && p8 == 1)) +
                            Convert.ToInt32((p8 == 0 && p9 == 1)) + Convert.ToInt32((p9 == 0 && p2 == 1));
                    int B  = p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9;
                    int m1 = iter == 0 ?
                             (p2 * p4 * p6) :
                             (p2 * p4 * p8);
                    int m2 = iter == 0 ?
                             (p4 * p6 * p8) :
                             (p2 * p6 * p8);

                    if (A == 1 && (B >= 2 && B <= 6) && m1 == 0 && m2 == 0)
                    {
                        img.Set(i, j, Color.White);
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///  去掉杂点(适合杂点/杂线粗为1)
        ///  NOTE: 没什么用
        /// </summary>
        public static void ClearNoise(this EffImage img, int MaxNearPoints)
        {
            Color piexl;
            int   nearDots    = 0;
            int   dgGrayValue = 50;

            //     int XSpan, YSpan, tmpX, tmpY;
            //逐点判断
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    piexl = img.At(i, j);
                    if (piexl.R < dgGrayValue)
                    {
                        nearDots = 0;
                        //判断周围8个点是否全为空
                        if (i == 0 || i == img.Width - 1 || j == 0 || j == img.Height - 1)  //边框全去掉
                        {
                            img.Set(i, j, Color.FromArgb(255, 255, 255));
                        }
                        else
                        {
                            if (img.At(i - 1, j - 1).R < dgGrayValue)
                            {
                                nearDots++;
                            }
                            if (img.At(i, j - 1).R < dgGrayValue)
                            {
                                nearDots += 2;
                            }

                            if (img.At(i + 1, j - 1).R < dgGrayValue)
                            {
                                nearDots += 2;
                            }

                            if (img.At(i - 1, j).R < dgGrayValue)
                            {
                                nearDots++;
                            }

                            if (img.At(i + 1, j).R < dgGrayValue)
                            {
                                nearDots++;
                            }

                            if (img.At(i - 1, j + 1).R < dgGrayValue)
                            {
                                nearDots += 2;
                            }

                            if (img.At(i, j + 1).R < dgGrayValue)
                            {
                                nearDots += 2;
                            }

                            if (img.At(i + 1, j + 1).R < dgGrayValue)
                            {
                                nearDots += 2;
                            }
                        }

                        if (nearDots < MaxNearPoints)
                        {
                            img.Set(i, j, Color.FromArgb(255, 255, 255));   //去掉单点 && 粗细小3邻边点
                        }
                    }
                    else  //背景
                    {
                        img.Set(i, j, Color.FromArgb(255, 255, 255));
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 卷积运算
        /// </summary>
        /// <param name="filterMatrix">矩阵</param>
        /// <param name="factor">因子</param>
        /// <param name="bias">偏移</param>
        public void ConvolutionFilter(double[,] filterMatrix,
                                      double factor = 1,
                                      int bias      = 0)
        {
            EffImage rt    = EffImage.White(width, height);
            double   blue  = 0.0;
            double   green = 0.0;
            double   red   = 0.0;

            int filterWidth  = filterMatrix.GetLength(1);
            int filterHeight = filterMatrix.GetLength(0);

            int filterOffset = (filterWidth - 1) / 2;


            // TODO : PADDING边角
            for (int x = filterOffset; x <
                 Width - filterOffset; x++)
            {
                for (int y = filterOffset; y <
                     Height - filterOffset; y++)
                {
                    blue = green = red = 0d;

                    for (int filterX = -filterOffset;
                         filterX <= filterOffset; filterX++)
                    {
                        for (int filterY = -filterOffset;
                             filterY <= filterOffset; filterY++)
                        {
                            var color = At(x + filterX, y + filterY);
                            red   += color.R * filterMatrix[filterY + filterOffset, filterX + filterOffset];
                            green += color.G * filterMatrix[filterY + filterOffset, filterX + filterOffset];
                            blue  += color.B * filterMatrix[filterY + filterOffset, filterX + filterOffset];
                        }
                    }

                    red   = red * factor + bias;
                    green = green * factor + bias;
                    blue  = blue * factor + bias;

                    if (red > 255)
                    {
                        red = 255;
                    }
                    else if (red < 0)
                    {
                        red = 0;
                    }

                    if (green > 255)
                    {
                        green = 255;
                    }
                    else if (green < 0)
                    {
                        green = 0;
                    }

                    if (blue > 255)
                    {
                        blue = 255;
                    }
                    else if (blue < 0)
                    {
                        blue = 0;
                    }

                    rt.Set(x, y, Color.FromArgb((int)red, (int)green, (int)blue));
                }
            }
            this.pixels = rt.pixels;
        }