ConvertGrayScaleToBinary() public static method

public static ConvertGrayScaleToBinary ( Bitmap srcimg, int threshold ) : Bitmap
srcimg System.Drawing.Bitmap
threshold int
return System.Drawing.Bitmap
Example #1
0
        public Bitmap Apply(Bitmap srcimg)
        {
            srcimg = ImageUtils.ConvertGrayScaleToBinary(srcimg, 254);
            srcimg = ImageUtils.InvertColors(srcimg);

            MyConnectedComponentsAnalysisFast.MyBlobCounter char_bc    = new MyConnectedComponentsAnalysisFast.MyBlobCounter();
            List <MyConnectedComponentsAnalysisFast.MyBlob> char_blobs = char_bc.GetBlobs(srcimg);

            ushort[] char_labels = char_bc.objectLabels;

            HashSet <int> boarder_char_idx_set = new HashSet <int>();

            for (int i = 0; i < char_blobs.Count; i++)
            {
                if (char_blobs[i].bbx.X == 0 ||
                    char_blobs[i].bbx.Right == srcimg.Width ||
                    char_blobs[i].bbx.Top == 0 ||
                    char_blobs[i].bbx.Bottom == srcimg.Height)
                {
                    boarder_char_idx_set.Add(i);
                }
            }

            for (int i = 0; i < srcimg.Width * srcimg.Height; i++)
            {
                if (char_labels[i] != 0)
                {
                    int idx = char_labels[i] - 1;
                    if (boarder_char_idx_set.Contains(idx))
                    {
                        char_labels[i] = 0;
                    }
                }
            }
            bool[,] img = new bool[srcimg.Height, srcimg.Width];
            for (int i = 0; i < srcimg.Width; i++)
            {
                for (int j = 0; j < srcimg.Height; j++)
                {
                    if (char_labels[j * srcimg.Width + i] == 0)
                    {
                        img[j, i] = false;
                    }
                    else
                    {
                        img[j, i] = true;
                    }
                }
            }
            return(ImageUtils.ArrayBool2DToBitmap(img));
        }
Example #2
0
        public Bitmap Apply(Bitmap srcimg, int char_size)
        {
            double min_pixel_area_size = 0.18;

            srcimg = ImageUtils.ConvertGrayScaleToBinary(srcimg, 254);
            srcimg = ImageUtils.InvertColors(srcimg);

            MyConnectedComponentsAnalysisFast.MyBlobCounter char_bc    = new MyConnectedComponentsAnalysisFast.MyBlobCounter();
            List <MyConnectedComponentsAnalysisFast.MyBlob> char_blobs = char_bc.GetBlobs(srcimg);

            ushort[] char_labels = char_bc.objectLabels;

            HashSet <int> noise_char_idx_set = new HashSet <int>();

            for (int i = 0; i < char_blobs.Count; i++)
            {
                if (((double)char_blobs[i].pixel_count / (double)char_blobs[i].area) < min_pixel_area_size) //line
                {
                    noise_char_idx_set.Add(i);
                }
                if (char_blobs[i].bbx.Width < char_size &&
                    char_blobs[i].bbx.Height < char_size)  // small cc
                {
                    noise_char_idx_set.Add(i);
                }
                if (char_blobs[i].bbx.Width <char_size && char_blobs[i].bbx.Height> char_size * 3)
                {
                    noise_char_idx_set.Add(i);
                }
                if (char_blobs[i].bbx.Height <char_size && char_blobs[i].bbx.Width> char_size * 3)
                {
                    noise_char_idx_set.Add(i);
                }
            }

            for (int i = 0; i < srcimg.Width * srcimg.Height; i++)
            {
                if (char_labels[i] != 0)
                {
                    int idx = char_labels[i] - 1;
                    if (noise_char_idx_set.Contains(idx))
                    {
                        char_labels[i] = 0;
                    }
                }
            }
            bool[,] img = new bool[srcimg.Height, srcimg.Width];
            for (int i = 0; i < srcimg.Width; i++)
            {
                for (int j = 0; j < srcimg.Height; j++)
                {
                    if (char_labels[j * srcimg.Width + i] == 0)
                    {
                        img[j, i] = false;
                    }
                    else
                    {
                        img[j, i] = true;
                    }
                }
            }
            return(ImageUtils.ArrayBool2DToBitmap(img));
        }