Exemple #1
0
        public static PercentagAreasData Calculate(WriteableBitmap bmp, Expression <Func <HSLColor, bool> > condition)
        {
            int width       = bmp.PixelWidth;
            int height      = bmp.PixelHeight;
            int totalPixels = width * height;

            Func <HSLColor, bool> f = condition.Compile();

            int sum = 0;

            using (BitmapContext context = bmp.GetBitmapContext())
            {
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        Color tmp = PixelHelper.GetPixel(context, x, y);

                        if (f.Invoke(RGB2HSL(tmp)))
                        {
                            ++sum;
                        }
                    }
                }
            }

            return(new PercentagAreasData(sum, totalPixels));
        }
        private static bool Delete2s(BitmapContext context, int[,] pixels, bool change)
        {
            int width  = context.Width;
            int height = context.Height;

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (pixels[i, j] == 2)
                    {
                        int weight = CalculateWeight(i, j, context);
                        if (A.Contains(weight))
                        {
                            pixels[i, j] = 0;
                            PixelHelper.SetWhite(context, i, j);
                            change = true;
                        }
                        else
                        {
                            pixels[i, j] = 1;
                        }
                    }
                }
            }

            return(change);
        }
Exemple #3
0
        public static Dictionary <Point, MinutiaeType> DetectMinutiae(WriteableBitmap bitmap)
        {
            Dictionary <Point, MinutiaeType> points = new Dictionary <Point, MinutiaeType>();

            using (BitmapContext context = bitmap.GetBitmapContext())
            {
                int width  = context.Width;
                int height = context.Height;

                int[] mask = new int[9];


                for (int x = 1; x < width - 1; ++x)
                {
                    for (int y = 1; y < height - 1; ++y)
                    {
                        if (PixelHelper.IsBlack(context, x, y))
                        {
                            mask[0] = PixelHelper.IsWhite(context, x, y + 1) ? 0 : 1;
                            mask[1] = PixelHelper.IsWhite(context, x - 1, y + 1) ? 0 : 1;
                            mask[2] = PixelHelper.IsWhite(context, x - 1, y) ? 0 : 1;
                            mask[3] = PixelHelper.IsWhite(context, x - 1, y - 1) ? 0 : 1;
                            mask[4] = PixelHelper.IsWhite(context, x, y - 1) ? 0 : 1;
                            mask[5] = PixelHelper.IsWhite(context, x + 1, y - 1) ? 0 : 1;
                            mask[6] = PixelHelper.IsWhite(context, x + 1, y) ? 0 : 1;
                            mask[7] = PixelHelper.IsWhite(context, x + 1, y + 1) ? 0 : 1;
                            mask[8] = mask[0];


                            MinutiaeType cn = CalculateCN(mask);
                            if (IsMinutiae(cn))
                            {
                                points.Add(new Point(x, y), cn);

                                if (cn == MinutiaeType.Point)
                                {
                                    PixelHelper.SetPixel(context, x, y, Colors.Red);
                                }
                                else if (cn == MinutiaeType.Ending)
                                {
                                    PixelHelper.SetPixel(context, x, y, Colors.Green);
                                }
                                else if (cn == MinutiaeType.Fork)
                                {
                                    PixelHelper.SetPixel(context, x, y, Colors.Yellow);
                                }
                                else if (cn == MinutiaeType.Crossing)
                                {
                                    PixelHelper.SetPixel(context, x, y, Colors.DeepPink);
                                }
                            }
                        }
                    }
                }
            }

            return(points);
        }
        public static void MedianFilter(WriteableBitmap Input, int maskSize)
        {
            int width  = Input.PixelWidth;
            int height = Input.PixelHeight;

            WriteableBitmap originalImage = Input.Clone();

            using (BitmapContext contextOriginal = originalImage.GetBitmapContext())
            {
                using (BitmapContext context = Input.GetBitmapContext())
                {
                    // red, green and blue are a 2D square of odd size like 3x3, 5x5, 7x7, ... For simplicity stored it into 1D array.
                    byte[] red, green, blue;

                    /** Median Filter operation */
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            byte a = PixelHelper.GetPixel(contextOriginal, x, y).A;
                            red   = new byte[maskSize * maskSize];
                            green = new byte[maskSize * maskSize];
                            blue  = new byte[maskSize * maskSize];
                            int count = 0;
                            for (int r = y - (maskSize / 2); r <= y + (maskSize / 2); r++)
                            {
                                for (int c = x - (maskSize / 2); c <= x + (maskSize / 2); c++)
                                {
                                    if (r < 0 || r >= height || c < 0 || c >= width)
                                    {
                                        /** Some portion of the mask is outside the image. */
                                        continue;
                                    }
                                    else
                                    {
                                        Color tmp = PixelHelper.GetPixel(contextOriginal, c, r);
                                        red[count]   = tmp.R;
                                        green[count] = tmp.G;
                                        blue[count]  = tmp.B;
                                        ++count;
                                    }
                                }
                            }
                            Array.Sort(red);
                            Array.Sort(green);
                            Array.Sort(blue);

                            int index = (count % 2 == 0) ? count / 2 - 1 : count / 2;
                            PixelHelper.SetPixel(context, x, y, a, red[index], green[index], blue[index]);
                        }
                    }
                }
            }
        }
        public static WriteableBitmap DilateAndErode(WriteableBitmap writeableBitmap, bool?[,] matrix3x3)
        {
            int width  = writeableBitmap.PixelWidth;
            int height = writeableBitmap.PixelHeight;

            WriteableBitmap newImage = writeableBitmap.Clone();

            using (BitmapContext newcontext = newImage.GetBitmapContext())
            {
                using (BitmapContext context = writeableBitmap.GetBitmapContext())
                {
                    for (int x = 0; x < width; ++x)
                    {
                        for (int y = 0; y < height; ++y)
                        {
                            bool exaclyMatch = true;

                            for (int i = 0; i < 3; ++i)
                            {
                                for (int j = 0; j < 3; ++j)
                                {
                                    bool?matVal = matrix3x3[i, j];
                                    int  cX     = x - 1 + i;
                                    int  cY     = y - 1 + j;

                                    if (cX > 0 && cY > 0 && cX < width && cY < height && matVal != null)
                                    {
                                        if (!(PixelHelper.IsBlack(context, cX, cY) == matVal))
                                        {
                                            exaclyMatch = false;
                                            goto ExitLoop;
                                        }
                                    }
                                }
                            }
ExitLoop:

                            if (exaclyMatch)
                            {
                                PixelHelper.SetBlack(newcontext, x, y);
                            }
                            else
                            {
                                PixelHelper.SetWhite(newcontext, x, y);
                            }
                        }
                    }
                }
            }
            return(newImage);
        }
        private static int CalculateWeight(int i, int j, BitmapContext context)
        {
            int width  = context.Width;
            int height = context.Height;

            int[] N      = new int[] { 128, 1, 2, 64, 0, 4, 32, 16, 8 };
            int   weight = 0;

            if (i - 1 > 0)
            {
                if (j - 1 > 0 && PixelHelper.IsBlack(context, i - 1, j - 1))
                {
                    weight += N[0];
                }
                if (PixelHelper.IsBlack(context, i - 1, j))
                {
                    weight += N[3];
                }
                if (j + 1 < height && PixelHelper.IsBlack(context, i - 1, j + 1))
                {
                    weight += N[6];
                }
            }
            if (i + 1 < width)
            {
                if (j - 1 > 0 && PixelHelper.IsBlack(context, i + 1, j - 1))
                {
                    weight += N[2];
                }
                if (PixelHelper.IsBlack(context, i + 1, j))
                {
                    weight += N[5];
                }
                if (j + 1 < height && PixelHelper.IsBlack(context, i + 1, j + 1))
                {
                    weight += N[8];
                }
            }

            if (j - 1 > 0 && PixelHelper.IsBlack(context, i, j - 1))
            {
                weight += N[1];
            }
            if (j + 1 < height && PixelHelper.IsBlack(context, i, j + 1))
            {
                weight += N[7];
            }

            return(weight);
        }
        private static int[,] PixelInfo(BitmapContext context)
        {
            int width  = context.Width;
            int height = context.Height;

            int[,] pixels = new int[width, height];
            for (int i = 0; i < width; ++i)
            {
                for (int j = 0; j < height; ++j)
                {
                    if (PixelHelper.IsBlack(context, i, j))
                    {
                        pixels[i, j] = 1;
                    }
                    else
                    {
                        pixels[i, j] = 0;
                    }
                }
            }
            return(pixels);
        }
        public static void KuwaharaFilter(WriteableBitmap Image, int size)
        {
            int width  = Image.PixelWidth;
            int height = Image.PixelHeight;

            WriteableBitmap originalImage = Image.Clone();

            using (BitmapContext contextOriginal = originalImage.GetBitmapContext())
            {
                using (BitmapContext context = Image.GetBitmapContext())
                {
                    int[] ApetureMinX = { -(size / 2), 0, -(size / 2), 0 };
                    int[] ApetureMaxX = { 0, (size / 2), 0, (size / 2) };
                    int[] ApetureMinY = { -(size / 2), -(size / 2), 0, 0 };
                    int[] ApetureMaxY = { 0, 0, (size / 2), (size / 2) };

                    for (int x = 0; x < width; ++x)
                    {
                        for (int y = 0; y < height; ++y)
                        {
                            int[] RValues   = { 0, 0, 0, 0 };
                            int[] GValues   = { 0, 0, 0, 0 };
                            int[] BValues   = { 0, 0, 0, 0 };
                            int[] NumPixels = { 0, 0, 0, 0 };
                            int[] MaxRValue = { 0, 0, 0, 0 };
                            int[] MaxGValue = { 0, 0, 0, 0 };
                            int[] MaxBValue = { 0, 0, 0, 0 };
                            int[] MinRValue = { 255, 255, 255, 255 };
                            int[] MinGValue = { 255, 255, 255, 255 };
                            int[] MinBValue = { 255, 255, 255, 255 };
                            for (int i = 0; i < 4; ++i)
                            {
                                for (int x2 = ApetureMinX[i]; x2 < ApetureMaxX[i]; ++x2)
                                {
                                    int TempX = x + x2;
                                    if (TempX >= 0 && TempX < width)
                                    {
                                        for (int y2 = ApetureMinY[i]; y2 < ApetureMaxY[i]; ++y2)
                                        {
                                            int TempY = y + y2;
                                            if (TempY >= 0 && TempY < height)
                                            {
                                                Color TempColor = PixelHelper.GetPixel(contextOriginal, TempX, TempY);
                                                RValues[i] += TempColor.R;
                                                GValues[i] += TempColor.G;
                                                BValues[i] += TempColor.B;

                                                if (TempColor.R > MaxRValue[i])
                                                {
                                                    MaxRValue[i] = TempColor.R;
                                                }
                                                else if (TempColor.R < MinRValue[i])
                                                {
                                                    MinRValue[i] = TempColor.R;
                                                }

                                                if (TempColor.G > MaxGValue[i])
                                                {
                                                    MaxGValue[i] = TempColor.G;
                                                }
                                                else if (TempColor.G < MinGValue[i])
                                                {
                                                    MinGValue[i] = TempColor.G;
                                                }

                                                if (TempColor.B > MaxBValue[i])
                                                {
                                                    MaxBValue[i] = TempColor.B;
                                                }
                                                else if (TempColor.B < MinBValue[i])
                                                {
                                                    MinBValue[i] = TempColor.B;
                                                }

                                                ++NumPixels[i];
                                            }
                                        }
                                    }
                                }
                            }
                            int j             = 0;
                            int MinDifference = 10000;
                            for (int i = 0; i < 4; ++i)
                            {
                                int CurrentDifference = (MaxRValue[i] - MinRValue[i]) + (MaxGValue[i] - MinGValue[i]) + (MaxBValue[i] - MinBValue[i]);
                                if (CurrentDifference < MinDifference && NumPixels[i] > 0)
                                {
                                    j             = i;
                                    MinDifference = CurrentDifference;
                                }
                            }

                            PixelHelper.SetPixel(context, x, y, (byte)(RValues[j] / NumPixels[j]), (byte)(GValues[j] / NumPixels[j]), (byte)(BValues[j] / NumPixels[j]));
                        }
                    }
                }
            }
        }