Ejemplo n.º 1
0
        public void BinarizationOperation(int threshold)
        {
            outBitmap = new WriteableBitmap(originalBitmap);
            outBitmap.Lock();
            byte MAX_COLOR = 0xFF;

            byte[] LUT = new byte[MAX_COLOR + 1];

            for (int i = threshold; i < MAX_COLOR + 1; i++)
            {
                LUT[i] = MAX_COLOR;
            }

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    pixel.ToGrayscale2();
                    pixel.ChangeColors(LUT[pixel.Red]);
                    DrawPixel(pixel);
                }
            }
            UpdateImage();
        }
Ejemplo n.º 2
0
        public void ApplyMedianFilter(RGBPixel[,] neighbours)
        {
            int size = 3, k = 0;

            RGBPixel[] array = new RGBPixel[size * size];

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    array[k++] = neighbours[i, j];
                }
            }

            Array.Sort(array, delegate(RGBPixel pixel1, RGBPixel pixel2)
            {
                return(pixel1.ColorValue.CompareTo(pixel2.ColorValue));
            });

            RGBPixel median = array[size * size / 2];

            Red   = median.Red;
            Green = median.Green;
            Blue  = median.Blue;
        }
Ejemplo n.º 3
0
        public void ApplyDilationFilter(BinaryMask mask, RGBPixel[,] neighbours)
        {
            int  size = mask.Size;
            byte maxRed = oldRed, maxGreen = oldGreen, maxBlue = oldBlue;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (mask.Matrix[i, j])
                    {
                        RGBPixel neighbour = neighbours[i, j];
                        if (neighbour.Red > maxRed)
                        {
                            maxRed = neighbour.oldRed;
                        }
                        if (neighbour.Green > maxGreen)
                        {
                            maxGreen = neighbour.oldGreen;
                        }
                        if (neighbour.Blue > maxBlue)
                        {
                            maxBlue = neighbour.oldBlue;
                        }
                    }
                }
            }
            Red   = maxRed;
            Green = maxGreen;
            Blue  = maxBlue;
        }
Ejemplo n.º 4
0
        public void ClosingOperation(BinaryMask mask)
        {
            outBitmap = new WriteableBitmap(originalBitmap);
            outBitmap.Lock();
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    RGBPixel[,] neighbours = GetNeighbours(pixel, mask.Size);
                    pixel.ApplyDilationFilter(mask, neighbours);
                }
            }
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    pixelMatrix[x, y].Update();
                }
            }

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    RGBPixel[,] neighbours = GetNeighbours(pixel, mask.Size);
                    pixel.ApplyErosionFilter(mask, neighbours);
                    DrawPixel(pixel);
                }
            }
            UpdateImage();
            UpdatePixelMatrix();
        }
Ejemplo n.º 5
0
        public void ApplyErosionFilter(BinaryMask mask, RGBPixel[,] neighbours)
        {
            int  size = mask.Size;
            byte minRed = oldRed, minGreen = oldGreen, minBlue = oldBlue;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    if (mask.Matrix[i, j])
                    {
                        RGBPixel neighbour = neighbours[i, j];
                        if (neighbour.oldRed < minRed)
                        {
                            minRed = neighbour.oldRed;
                        }
                        if (neighbour.oldGreen < minGreen)
                        {
                            minGreen = neighbour.oldGreen;
                        }
                        if (neighbour.oldBlue < minBlue)
                        {
                            minBlue = neighbour.oldBlue;
                        }
                    }
                }
            }
            Red   = minRed;
            Green = minGreen;
            Blue  = minBlue;
        }
Ejemplo n.º 6
0
 private void MenuDivide_OnClick(object sender, RoutedEventArgs e)
 {
     rgbColor = new RGBPixel(1, 1, 1);
     rgbColor.PropertyChanged += DivideColorPicker_OnChanged;
     colorPickWindow           = new ColorPickWindow(rgbColor);
     colorPickWindow.Closed   += Dialog_OnClosed;
     colorPickWindow.Owner     = this;
     colorPickWindow.Show();
 }
Ejemplo n.º 7
0
 public RGBPixel[,] CopyPixelMatrix()
 {
     RGBPixel[,] newMatrix = new RGBPixel[width, height];
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             newMatrix[x, y] = pixelMatrix[x, y].GetCopy();
         }
     }
     return(newMatrix);
 }
Ejemplo n.º 8
0
        public byte[] GetMinMax()
        {
            byte rMin, rMax, gMin, gMax, bMin, bMax;

            rMin = rMax = pixelMatrix[0, 0].Red;
            gMin = gMax = pixelMatrix[0, 0].Green;
            bMin = bMax = pixelMatrix[0, 0].Blue;

            for (int x = 1; x < width; x++)
            {
                for (int y = 1; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    if (pixel.Red < rMin)
                    {
                        rMin = pixel.Red;
                    }
                    else if (pixel.Red > rMax)
                    {
                        rMax = pixel.Red;
                    }

                    if (pixel.Green < gMin)
                    {
                        gMin = pixel.Green;
                    }
                    else if (pixel.Green > gMax)
                    {
                        gMax = pixel.Green;
                    }

                    if (pixel.Blue < bMin)
                    {
                        bMin = pixel.Blue;
                    }
                    else if (pixel.Blue > bMax)
                    {
                        bMax = pixel.Blue;
                    }
                }
            }
            byte[] output = new byte[6];
            output[0] = rMin;
            output[1] = rMax;
            output[2] = gMin;
            output[3] = gMax;
            output[4] = bMin;
            output[5] = bMax;

            return(output);
        }
