コード例 #1
0
        public override void Binarize(int level)
        {
            ResetToDefault();
            PointTransformation.ConvertToGray(_bitmap, GrayConversionMode.Colorimetric);
            _histogram.GenerateHistograms();
            int Tk = 100;

            while (true)
            {
                int leftSum        = 0;
                int leftBottomSum  = 0;
                int rightSum       = 0;
                int rightBottomSum = 0;

                for (int j = 0; j <= Tk; j++)
                {
                    leftSum       += _histogram.RedHistogram[j] * j;
                    leftBottomSum += _histogram.RedHistogram[j];
                }

                for (int j = Tk + 1; j <= 255; j++)
                {
                    rightSum       += _histogram.RedHistogram[j] * j;
                    rightBottomSum += _histogram.RedHistogram[j];
                }
                if (rightBottomSum == 0)
                {
                    rightBottomSum = 255;
                }
                if (leftBottomSum == 0)
                {
                    leftBottomSum = 1;
                }

                if (leftSum / (2 * leftBottomSum) == rightSum / (2 * rightBottomSum))
                {
                    break;
                }
                if (Tk == (leftSum / (2 * leftBottomSum)) + (rightSum / (2 * rightBottomSum)))
                {
                    break;
                }
                Tk = (leftSum / (2 * leftBottomSum)) + (rightSum / (2 * rightBottomSum));
            }
            _levelBinarization.Binarize(Tk);
        }
コード例 #2
0
        public override void Binarize(int level)
        {
            ResetToDefault();
            int desiredPixels = Convert.ToInt32(_bitmap.Width * _bitmap.Height * ScaleLevelToPercentage(level));

            PointTransformation.ConvertToGray(_bitmap, GrayConversionMode.Colorimetric);
            _histogram.GenerateHistograms();
            int sum = _bitmap.Width * _bitmap.Height;

            for (int i = 255; i >= 0; i--)
            {
                sum -= _histogram.RedHistogram[i];
                if (sum < desiredPixels)
                {
                    level = i + 1;
                    break;
                }
            }
            _levelBinarization.Binarize(level);
        }
コード例 #3
0
        public override void Binarize(int level)
        {
            ResetToDefault();
            PointTransformation.ConvertToGray(_bitmap, GrayConversionMode.Colorimetric);
            _histogram.GenerateHistograms();
            int     pixels = _bitmap.Height * _bitmap.Width;
            decimal Const  = 255 / (decimal)pixels;

            int[] cdf = new int[256];

            Array.Copy(_histogram.RedHistogram, cdf, 255);

            for (int r = 1; r <= 255; r++)
            {
                cdf[r] = cdf[r] + cdf[r - 1];
            }
            decimal Pob = 0.0M, Pb = 0.0M;
            decimal Hob = 0.0M, Hb = 0.0M;

            for (int i = 0; i <= level; i++)
            {
                Pob += cdf[i] * Const;
            }
            for (int i = 0; i <= level; i++)
            {
                Hob += (cdf[i] * Const) / Pob * Convert.ToDecimal(Math.Log(Convert.ToDouble((cdf[i] * Const) / Pob), 2));
            }
            for (int i = level + 1; i <= 255; i++)
            {
                Pb += cdf[i] * Const;
            }
            for (int i = level + 1; i <= 255; i++)
            {
                Hb += (cdf[i] * Const) / Pb * Convert.ToDecimal(Math.Log(Convert.ToDouble((cdf[i] * Const) / Pb), 2));
            }
            level = (int)(-1 * (Hob + Hb));
            _levelBinarization.Binarize(level);
        }