Ejemplo n.º 1
0
        private (double sR, double sG, double sB) GetF1(Point point)
        {
            var H = new double[, ]
            {
                { 1, Math.Sqrt(2), 1 },
                { 0, 0, 0 },
                { -1, -Math.Sqrt(2), -1 }
            };
            double sR = 0, sG = 0, sB = 0;

            for (var col = point.X - 1; col <= point.X + 1; col++)
            {
                for (var row = point.Y - 1; row <= point.Y + 1; row++)
                {
                    var _point = new Point {
                        X = col, Y = row
                    };
                    var color = image.GetPixel(_point);
                    sG += (color.G * H[row - point.Y + 1, col - point.X + 1]);
                    sR += (color.R * H[row - point.Y + 1, col - point.X + 1]);
                    sB += (color.B * H[row - point.Y + 1, col - point.X + 1]);
                }
            }

            return(sR : sR, sG : sG, sB : sB);
        }
Ejemplo n.º 2
0
 private void Fts()
 {
     for (var row = 1; row <= image.Height - 2; row++)
     {
         for (var col = 1; col <= image.Width - 2; col++)
         {
             int   sR = 0, sG = 0, sB = 0;
             Color color;
             for (var _col = col - 1; _col <= col + 1; _col++)
             {
                 for (var _row = row - 1; _row <= row + 1; _row++)
                 {
                     var _point = new Point {
                         X = _col, Y = _row
                     };
                     color = image.GetPixel(_point);
                     sR   += (int)(color.R * H[_row - row + 1, _col - col + 1]);
                     sG   += (int)(color.G * H[_row - row + 1, _col - col + 1]);
                     sB   += (int)(color.B * H[_row - row + 1, _col - col + 1]);
                 }
             }
             var point = new Point {
                 X = col, Y = row
             };
             sR    = Utils.ClampByte(sR);
             sG    = Utils.ClampByte(sG);
             sB    = Utils.ClampByte(sB);
             color = Color.FromArgb(sR, sG, sB);
             image.SetPixel(point, color);
         }
     }
 }
Ejemplo n.º 3
0
        private static bool SaltPepper(FastImage fastImage, int i, int j)
        {
            var color = fastImage.GetPixel(i, j);

            var isBlack = color.R == 0 && color.G == 0 && color.B == 0;
            var isWhite = color.R == 255 && color.G == 255 && color.B == 255;

            return(isBlack || isWhite);
        }
Ejemplo n.º 4
0
        public Gene(FastImage src, int minx, int miny, int maxx, int maxy, int minsz, int maxsz)
        {
            x  = _rnd.Next(minx, maxx);
            y  = _rnd.Next(miny, maxy);
            sz = _rnd.Next(minsz, maxsz);
            //color = new SimpleColor(_rnd.Next(0, 256), _rnd.Next(0, 256), _rnd.Next(0, 256));
            SimpleColor sc = src.GetPixel(_rnd.Next(0, src.width), _rnd.Next(0, src.height));

            color = new SimpleColor(sc.GetR(), sc.GetG(), sc.GetB());
        }
Ejemplo n.º 5
0
        private Color CBP(Point point)
        {
            var Q = new Dictionary <Color, int>();

            for (var y = point.Y - SR; y <= point.Y + SR; y++)
            {
                if (!(0 <= y && y < image.Width))
                {
                    continue;
                }
                for (var x = point.X - SR; x <= point.X + SR; x++)
                {
                    if (!(0 <= x && x < image.Height))
                    {
                        continue;
                    }
                    if (x == point.X && y == point.Y)
                    {
                        continue;
                    }
                    var currentPoint = new Point {
                        X = x, Y = y
                    };
                    if (SAD(point, currentPoint) < T && !SaltAndPepper(currentPoint))
                    {
                        var color = image.GetPixel(currentPoint);
                        if (Q.ContainsKey(color))
                        {
                            Q[color]++;
                        }
                        else
                        {
                            Q.Add(color, 1);
                        }
                    }
                }
            }

            var max = Q.Max(pair => pair.Value);

            return(Q.Where(pair => pair.Value == max).Select(pair => pair.Key).ToList()[0]);
        }