Ejemplo n.º 9
0
        public void DrawPixel(RGBPixel pixel)
        {
            Int32Rect rectangle = new Int32Rect(pixel.X, pixel.Y, 1, 1);

            unsafe
            {
                int    colorValue = pixel.ColorValue;
                IntPtr backBuffer = outBitmap.BackBuffer;
                backBuffer          += pixel.Y * outBitmap.BackBufferStride;
                backBuffer          += pixel.X * 4;
                *((int *)backBuffer) = colorValue;
            }
            outBitmap.AddDirtyRect(rectangle);
        }
Ejemplo n.º 10
0
 public void ChangeBrightnessOperation(int value)
 {
     outBitmap = new WriteableBitmap(originalBitmap);
     outBitmap.Lock();
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             RGBPixel pixel = pixelMatrix[x, y];
             pixel.ChangeBrightness(value);
             DrawPixel(pixel);
         }
     }
     UpdateImage();
 }
Ejemplo n.º 11
0
 public void GrayscaleOperation2()
 {
     outBitmap = new WriteableBitmap(originalBitmap);
     outBitmap.Lock();
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             RGBPixel pixel = pixelMatrix[x, y];
             pixel.ToGrayscale2();
             DrawPixel(pixel);
         }
     }
     UpdateImage();
     UpdatePixelMatrix();
 }
Ejemplo n.º 12
0
 public void ApplyLUT(int[] rLUT, int[] gLUT, int[] bLUT)
 {
     outBitmap = new WriteableBitmap(originalBitmap);
     outBitmap.Lock();
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             RGBPixel pixel = pixelMatrix[x, y];
             pixel.ChangeColors(rLUT[pixel.Red], gLUT[pixel.Green], bLUT[pixel.Blue]);
             DrawPixel(pixel);
         }
     }
     UpdateImage();
     UpdatePixelMatrix();
 }
Ejemplo n.º 13
0
        public int[] GetGrayscaleHistogram()
        {
            byte MAX_COLOR = 0xFF;

            int[] h = new int[MAX_COLOR + 1];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    pixel.ToGrayscale2();
                    h[pixel.Red]++;
                }
            }
            return(h);
        }
Ejemplo n.º 14
0
        public void ApplyHitOrMissFilter(Mask mask, RGBPixel[,] neighbours)
        {
            int  size = mask.Size;
            byte MAX_COLOR = 255, MIN_COLOR = 0;
            bool fits = true;

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    RGBPixel neighbour = neighbours[i, j];
                    if (mask.Matrix[i, j] == 1)
                    {
                        if (neighbour.oldRed != MAX_COLOR || neighbour.oldGreen != MAX_COLOR || neighbour.oldBlue != MAX_COLOR)
                        {
                            fits = false;
                            break;
                        }
                    }
                    else if (mask.Matrix[i, j] == -1)
                    {
                        if (neighbour.oldRed != MIN_COLOR || neighbour.oldGreen != MIN_COLOR || neighbour.oldBlue != MIN_COLOR)
                        {
                            fits = false;
                            break;
                        }
                    }
                }
                if (!fits)
                {
                    break;
                }
            }

            if (fits)
            {
                Red   = MAX_COLOR;
                Green = MAX_COLOR;
                Blue  = MAX_COLOR;
            }
            else
            {
                Red   = MIN_COLOR;
                Green = MIN_COLOR;
                Blue  = MIN_COLOR;
            }
        }
Ejemplo n.º 15
0
        public void ThickenOperation()
        {
            outBitmap = new WriteableBitmap(originalBitmap);
            outBitmap.Lock();

            bool changed;

            Mask[] masks = new Mask[8];

            masks[0] = Mask.Thicken1;
            masks[1] = Mask.Thicken2;

            for (int i = 2; i < 8; i++)
            {
                masks[i] = masks[i - 2].RotateRight();
            }

            do
            {
                changed = false;
                for (int i = 0; i < 8; i++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            RGBPixel pixel = pixelMatrix[x, y];
                            RGBPixel[,] neighbours = GetNeighbours(pixel, masks[0].Size);
                            if (pixel.Thicken(masks[i], neighbours))
                            {
                                changed = true;
                            }
                            pixel.Update();
                        }
                    }
                }
            } while (changed);
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    DrawPixel(pixelMatrix[x, y]);
                }
            }
            UpdateImage();
        }
