Beispiel #1
0
        public static void LowPassFiler(FastImage fastImage, FastImage originalFastImage, int n)
        {
            if (n < 1)
            {
                throw new ArgumentException();
            }

            var matrix = GetLowPassFilterMatrix(n);

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    var sumRed   = 0;
                    var sumGreen = 0;
                    var sumBlue  = 0;

                    for (int i = row - 1; i <= row + 1; i++)
                    {
                        for (int j = column - 1; j <= column + 1; j++)
                        {
                            var pixel = originalFastImage.GetPixel(i, j);
                            sumRed   += pixel.R * matrix[i - row + 1, j - column + 1];
                            sumGreen += pixel.G * matrix[i - row + 1, j - column + 1];
                            sumBlue  += pixel.B * matrix[i - row + 1, j - column + 1];
                        }
                    }

                    var divide   = (n + 2) * (n + 2);
                    var newRed   = sumRed / divide;
                    var newGreen = sumGreen / divide;
                    var newBlue  = sumBlue / divide;
                    var newColor = Color.FromArgb(newRed, newGreen, newBlue);

                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #2
0
        public static void GrayScaleFastImage(FastImage fastImage)
        {
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color   = fastImage.GetPixel(i, j);
                    var average = (byte)((color.R + color.G + color.B) / 3);

                    color = Color.FromArgb(average, average, average);

                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
        }
Beispiel #3
0
        public static void Sobel(FastImage fastImage, FastImage originalFastImage)
        {
            var P = new[, ]
            {
                { -1, -2, -1 },
                { 0, 0, 0 },
                { 1, 2, 1 }
            };

            var Q = new[, ]
            {
                { -1, 0, 1 },
                { -2, 0, 2 },
                { -1, 0, 1 }
            };

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSums(originalFastImage, row, column, P, out var sumRedP, out var sumGreenP, out var sumBlueP);
                    GetConvolutionSums(originalFastImage, row, column, Q, out var sumRedQ, out var sumGreenQ, out var sumBlueQ);

                    var maxRed   = (int)Math.Sqrt(Math.Pow(sumRedP, 2) + Math.Pow(sumRedQ, 2));
                    var maxGreen = (int)Math.Sqrt(Math.Pow(sumGreenP, 2) + Math.Pow(sumGreenQ, 2));
                    var maxBlue  = (int)Math.Sqrt(Math.Pow(sumBlueP, 2) + Math.Pow(sumBlueQ, 2));

                    maxRed   = Normalizare(maxRed, 0, 255);
                    maxGreen = Normalizare(maxGreen, 0, 255);
                    maxBlue  = Normalizare(maxBlue, 0, 255);

                    var newColor = Color.FromArgb(maxRed, maxGreen, maxBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #4
0
        public static void Roberts(FastImage fastImage, FastImage originalFastImage)
        {
            var P = new[, ]
            {
                { -1, 0 },
                { 0, 1 },
            };

            var Q = new[, ]
            {
                { 0, 1 },
                { -1, 0 },
            };

            fastImage.Lock();
            originalFastImage.Lock();

            const int k = 7;

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSumsForRobert(originalFastImage, row, column, P, out var sumRedP, out var sumGreenP, out var sumBlueP);
                    GetConvolutionSumsForRobert(originalFastImage, row, column, Q, out var sumRedQ, out var sumGreenQ, out var sumBlueQ);

                    int newRed   = k * (int)Math.Sqrt(Math.Pow(sumRedP, 2) + Math.Pow(sumRedQ, 2));
                    int newGreen = k * (int)Math.Sqrt(Math.Pow(sumGreenP, 2) + Math.Pow(sumGreenQ, 2));
                    int newBlue  = k * (int)Math.Sqrt(Math.Pow(sumBlueP, 2) + Math.Pow(sumBlueQ, 2));

                    newRed   = Normalizare(newRed, 0, 255);
                    newGreen = Normalizare(newGreen, 0, 255);
                    newBlue  = Normalizare(newBlue, 0, 255);

                    var newColor = Color.FromArgb(newRed, newGreen, newBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #5
0
        public static void CBPF(FastImage fastImage, FastImage originalFastImage, int contextSize, int searchRadius, int sumLimit)
        {
            fastImage.Lock();
            originalFastImage.Lock();

            for (int i = 0; i < originalFastImage.Width; i++)
            {
                for (int j = 0; j < originalFastImage.Height; j++)
                {
                    if (SaltPepper(originalFastImage, i, j))
                    {
                        var newColor = CBP(originalFastImage, i, j, contextSize, searchRadius, sumLimit);
                        fastImage.SetPixel(i, j, Color.FromArgb(newColor, newColor, newColor));
                    }
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #6
0
        public static void NegateFastImage(FastImage fastImage)
        {
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color = fastImage.GetPixel(i, j);

                    var newRed   = (byte)(255 - color.R);
                    var newGreen = (byte)(255 - color.G);
                    var newBlue  = (byte)(255 - color.B);

                    color = Color.FromArgb(newRed, newGreen, newBlue);

                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
        }
Beispiel #7
0
        public static void ApplyEqualization(FastImage fastImage, FastImage originalFastImage)
        {
            var oldGrayScaleHistogram = originalFastImage.GrayScaleHistogram;
            var newGrayScaleHistogram = new int[256];

            newGrayScaleHistogram[0] = oldGrayScaleHistogram[0];

            for (var i = 1; i < oldGrayScaleHistogram.Length; i++)
            {
                newGrayScaleHistogram[i] = newGrayScaleHistogram[i - 1] + oldGrayScaleHistogram[i];
            }

            var transf = new int[256];

            for (var i = 0; i < transf.Length; i++)
            {
                transf[i] = (newGrayScaleHistogram[i] * 255) / (originalFastImage.Width * originalFastImage.Height);
            }

            originalFastImage.Lock();
            fastImage.Lock();

            for (var i = 0; i < fastImage.Width; i++)
            {
                for (var j = 0; j < fastImage.Height; j++)
                {
                    var color    = originalFastImage.GetPixel(i, j);
                    var gray     = (color.R + color.G + color.B) / 3;
                    var newColor = Color.FromArgb(transf[gray], transf[gray], transf[gray]);

                    fastImage.SetPixel(i, j, newColor);
                }
            }

            originalFastImage.Unlock();
            fastImage.Unlock();
        }
        public Bitmap Compute(Point point)
        {
            image.Lock();
            canvas.Lock();
            pointsQueue.Enqueue(point);
            sumaIntensitati += Utils.GetGrayscale(image.GetPixel(point));
            contor++;

            while (pointsQueue.Count > 0)
            {
                var selectedPoint = pointsQueue.Peek();

                if (matriceVizitate[selectedPoint.X, selectedPoint.Y])
                {
                    pointsQueue.Dequeue();
                    continue;
                }

                var limitXmin = selectedPoint.X == 0 ? selectedPoint.X : selectedPoint.X - 1;
                var limitXmax = selectedPoint.X == image.Width - 1 ? selectedPoint.X : selectedPoint.X + 1;
                var limitYmin = selectedPoint.Y == 0 ? 0 : selectedPoint.Y - 1;
                var limitYmax = selectedPoint.Y == image.Height - 1 ? selectedPoint.Y : selectedPoint.Y + 1;

                var pointA = new Point {
                    X = limitXmin, Y = limitYmin
                };
                var pointB = new Point {
                    X = limitXmax, Y = limitYmin
                };
                var pointC = new Point {
                    X = limitXmin, Y = limitYmax
                };
                var pointD = new Point {
                    X = limitXmax, Y = limitYmax
                };

                var culoareA = image.GetPixel(pointA);
                var culoareB = image.GetPixel(pointB);
                var culoareC = image.GetPixel(pointC);
                var culoareD = image.GetPixel(pointD);

                if (Math.Abs(Utils.GetGrayscale(culoareA) - mediaIntensitati) < prag)
                {
                    pointsQueue.Enqueue(pointA);
                }

                if (Math.Abs(Utils.GetGrayscale(culoareB) - mediaIntensitati) < prag)
                {
                    pointsQueue.Enqueue(pointB);
                }

                if (Math.Abs(Utils.GetGrayscale(culoareB) - mediaIntensitati) < prag)
                {
                    pointsQueue.Enqueue(pointC);
                }

                if (Math.Abs(Utils.GetGrayscale(culoareB) - mediaIntensitati) < prag)
                {
                    pointsQueue.Enqueue(pointD);
                }

                sumaIntensitati += Utils.GetGrayscale(image.GetPixel(point));
                contor++;
                matriceVizitate[selectedPoint.X, selectedPoint.Y] = true;
                canvas.SetPixel(selectedPoint, image.GetPixel(selectedPoint));
                pointsQueue.Dequeue();
            }

            image.Unlock();
            canvas.Unlock();

            return(canvas.GetBitMap());
        }
Beispiel #9
0
        public static void FreiChen(FastImage fastImage, FastImage originalFastImage)
        {
            var matrixes = new List <double[, ]>();
            var radical2 = Math.Sqrt(2);

            //F1
            matrixes.Add(new double[, ]
            {
                { 1, radical2, 1 },
                { 0, 0, 0 },
                { -1, -radical2, -1 }
            });

            // F2
            matrixes.Add(new double[, ]
            {
                { 1, 0, -1 },
                { radical2, 0, -radical2 },
                { 1, 0, -1 }
            });

            // F3
            matrixes.Add(new double[, ]
            {
                { 0, -1, radical2 },
                { 1, 0, -1 },
                { -radical2, 1, 0 }
            });

            // F4
            matrixes.Add(new double[, ]
            {
                { radical2, -1, 0 },
                { -1, 0, 1 },
                { 0, 1, -radical2 }
            });

            // F5
            matrixes.Add(new double[, ]
            {
                { 0, 1, 0 },
                { -1, 0, -1 },
                { 0, 1, 0 }
            });

            // F6
            matrixes.Add(new double[, ]
            {
                { -1, 0, 1 },
                { 0, 0, 0 },
                { 1, 0, -1 }
            });

            // F7
            matrixes.Add(new double[, ]
            {
                { 1, -2, 1 },
                { -2, 4, -2 },
                { 1, -2, 1 }
            });

            // F8
            matrixes.Add(new double[, ]
            {
                { -2, 1, -2 },
                { 1, 4, 1 },
                { -2, 1, -2 }
            });

            // F9
            matrixes.Add(new double[, ]
            {
                { 1d / 9, 1d / 9, 1d / 9 },
                { 1d / 9, 1d / 9, 1d / 9 },
                { 1d / 9, 1d / 9, 1d / 9 }
            });

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSumsForFreiChen(originalFastImage, row, column, matrixes, out var sumRed, out var sumGreen, out var sumBlue);

                    sumRed   = Normalizare(sumRed, 0, 255);
                    sumGreen = Normalizare(sumGreen, 0, 255);
                    sumBlue  = Normalizare(sumBlue, 0, 255);

                    var newColor = Color.FromArgb(sumRed, sumGreen, sumBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #10
0
        public static void ChangeIntensityForFastImage(FastImage fastImage, FastImage originalFastImage, int intensity)
        {
            fastImage.Lock();
            originalFastImage.Lock();

            var minR = originalFastImage.RedMinimumValue;
            var maxR = originalFastImage.RedMaximumValue;
            var minG = originalFastImage.GreenMinimumValue;
            var maxG = originalFastImage.GreenMaximumValue;
            var minB = originalFastImage.BlueMinimumValue;
            var maxB = originalFastImage.BlueMaximumValue;

            var redA   = GetA(minR, intensity);
            var redB   = GetB(maxR, intensity);
            var greenA = GetA(minG, intensity);
            var greenB = GetB(maxG, intensity);
            var blueA  = GetA(minB, intensity);
            var blueB  = GetB(maxB, intensity);

            for (var i = 0; i < originalFastImage.Width; i++)
            {
                for (var j = 0; j < originalFastImage.Height; j++)
                {
                    var oldRed = originalFastImage.GetPixel(i, j).R;
                    var newRed = (redB - redA) * (oldRed - minR) / (maxR - minR) + redA;

                    var oldGreen = originalFastImage.GetPixel(i, j).G;
                    var newGreen = (greenB - greenA) * (oldGreen - minG) / (maxG - minG) + greenA;

                    var oldBlue = originalFastImage.GetPixel(i, j).B;
                    var newBlue = (blueB - blueA) * (oldBlue - minB) / (maxB - minB) + blueA;

                    if (newRed > 255)
                    {
                        newRed = 255;
                    }
                    else if (newRed < 0)
                    {
                        newRed = 0;
                    }

                    if (newGreen > 255)
                    {
                        newGreen = 255;
                    }
                    else if (newGreen < 0)
                    {
                        newGreen = 0;
                    }

                    if (newBlue > 255)
                    {
                        newBlue = 255;
                    }
                    else if (newBlue < 0)
                    {
                        newBlue = 0;
                    }

                    var newColor = Color.FromArgb(newRed, newGreen, newBlue);
                    fastImage.SetPixel(i, j, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #11
0
        public static void Kirsch(FastImage fastImage, FastImage originalFastImage)
        {
            var H1 = new[, ]
            {
                { -1, 0, 1 },
                { -1, 0, 1 },
                { -1, 0, 1 }
            };

            var H2 = new[, ]
            {
                { 1, 1, 1 },
                { 0, 0, 0 },
                { -1, -1, -1 }
            };

            var H3 = new[, ]
            {
                { 0, 1, 1 },
                { -1, 0, 1 },
                { -1, -1, 0 }
            };

            var H4 = new[, ]
            {
                { 1, 1, 0 },
                { 1, 0, -1 },
                { 0, -1, -1 }
            };

            fastImage.Lock();
            originalFastImage.Lock();

            for (int row = 1; row < originalFastImage.Width - 2; row++)
            {
                for (int column = 1; column < originalFastImage.Height - 2; column++)
                {
                    GetConvolutionSums(originalFastImage, row, column, H1, out var sumRedH1, out var sumGreenH1, out var sumBlueH1);
                    GetConvolutionSums(originalFastImage, row, column, H2, out var sumRedH2, out var sumGreenH2, out var sumBlueH2);
                    GetConvolutionSums(originalFastImage, row, column, H3, out var sumRedH3, out var sumGreenH3, out var sumBlueH3);
                    GetConvolutionSums(originalFastImage, row, column, H4, out var sumRedH4, out var sumGreenH4, out var sumBlueH4);

                    var sumsRed = new List <int> {
                        sumRedH1, sumRedH2, sumRedH3, sumRedH4
                    };
                    var sumsGreen = new List <int> {
                        sumGreenH1, sumGreenH2, sumGreenH3, sumGreenH4
                    };
                    var sumsBlue = new List <int> {
                        sumBlueH1, sumBlueH2, sumBlueH3, sumBlueH4
                    };

                    var maxRed   = sumsRed.Max();
                    var maxGreen = sumsGreen.Max();
                    var maxBlue  = sumsBlue.Max();

                    maxRed   = Normalizare(maxRed, 0, 255);
                    maxGreen = Normalizare(maxGreen, 0, 255);
                    maxBlue  = Normalizare(maxBlue, 0, 255);

                    var newColor = Color.FromArgb(maxRed, maxGreen, maxBlue);
                    fastImage.SetPixel(row, column, newColor);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #12
0
        public static void Unsharp(FastImage fastImage, FastImage originalFastImage)
        {
            int[,] H = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
            double c = 0.6;

            fastImage.Lock();
            originalFastImage.Lock();
            for (int i = 1; i < fastImage.Width - 2; i++)
            {
                for (int j = 1; j < fastImage.Height - 2; j++)
                {
                    double sumR   = 0;
                    double sumG   = 0;
                    double sumB   = 0;
                    double SumaR0 = 0;
                    double SumaG0 = 0;
                    double SumaB0 = 0;
                    double R      = 0;
                    double G      = 0;
                    double B      = 0;
                    Color  color;
                    for (int row = i - 1; row <= i + 1; row++)
                    {
                        for (int col = j - 1; col <= j + 1; col++)
                        {
                            color = originalFastImage.GetPixel(row, col);
                            R     = color.R;
                            G     = color.G;
                            B     = color.B;

                            sumR = sumR + R * H[row - i + 1, col - j + 1];
                            sumG = sumG + G * H[row - i + 1, col - j + 1];
                            sumB = sumB + B * H[row - i + 1, col - j + 1];
                        }
                    }
                    sumR /= (1 + 2) * (1 + 2);
                    sumG /= (1 + 2) * (1 + 2);
                    sumB /= (1 + 2) * (1 + 2);

                    SumaR0 = (c / (1.2 - 1.0)) * R - ((1.0 - c) / (1.2 - 1.0)) * sumR;
                    SumaG0 = (c / (1.2 - 1.0)) * G - ((1.0 - c) / (1.2 - 1.0)) * sumG;
                    SumaB0 = (c / (1.2 - 1.0)) * B - ((1.0 - c) / (1.2 - 1.0)) * sumB;

                    if (SumaR0 > 255)
                    {
                        SumaR0 = 255;
                    }
                    if (SumaR0 < 0)
                    {
                        SumaR0 = 0;
                    }
                    if (SumaG0 > 255)
                    {
                        SumaG0 = 255;
                    }
                    if (SumaG0 < 0)
                    {
                        SumaG0 = 0;
                    }
                    if (SumaB0 > 255)
                    {
                        SumaB0 = 255;
                    }
                    if (SumaB0 < 0)
                    {
                        SumaB0 = 0;
                    }

                    color = Color.FromArgb((int)SumaR0, (int)SumaG0, (int)SumaB0);
                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }
Beispiel #13
0
        public static void HighPassFilter(FastImage fastImage, FastImage originalFastImage)
        {
            int[,] H =
            {
                {  0, -1,  0 },
                { -1,  5, -1 },
                {  0, -1,  0 }
            };

            fastImage.Lock();
            originalFastImage.Lock();
            for (int i = 1; i <= fastImage.Width - 2; i++)
            {
                for (int j = 1; j <= fastImage.Height - 2; j++)
                {
                    var   sumaR = 0;
                    var   sumaG = 0;
                    var   sumaB = 0;
                    Color color;

                    for (int row = i - 1; row <= i + 1; row++)
                    {
                        for (int col = j - 1; col <= j + 1; col++)
                        {
                            color = originalFastImage.GetPixel(row, col);
                            int R = color.R;
                            int G = color.G;
                            int B = color.B;

                            sumaR += R * H[row - i + 1, col - j + 1];
                            sumaG += G * H[row - i + 1, col - j + 1];
                            sumaB += B * H[row - i + 1, col - j + 1];
                        }
                    }
                    if (sumaR > 255)
                    {
                        sumaR = 255;
                    }
                    if (sumaR < 0)
                    {
                        sumaR = 0;
                    }
                    if (sumaG > 255)
                    {
                        sumaG = 255;
                    }
                    if (sumaG < 0)
                    {
                        sumaG = 0;
                    }
                    if (sumaB > 255)
                    {
                        sumaB = 255;
                    }
                    if (sumaB < 0)
                    {
                        sumaB = 0;
                    }

                    color = Color.FromArgb(sumaR, sumaG, sumaB);
                    fastImage.SetPixel(i, j, color);
                }
            }

            fastImage.Unlock();
            originalFastImage.Unlock();
        }