CaptchaRecogition.ImageProcess.ComputeThresholdValue C# (CSharp) Метод

ComputeThresholdValue() публичный статический Метод

public static ComputeThresholdValue ( Bitmap img ) : int
img System.Drawing.Bitmap
Результат int
        public static int ComputeThresholdValue(Bitmap img)
        {
            int i;
            int k;
            double csum;
            int thresholdValue = 1;
            int[] ihist = new int[0x100];
            for (i = 0; i < 0x100; i++)
            {
                ihist[i] = 0;
            }
            int gmin = 0xff;
            int gmax = 0;
            for (i = 1; i < (img.Width - 1); i++)
            {
                for (int j = 1; j < (img.Height - 1); j++)
                {
                    int cn = img.GetPixel(i, j).R; //生成直方图
                    ihist[cn]++;
                    if (cn > gmax)
                    {
                        gmax = cn; //找到最大像素点R
                    }
                    if (cn < gmin)
                    {
                        gmin = cn; //找到最小像素点R

                    }
                }
            }
            double sum = csum = 0.0;
            int n = 0;
            for (k = 0; k <= 0xff; k++)
            {
                sum += k * ihist[k];
                n += ihist[k];
            }
            if (n == 0)
            {
                return 60;
            }
            double fmax = -1.0;
            int n1 = 0;
            for (k = 0; k < 0xff; k++)
            {
                n1 += ihist[k];
                if (n1 != 0)
                {
                    int n2 = n - n1;
                    if (n2 == 0)
                    {
                        return thresholdValue;
                    }
                    csum += k * ihist[k];
                    double m1 = csum / ((double)n1);
                    double m2 = (sum - csum) / ((double)n2);
                    double sb = ((n1 * n2) * (m1 - m2)) * (m1 - m2);
                    if (sb > fmax)
                    {
                        fmax = sb;
                        thresholdValue = k;
                    }
                }
            }
            return thresholdValue;
        }

Usage Example

Пример #1
0
        /// <summary>
        /// 去背景
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_bg_Click(object sender, EventArgs e)
        {
            Image img_noise = (Image)pb_grey.Image.Clone();
            //得到灰度图像前景色临界值

            // img_noise = ccccccmd.ImageHelper.RemoveBlock(img_noise,2);//报错
            //int v = ImageProcess.GetDgGrayValue(img_noise);
            int v = ImageProcess.ComputeThresholdValue((Bitmap)img_noise);
            //Image test = ImageProcess.RemoveBg((Bitmap)img_noise ,v );
            // int v2 = Test.ImageProcess.GetDgGrayValue(img_noise);
            Image img_bg = ImageProcess.RemoveBg((Bitmap)img_noise, v); //-----------去背景,内存法

            imgBged = Image2Num((Bitmap)img_bg);
            WriteToFile(imgBged, "experiment\\" + Path.GetFileNameWithoutExtension(imgurl) + "_bged.txt");
            pb_bg.Image = img_bg;
        }