Ejemplo n.º 16
0
 public void HitOrMissOperation()
 {
     outBitmap = new WriteableBitmap(originalBitmap);
     outBitmap.Lock();
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             RGBPixel pixel = pixelMatrix[x, y];
             RGBPixel[,] neighbours = GetNeighbours(pixel, 3);
             pixel.ApplyHitOrMissFilter(Mask.BottomLeftCorner, neighbours);
             DrawPixel(pixel);
         }
     }
     UpdateImage();
     UpdatePixelMatrix();
 }
Ejemplo n.º 17
0
 public void SobelFilterOperation()
 {
     outBitmap = new WriteableBitmap(originalBitmap);
     outBitmap.Lock();
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             RGBPixel pixel = pixelMatrix[x, y];
             RGBPixel[,] neighbours = GetNeighbours(pixel, 3);
             pixel.ApplySobelFilter(neighbours);
             DrawPixel(pixel);
         }
     }
     UpdateImage();
     UpdatePixelMatrix();
 }
Ejemplo n.º 18
0
        public void LogicalSubtraction(RGBPixel value)
        {
            byte MAX_COLOR = 255, MIN_COLOR = 0;

            if (oldRed == MAX_COLOR && value.Red != MAX_COLOR)
            {
                Red   = MAX_COLOR;
                Green = MAX_COLOR;
                Blue  = MAX_COLOR;
            }
            else
            {
                Red   = MIN_COLOR;
                Green = MIN_COLOR;
                Blue  = MIN_COLOR;
            }
        }
Ejemplo n.º 19
0
        public Tuple <int[], int[], int[]> GetHistogram()
        {
            byte MAX_COLOR = 0xFF;

            int[] rH = new int[MAX_COLOR + 1], gH = new int[MAX_COLOR + 1], bH = new int[MAX_COLOR + 1];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    RGBPixel pixel = pixelMatrix[x, y];
                    rH[pixel.Red]++;
                    gH[pixel.Green]++;
                    bH[pixel.Blue]++;
                }
            }
            return(new Tuple <int[], int[], int[]>(rH, gH, bH));
        }
Ejemplo n.º 20
0
 public RGBPixel[,] GetNeighbours(RGBPixel pixel, int maskSize)
 {
     RGBPixel[,] neighbours = new RGBPixel[maskSize, maskSize];
     for (int i = maskSize / 2; i >= -maskSize / 2; i--)
     {
         for (int j = maskSize / 2; j >= -maskSize / 2; j--)
         {
             if (pixel.X - i < 0 || pixel.X - i >= width || pixel.Y - j < 0 || pixel.Y - j >= height)
             {
                 neighbours[i + maskSize / 2, j + maskSize / 2] = new RGBPixel(0, 0, 0);
             }
             else
             {
                 neighbours[i + maskSize / 2, j + maskSize / 2] = pixelMatrix[pixel.X - i, pixel.Y - j];
             }
         }
     }
     return(neighbours);
 }
Ejemplo n.º 21
0
        public void ReadPixelMatrix()
        {
            int x = 0, y = 0;
            int stride = (originalBitmap.PixelWidth * originalBitmap.Format.BitsPerPixel + 7) / 8;

            byte[] pixelArray = new byte[originalBitmap.PixelHeight * stride];
            originalBitmap.CopyPixels(pixelArray, stride, 0);

            pixelMatrix = new RGBPixel[width, height];

            for (int i = 0; i <= pixelArray.Length - 4; i += 4)
            {
                Point    position = new Point(x, y);
                RGBPixel pixel    = new RGBPixel(position, pixelArray[i + 2], pixelArray[i + 1], pixelArray[i]);
                pixelMatrix[x, y] = pixel;
                x = (x + 1) % width;
                if (x == 0)
                {
                    y++;
                }
            }
            grayscaleHistogram = null;
        }
 public ColorPickWindow(RGBPixel rgbColor)
 {
     RGB = rgbColor;
     InitializeComponent();
     DataContext = RGB;
 }
Ejemplo n.º 23
0
 public void Add(RGBPixel value)
 {
     Red   = (byte)(truncate(oldRed + value.Red));
     Green = (byte)(truncate(oldGreen + value.Green));
     Blue  = (byte)(truncate(oldBlue + value.Blue));
 }
Ejemplo n.º 24
0
 public void Divide(RGBPixel value)
 {
     Red   = value.Red != 0 ? (byte)(truncate(oldRed / value.Red)) : (byte)255;
     Green = value.Green != 0 ? (byte)(truncate(oldGreen / value.Green)) : (byte)255;
     Blue  = value.Blue != 0 ? (byte)(truncate(oldBlue / value.Blue)) : (byte)255;
 }
Ejemplo n.º 25
0
 public void Multiply(RGBPixel value)
 {
     Red   = (byte)(truncate(oldRed * value.Red));
     Green = (byte)(truncate(oldGreen * value.Green));
     Blue  = (byte)(truncate(oldBlue * value.Blue));
 }
Ejemplo n.º 26
0
 public void Subtract(RGBPixel value)
 {
     Red   = (byte)(truncate(oldRed - value.Red));
     Green = (byte)(truncate(oldGreen - value.Green));
     Blue  = (byte)(truncate(oldBlue - value.Blue));
 }