CaptchaRecogition.ImageProcess.RemoveNoise C# (CSharp) Method

RemoveNoise() public static method

去除噪点
public static RemoveNoise ( Bitmap img, int maxAroundPoints = 1 ) : Image
img System.Drawing.Bitmap 图片
maxAroundPoints int 噪点的最大粘连数
return Image
        public static Image RemoveNoise(Bitmap img, int maxAroundPoints=1)
        {
            int width = img.Width;
            int height = img.Height;
            BitmapData bdata = img.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite,
                PixelFormat.Format32bppRgb);

            #region 指针法

            unsafe
            {
                byte* ptr = (byte*)bdata.Scan0.ToPointer();
                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        if (i == 0 || i == height - 1 || j == 0 || j == width - 1) //边界点,直接当作噪点去除掉
                        {
                            ptr[0] = ptr[1] = ptr[2] = 255;
                        }
                        else
                        {
                            int aroundPoint = 0;
                            if (ptr[0] != 255) //看标记,不是背景点
                            {
                                //判断其周围8个方向与自己相连接的有几个点
                                if ((ptr - 4)[0] != 255) aroundPoint++; //左边
                                if ((ptr + 4)[0] != 255) aroundPoint++; //右边
                                if ((ptr - width * 4)[0] != 255) aroundPoint++; //正上方
                                if ((ptr - width * 4 + 4)[0] != 255) aroundPoint++; //右上角
                                if ((ptr - width * 4 - 4)[0] != 255) aroundPoint++; //左上角
                                if ((ptr + width * 4)[0] != 255) aroundPoint++; //正下方
                                if ((ptr + width * 4 + 4)[0] != 255) aroundPoint++; //右下方
                                if ((ptr + width * 4 - 4)[0] != 255) aroundPoint++; //左下方
                            }
                            if (aroundPoint < maxAroundPoints)//目标点是噪点
                            {
                                ptr[0] = ptr[1] = ptr[2] = 255; //去噪点
                            }
                        }
                        ptr += 4;
                    }
                    ptr += bdata.Stride - width * 4;
                }
            }
            img.UnlockBits(bdata);

            #endregion

            return img;
        }

Usage Example

コード例 #1
0
        /// <summary>
        /// 去噪点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_noise_Click(object sender, EventArgs e)
        {
            Image img_noise = ImageProcess.RemoveNoise((Bitmap)pb_bg.Image.Clone(), noiseThreshold);

            //img_noise = ccccccmd.ImageHelper.ClearNoise(img_noise, v, 2);
            imgNoised = Image2Num((Bitmap)img_noise);
            WriteToFile(imgNoised, "experiment\\" + Path.GetFileNameWithoutExtension(imgurl) + "_noised.txt");
            pb_noise.Image = img_noise;
        }