Ejemplo n.º 6
0
        private void DOSmth()
        {
            for (var row = 1; row <= image.Height - 2; row++)
            {
                for (var col = 1; col <= image.Width - 2; col++)
                {
                    var point = new Point {
                        X = col, Y = row
                    };
                    var colorFtj = getFTJ(point);
                    var color    = image.GetPixel(point);
                    var r        = ComputeNewPixelColor(color.R, colorFtj.R);
                    var g        = ComputeNewPixelColor(color.G, colorFtj.G);
                    var b        = ComputeNewPixelColor(color.B, colorFtj.B);
                    var newColor = Color.FromArgb(r, g, b);
//                    var newColor = Color.FromArgb(colorFtj.R, colorFtj.G, colorFtj.B);
                    image.SetPixel(point, newColor);
                }
            }
        }
Ejemplo n.º 7
0
        private static int SAD(FastImage fastImage, int x1, int y1, int x2, int y2, int contextSize)
        {
            int sum = 0;

            for (int i = -contextSize / 2; i <= contextSize / 2; i++)
            {
                if (i + x1 < 0 || i + x1 >= fastImage.Width)
                {
                    continue;
                }

                if (i + x2 < 0 || i + x2 >= fastImage.Width)
                {
                    continue;
                }

                for (int j = -contextSize / 2; j < contextSize / 2; j++)
                {
                    if (i + y1 < 0 || i + y1 >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i + y2 < 0 || i + y2 >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    var color1 = fastImage.GetPixel(j + x1, i + y1);
                    var color2 = fastImage.GetPixel(j + x2, i + y2);
                    sum += Math.Abs(color1.R - color2.R);
                }
            }

            return(sum);
        }
Ejemplo n.º 8
0
        private static void GetConvolutionSumsForRobert(FastImage fastImage, int x, int y, int[,] matrix, out int sumRed, out int sumGreen, out int sumBlue)
        {
            sumRed   = 0;
            sumGreen = 0;
            sumBlue  = 0;

            for (int i = x; i <= x + 1; i++)
            {
                for (int j = y; j <= y + 1; j++)
                {
                    var pixel = fastImage.GetPixel(i, j);
                    sumRed   += pixel.R * matrix[i - x, j - y];
                    sumGreen += pixel.G * matrix[i - x, j - y];
                    sumBlue  += pixel.B * matrix[i - x, j - y];
                }
            }
        }
Ejemplo n.º 9
0
        private static byte CBP(FastImage fastImage, int x, int y, int contextSize, int searchRadius, int sumLimit)
        {
            var intensities = new int[256];

            for (int i = x - searchRadius; i <= x + searchRadius; i++)
            {
                if (i < 0 || i >= fastImage.Width)
                {
                    continue;
                }

                for (int j = y - searchRadius; j < y + searchRadius; j++)
                {
                    if (j < 0 || j >= fastImage.Height)
                    {
                        continue;
                    }

                    if (i == y && j == x)
                    {
                        continue;
                    }

                    if (SAD(fastImage, x, y, i, j, contextSize) < sumLimit && !SaltPepper(fastImage, i, j))
                    {
                        var color = fastImage.GetPixel(i, j);
                        intensities[color.R]++;
                    }
                }
            }

            int indexOfBiggestValue = 0;
            var max = 0;

            for (int i = 0; i < intensities.Length; i++)
            {
                if (intensities[i] > max)
                {
                    max = intensities[i];
                    indexOfBiggestValue = i;
                }
            }

            return((byte)indexOfBiggestValue);
        }
Ejemplo n.º 10
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();
        }
