ARKBreedingStats.ArkOCR.removePixelsUnderThreshold C# (CSharp) Метод

removePixelsUnderThreshold() публичный Метод

public removePixelsUnderThreshold ( Bitmap source, int threshold ) : Bitmap
source System.Drawing.Bitmap
threshold int
Результат System.Drawing.Bitmap
        public Bitmap removePixelsUnderThreshold(Bitmap source, int threshold)
        {
            Bitmap dest = (Bitmap)source.Clone();

            PixelFormat pxf = PixelFormat.Format24bppRgb;

            Rectangle rect = new Rectangle(0, 0, dest.Width, dest.Height);
            BitmapData bmpData = dest.LockBits(rect, ImageLockMode.ReadWrite, pxf);

            IntPtr ptr = bmpData.Scan0;

            int numBytes = bmpData.Stride * dest.Height;
            byte[] rgbValues = new byte[numBytes];

            Marshal.Copy(ptr, rgbValues, 0, numBytes);

            double lowT = threshold * .85, highT = threshold * 1;

            for (int counter = 0; counter < rgbValues.Length; counter++)
            {
                //if (rgbValues[counter] < threshold)
                //    rgbValues[counter] = 0;
                //else
                //    rgbValues[counter] = 255; // maximize the white

                if (rgbValues[counter] < lowT)
                {
                    rgbValues[counter] = 0;
                }
                else if (rgbValues[counter] > highT)
                {
                    rgbValues[counter] = 255; // maximize the white
                }
                else
                {
                    rgbValues[counter] = 150;
                    //// if a neighbour-pixel (up, right, down or left) is above the threshold, also set this ambigious pixel to white
                    //if ((counter % bmpData.Stride > 0 && rgbValues[counter - 3] > threshold)
                    //|| (counter % bmpData.Stride < bmpData.Stride - 3 && rgbValues[counter + 3] > threshold)
                    //|| (counter >= bmpData.Stride && rgbValues[counter - bmpData.Stride] > threshold)
                    //|| (counter < numBytes - bmpData.Stride && rgbValues[counter + bmpData.Stride] > threshold))
                    //    rgbValues[counter] = 255;
                    //else
                    //    rgbValues[counter] = 0;
                }
            }

            Marshal.Copy(rgbValues, 0, ptr, numBytes);

            dest.UnlockBits(bmpData);

            return dest;
        }