Ejemplo n.º 11
0
        private double ComputePixelR(double pS, double qS, Point point)
        {
            double u;

            if (qS == 0)
            {
                u = pS > 0 ? Math.PI / 2 : -Math.PI / 2;
            }
            else
            {
                u = Math.Atan(pS / qS);
                if (qS < 0)
                {
                    u += Math.PI;
                }
            }

            u += Math.PI / 2;
            var suma       = 0d;
            var sigma      = 0.66;
            var omega      = 1.5;
            var colorPoint = image.GetPixel(point);

            for (var col = point.X - 1; col <= point.X + 1; col++)
            {
                for (var row = point.Y - 1; row <= point.Y + 1; row++)
                {
                    var _point = new Point {
                        X = col, Y = row
                    };
                    var color = image.GetPixel(_point);
                    var pozr  = -point.Y - row + 1;
                    var pozc  = -point.X - col + 1;
                    var scale = Math.Pow(Math.E, -(pozr * pozr + pozc * pozc) / (2 * sigma * sigma));
                    scale = scale * Math.Sin(omega * (pozr * Math.Cos(u) + pozc * Math.Sin(u)));
                    suma += scale * colorPoint.R;
                }
            }

            return(suma);
        }
Ejemplo n.º 12
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();
        }
Ejemplo n.º 13
0
        public static long Error(FastImage a, FastImage b)
        {
            long error = 0;

            for (int x = 0; x < a.width; x++)
            {
                for (int y = 0; y < a.height; y++)
                {
                    SimpleColor old    = a.GetPixel(x, y);
                    SimpleColor srcCol = b.GetPixel(x, y);

                    error += (long)Math.Sqrt(
                        Math.Pow(old.GetR() - srcCol.GetR(), 2) +
                        Math.Pow(old.GetG() - srcCol.GetG(), 2) +
                        Math.Pow(old.GetB() - srcCol.GetB(), 2));
                }
            }

            return(error);
        }
Ejemplo n.º 14
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();
        }
Ejemplo n.º 15
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();
        }
Ejemplo n.º 16
0
        public void TestFitness(FastImage src)
        {
            f.Clear();
            _error = 0;

            foreach (Gene gene in genes)
            {
                for (int x = gene.x; x < src.width && x < gene.x + gene.sz; x++)
                {
                    for (int y = gene.y; y < src.height && y < gene.y + gene.sz; y++)
                    {
                        f.SetPixel(x, y, gene.color);
                    }
                }
            }

            for (int x = 0; x < src.width; x++)
            {
                for (int y = 0; y < src.height; y++)
                {
                    _error += src.GetPixel(x, y).Difference(f.GetPixel(x, y));
                }
            }
        }
Ejemplo n.º 17
0
        private static void GetConvolutionSumsForFreiChen(FastImage fastImage, int x, int y, List <double[, ]> matrixes, out int sumRed, out int sumGreen, out int sumBlue)
        {
            var firstSumR  = 0d;
            var firstSumG  = 0d;
            var firstSumB  = 0d;
            var secondSumR = 0d;
            var secondSumG = 0d;
            var secondSumB = 0d;

            for (var index = 0; index < matrixes.Count; index++)
            {
                var matrix = matrixes[index];

                for (int i = x; i <= x + 1; i++)
                {
                    for (int j = y; j <= y + 1; j++)
                    {
                        var pixel = fastImage.GetPixel(i, j);
                        secondSumR += Math.Pow(pixel.R * matrix[i - x, j - y], 2);
                        secondSumG += Math.Pow(pixel.G * matrix[i - x, j - y], 2);
                        secondSumB += Math.Pow(pixel.B * matrix[i - x, j - y], 2);

                        if (index < 4)
                        {
                            firstSumR += Math.Pow(pixel.R * matrix[i - x, j - y], 2);
                            firstSumG += Math.Pow(pixel.G * matrix[i - x, j - y], 2);
                            firstSumB += Math.Pow(pixel.B * matrix[i - x, j - y], 2);
                        }
                    }
                }
            }

            sumRed   = (int)(Math.Sqrt(firstSumR / secondSumR) * 255);
            sumGreen = (int)(Math.Sqrt(firstSumG / secondSumG) * 255);
            sumBlue  = (int)(Math.Sqrt(firstSumB / secondSumB) * 255);
        }
Ejemplo n.º 18
0
        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());
        }
Ejemplo n.º 19
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();
        }
Ejemplo n.º 20
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();
        }
Ejemplo n.º 21
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();
        }