Exemple #1
0
        internal static GrayscaleStandardImage ExactHat(StandardImage image, Color colorHat, int roundness)
        {
            var resultImage = image.ConvertToGrayScaleStandardImage();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    if (image.R[x, y] >= colorHat.R - roundness &&
                        image.G[x, y] >= colorHat.G - roundness &&
                        image.B[x, y] >= colorHat.B - roundness &&
                        image.R[x, y] <= colorHat.R + roundness &&
                        image.G[x, y] <= colorHat.G + roundness &&
                        image.B[x, y] <= colorHat.B + roundness)
                    {
                        resultImage.C[x, y] = 255;
                    }
                    else
                    {
                        resultImage.C[x, y] = 0;
                    }
                }
            }

            return(resultImage);
        }
Exemple #2
0
        internal static IDictionary <int, int> GetHueMAp(StandardImage image)
        {
            var map = new Dictionary <int, int>();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    var myRgb = new Rgb {
                        R = image.R[x, y], G = image.G[x, y], B = image.B[x, y]
                    };
                    var hsl = myRgb.To <Hsv>();
                    var hue = (int)(hsl.H / 3.6);

                    if (map.ContainsKey(hue))
                    {
                        map[hue]++;
                    }
                    else
                    {
                        map.Add(hue, 1);
                    }
                }
            }

            return(map);
        }
Exemple #3
0
        internal static IDictionary <Point, int> GetSVMap(StandardImage image)
        {
            var map = new Dictionary <Point, int>();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    var myRgb = new Rgb {
                        R = image.R[x, y], G = image.G[x, y], B = image.B[x, y]
                    };
                    var hsl        = myRgb.To <Hsv>();
                    var saturation = (int)(hsl.S * 100);
                    var brightness = (int)(hsl.V * 100);
                    var point      = new Point(saturation, brightness);

                    if (map.ContainsKey(point))
                    {
                        map[point]++;
                    }
                    else
                    {
                        map.Add(point, 1);
                    }
                }
            }

            return(map);
        }
Exemple #4
0
        public GrayscaleStandardImage IsolateDocument(StandardImage image, IDictionary <Point, int> svPikes)
        {
            //var documentColor = GetDocumentColor(image);
            var brightessEdges  = ImageHelper.BrightnessLaplacien(image);
            var saturationEdges = ImageHelper.SaturationLaplacien(image);

            var grayImage = ImageHelper.Average(brightessEdges, saturationEdges);

            grayImage = ImageHelper.Hat(grayImage, 80);


            grayImage = ImageHelper.Erosion(grayImage, 0);
            grayImage = ImageHelper.Dilatation(grayImage, 0);
            grayImage = ImageHelper.Erosion(grayImage, 0);
            grayImage = ImageHelper.Dilatation(grayImage, 0);
            grayImage = ImageHelper.Erosion(grayImage, 0);
            grayImage = ImageHelper.Dilatation(grayImage, 0);
            grayImage = ImageHelper.Erosion(grayImage, 0);

            //grayImage = ImageHelper.Laplacien(grayImage);
            //grayImage = ImageHelper.Hat(grayImage, 10);

            //  grayImage = ImageHelper.Hat(grayImage, 10);
            // grayImage = UniformizeDocument(grayImage);

            return(grayImage);
        }
Exemple #5
0
        public Color GetDocumentColor(StandardImage image)
        {
            var colors = new Dictionary <Color, int>();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    Color pixelColor = Color.FromArgb(1, image.R[x, y], image.G[x, y], image.B[x, y]);

                    if (colors.ContainsKey(pixelColor))
                    {
                        colors[pixelColor]++;
                    }
                    else
                    {
                        colors.Add(pixelColor, 1);
                    }
                }
            }
            List <KeyValuePair <Color, int> > lightPopularity = new List <KeyValuePair <Color, int> >();

            foreach (var kv in colors)
            {
                var color = kv.Key;
                lightPopularity.Add(new KeyValuePair <Color, int>(kv.Key, kv.Value * (color.R + color.G + color.B)));
            }

            lightPopularity.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
            var lightPopuplarColor = lightPopularity.First().Key;
            int roundness          = 20;
            int pixelCount         = 0;
            int r = 0;
            int g = 0;
            int b = 0;

            foreach (var kv in lightPopularity)
            {
                var color = kv.Key;

                if (color.R >= lightPopuplarColor.R - roundness &&
                    color.G >= lightPopuplarColor.G - roundness &&
                    color.B >= lightPopuplarColor.B - roundness &&
                    color.R <= lightPopuplarColor.R + roundness &&
                    color.G <= lightPopuplarColor.G + roundness &&
                    color.B <= lightPopuplarColor.B + roundness)
                {
                    var colorOccurence = colors[color];
                    r += kv.Key.R * colorOccurence;
                    g += kv.Key.G * colorOccurence;
                    b += kv.Key.B * colorOccurence;

                    pixelCount += colorOccurence;
                }
            }

            return(Color.FromArgb(255, r / pixelCount, g / pixelCount, b / pixelCount));
        }
Exemple #6
0
        public GrayscaleStandardImage SvPikeHat(StandardImage image, IDictionary <Point, int> svPikes)
        {
            var grayImage        = image.ConvertToGrayScaleStandardImage();
            var saturationCutoff = 0;
            var brightnessCutoff = 0;

            if (svPikes.Count == 1)
            {
                saturationCutoff = svPikes.Single().Key.X + 30;
                brightnessCutoff = svPikes.Single().Key.Y - 30;
            }
            else if (svPikes.Count == 2)
            {
                saturationCutoff = (svPikes.First().Key.X + svPikes.Last().Key.X) / 2;
                brightnessCutoff = (svPikes.First().Key.Y + svPikes.Last().Key.Y) / 2;
            }
            else
            {
                var pikesList = svPikes.ToList();
                pikesList.Sort((pair1, pair2) => pair2.Value.CompareTo(pair1.Value));
                var pike1 = pikesList[0];
                var pike2 = pikesList[1];
                saturationCutoff = (pike1.Key.X + pike2.Key.X) / 2;
                brightnessCutoff = (pike1.Key.Y + pike2.Key.Y) / 2;
            }

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    var myRgb = new Rgb {
                        R = image.R[i, j], G = image.G[i, j], B = image.B[i, j]
                    };
                    var hsl = myRgb.To <Hsv>();

                    var saturation = hsl.S * 100;
                    if (saturation > 13 && saturation < 40)
                    {
                        grayImage.C[i, j] = 255;
                    }
                    else
                    {
                        grayImage.C[i, j] = 0;
                    }
                    //var saturation = hsl.S*100;
                    //var brightness = hsl.V*100;
                    //if (saturation < saturationCutoff && brightness  > brightnessCutoff)
                    //    grayImage.C[i, j] = 255;
                    //else
                    //    grayImage.C[i, j] = 0;
                }
            }

            return(grayImage);
        }
Exemple #7
0
        internal static StandardImage DrawPixels(this StandardImage image, IList <Point> pixels, Color color)
        {
            foreach (var pixel in pixels)
            {
                image.R[pixel.X, pixel.Y] = color.R;
                image.G[pixel.X, pixel.Y] = color.G;
                image.B[pixel.X, pixel.Y] = color.B;
            }

            return(image);
        }
Exemple #8
0
        internal static StandardImage Round(this StandardImage image, int precision)
        {
            for (int x = 1; x < image.Width - 1; x++)
            {
                for (int y = 1; y < image.Height - 1; y++)
                {
                    image.B[x, y] = (image.B[x, y] / precision) * precision;
                    image.G[x, y] = (image.G[x, y] / precision) * precision;
                    image.B[x, y] = (image.B[x, y] / precision) * precision;
                }
            }

            return(image);
        }
Exemple #9
0
        internal static StandardImage CreateStandardImage(int width, int height)
        {
            var standardImage = new StandardImage()
            {
                Height = height,
                Width  = width,
                R      = new int[width, height],
                G      = new int[width, height],
                B      = new int[width, height],
                Area   = new Area(0, 0, width, height)
            };

            return(standardImage);
        }
Exemple #10
0
        internal static Bitmap ConvertToBitmap(this StandardImage standardImage)
        {
            var bitmap = new Bitmap(standardImage.Width, standardImage.Height);


            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    bitmap.SetPixel(x, y, Color.FromArgb(standardImage.R[x, y], standardImage.G[x, y], standardImage.B[x, y]));
                }
            }

            return(bitmap);
        }
Exemple #11
0
        public StandardImage StraightenDocument(StandardImage image, IList <Point> points)
        {
            double[]      system = RotationHelper.GetSystem(points.ToArray());
            var           size = RotationHelper.GetOriginalDimensions(points);
            int           W = size.Width, H = size.Height;
            StandardImage target = ImageStandardizer.CreateStandardImage(W, H);

            // pour chaque pixel (x,y) de l'image corrigée
            for (int y = 0; y < H; y++)
            {
                for (int x = 0; x < W; x++)
                {
                    // conversion dans le repère orthonormé (u,v) [0,1]x[0,1]
                    double u = (double)x / W;
                    double v = (double)y / H;

                    // passage dans le repère perspective
                    double[] P = RotationHelper.Invert(u, v, system);


                    // copie du pixel (px,py) correspondant de l'image source
                    // TODO: faire une interpolation
                    int px     = (int)Math.Round(P[0]);
                    int py     = (int)Math.Round(P[1]);
                    int colorR = 0;
                    int colorG = 0;
                    int colorB = 0;

                    if (px < 0 || px >= image.Width || py < 0 || py >= image.Height)
                    {
                        colorR = 0;
                        colorG = 0;
                        colorB = 0;
                    }
                    else
                    {
                        colorR = image.R[px, py];
                        colorG = image.G[px, py];
                        colorB = image.B[px, py];
                    }

                    target.R[x, y] = colorR;
                    target.G[x, y] = colorG;
                    target.B[x, y] = colorB;
                }
            }
            return(target);
        }
Exemple #12
0
        public GrayscaleStandardImage DocumentEligibilityMap(StandardImage image)
        {
            var svMap     = ImageHelper.GetSVMap(image);
            var pike      = ImageHelper.GetDocumentPike(svMap);
            var grayImage = image.ConvertToGrayScaleStandardImage();

            for (int i = 0; i < image.Width; i++)
            {
                for (int j = 0; j < image.Height; j++)
                {
                    Color pixelColor = Color.FromArgb(1, image.R[i, j], image.G[i, j], image.B[i, j]);

                    grayImage.C[i, j] = pixelColor.IsInPike(pike) ? 255 : 0;
                }
            }

            return(grayImage);
        }
Exemple #13
0
        GrayscaleStandardImage GetDocumentEdges(StandardImage image)
        {
            var outputData = new GrayscaleStandardImage()
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
            };

            var matrice = new[, ]
            {
                { 0, -1, 0 },
                { -1, 4, -1 },
                { 0, -1, 0 }
            };
            int scanSize = 3;
            int half     = (scanSize - 1) / 2;

            for (int x = half; x < image.Width - half; x++)
            {
                for (int y = half; y < image.Height - half; y++)
                {
                    var color1 = Color.FromArgb(255, image.R[x, y], image.R[x, y], image.R[x, y]);
                    int valueC = 0;
                    for (int i = 0; i < scanSize; i++)
                    {
                        for (int j = 0; j < scanSize; j++)
                        {
                            var color2 = Color.FromArgb(255, image.R[x + i - half, y + j - half], image.R[x + i - half, y + j - half], image.R[x + i - half, y + j - half]);

                            valueC += (int)GetDistanceBetweenColors(color1, color2);
                        }
                    }

                    if (CouldBeDocumentColor(color1))
                    {
                        outputData.C[x, y] = SafeAdd(0, valueC);
                    }
                }
            }

            return(outputData);
        }
Exemple #14
0
        internal static StandardImage DrawIndicator(this StandardImage image, int x, int y, int size, Color color)
        {
            for (int i = -size; i < size; i++)
            {
                for (int j = -size; j < size; j++)
                {
                    var posX = x + i;
                    var posY = y + j;
                    if (posX > 0 && posX < image.Width && posY > 0 && posY < image.Height)
                    {
                        image.R[posX, posY] = color.R;
                        image.G[posX, posY] = color.G;
                        image.B[posX, posY] = color.B;
                    }
                }
            }

            return(image);
        }
Exemple #15
0
        internal static GrayscaleStandardImage ConvertToGrayScaleStandardImage(this StandardImage image)
        {
            var standardImage = new GrayscaleStandardImage
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
                Area   = image.Area
            };

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    standardImage.C[x, y] = (image.R[x, y] + image.G[x, y] + image.B[x, y]) / 3;
                }
            }

            return(standardImage);
        }
Exemple #16
0
 public IOperationResult LoadIfNeeded(StandardImage image)
 {
     return((image.Id != 0
     ? _dataStorage.GetStandardImage(image.Id)
     : _dataStorage.GetStandardImagesIds(ImageType.General)
             .Bind(ids => OperationResult.Try(() =>
     {
         if (ids.Length == 0)
         {
             return OperationResult.Failure <StandardImage>(new Exception("No images found in collection."));
         }
         var randomId = ids[_random.Next(0, ids.Length)];
         return _dataStorage.GetStandardImage(randomId);
     })))
            .Bind(storedImage =>
     {
         image.ImageBase64 = storedImage.ImageBase64;
         return OperationResult.Success();
     }));
 }
Exemple #17
0
        public Bitmap PrepareImageToOcr(Bitmap bitmap)
        {
            //bitmap = bitmap.ReduceSize((double)500 / Math.Max(bitmap.Width, bitmap.Height));
            //var standardImage = bitmap.ConvertToStandardImage();
            //var grayImage = _documentPreparationService.DocumentEligibilityMap(standardImage);

            //var corners = _documentCornersDetectionService.GetCorners(grayImage);

            //var coloredStandardImage = grayImage.ConvertToStandardImage();

            ////foreach (var point in corners)
            ////{
            ////    coloredStandardImage = coloredStandardImage.DrawIndicator(point.X, point.Y, 2);
            ////}

            StandardImage rotatedImage = null;// _documentStraightenerService.StraightenDocument(standardImage, corners);

            var result = rotatedImage.ConvertToBitmap();

            return(result);
        }
Exemple #18
0
        internal static GrayscaleStandardImage Hat(StandardImage image, Color colorHat)
        {
            var resultImage = image.ConvertToGrayScaleStandardImage();

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    if (image.R[x, y] > colorHat.R &&
                        image.G[x, y] > colorHat.G &&
                        image.B[x, y] > colorHat.B)
                    {
                        resultImage.C[x, y] = 255;
                    }
                    else
                    {
                        resultImage.C[x, y] = 0;
                    }
                }
            }

            return(resultImage);
        }
Exemple #19
0
        internal static StandardImage ConvertToStandardImage(this GrayscaleStandardImage image)
        {
            var standardImage = new StandardImage
            {
                Height = image.Height,
                Width  = image.Width,
                R      = new int[image.Width, image.Height],
                G      = new int[image.Width, image.Height],
                B      = new int[image.Width, image.Height],
                Area   = image.Area
            };

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    standardImage.R[x, y] = image.C[x, y];
                    standardImage.G[x, y] = image.C[x, y];
                    standardImage.B[x, y] = image.C[x, y];
                }
            }

            return(standardImage);
        }
Exemple #20
0
        internal static StandardImage ConvertToStandardImage(this Bitmap bitmap, int overlay = 0)
        {
            var imageData = new StandardImage
            {
                Height = bitmap.Height + overlay,
                Width  = bitmap.Width + overlay,
                R      = new int[bitmap.Width + overlay, bitmap.Height + overlay],
                G      = new int[bitmap.Width + overlay, bitmap.Height + overlay],
                B      = new int[bitmap.Width + overlay, bitmap.Height + overlay],
                Area   = new Area(0, 0, bitmap.Width + overlay, bitmap.Height + overlay)
            };

            for (int x = -overlay / 2; x < bitmap.Width + overlay / 2; x++)
            {
                for (int y = -overlay / 2; y < bitmap.Height + overlay / 2; y++)
                {
                    Color pixelColor = Color.DarkBlue;
                    if (x < 0 && (y >= 0 && y < bitmap.Height))
                    {
                        pixelColor = bitmap.GetPixel(0, y);
                    }
                    else if (y < 0 / 2 && (x >= 0 && x < bitmap.Width))
                    {
                        pixelColor = bitmap.GetPixel(x, 0);
                    }
                    else if (x >= bitmap.Width && (y >= 0 && y < bitmap.Height))
                    {
                        pixelColor = bitmap.GetPixel(bitmap.Width - 1, y);
                    }
                    else if (y >= bitmap.Height && (x >= 0 && x < bitmap.Width))
                    {
                        pixelColor = bitmap.GetPixel(x, bitmap.Height - 1);
                    }
                    else if (x < 0 && y < 0)
                    {
                        pixelColor = bitmap.GetPixel(0, 0);
                    }
                    else if (x < 0 && y >= bitmap.Height)
                    {
                        pixelColor = bitmap.GetPixel(0, bitmap.Height - 1);
                    }

                    else if (x >= bitmap.Width && y < 0)
                    {
                        pixelColor = bitmap.GetPixel(bitmap.Width - 1, 0);
                    }
                    else if (x >= bitmap.Width && y >= bitmap.Height)
                    {
                        pixelColor = bitmap.GetPixel(bitmap.Width - 1, bitmap.Height - 1);
                    }
                    else
                    {
                        pixelColor = bitmap.GetPixel(x, y);
                    }

                    imageData.R[x + overlay / 2, y + overlay / 2] = pixelColor.R;
                    imageData.G[x + overlay / 2, y + overlay / 2] = pixelColor.G;
                    imageData.B[x + overlay / 2, y + overlay / 2] = pixelColor.B;
                }
            }

            return(imageData);
        }
Exemple #21
0
        GrayscaleStandardImage BrightnessLaplacien(StandardImage image)
        {
            var outputData = new GrayscaleStandardImage()
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
            };

            var dist       = 8;
            var mediumDist = 2;
            var smallDist  = 1;

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    double sx1 = 0;
                    double sx2 = 0;
                    if (x - dist > 0 && x + dist < image.Width)
                    {
                        sx1 = (new Rgb {
                            R = image.R[x - dist, y], G = image.G[x - dist, y], B = image.B[x - dist, y]
                        }).To <Hsv>().V * 100;
                        sx2 = (new Rgb {
                            R = image.R[x + dist, y], G = image.G[x + dist, y], B = image.B[x + dist, y]
                        }).To <Hsv>().V * 100;
                    }

                    double sy1 = 0;
                    double sy2 = 0;
                    if (y - dist > 0 && y + dist < image.Height)
                    {
                        sy1 = (new Rgb {
                            R = image.R[x, y - dist], G = image.G[x, y - dist], B = image.B[x, y - dist]
                        }).To <Hsv>().V * 100;
                        sy2 = (new Rgb {
                            R = image.R[x, y + dist], G = image.G[x, y + dist], B = image.B[x, y + dist]
                        }).To <Hsv>().V * 100;
                    }

                    double sdiag1 = 0;
                    double sdiag2 = 0;
                    if (y - dist > 0 && y + dist < image.Height && x - dist > 0 && x + dist < image.Width)
                    {
                        sdiag1 = (new Rgb {
                            R = image.R[x - dist, y - dist], G = image.G[x - dist, y - dist], B = image.B[x - dist, y - dist]
                        }).To <Hsv>().V * 100;
                        sdiag2 = (new Rgb {
                            R = image.R[x + dist, y + dist], G = image.G[x + dist, y + dist], B = image.B[x + dist, y + dist]
                        }).To <Hsv>().V * 100;
                    }

                    double sdiag3 = 0;
                    double sdiag4 = 0;
                    if (y - dist > 0 && y + dist < image.Height && x - dist > 0 && x + dist < image.Width)
                    {
                        sdiag3 = (new Rgb {
                            R = image.R[x + dist, y - dist], G = image.G[x + dist, y - dist], B = image.B[x + dist, y - dist]
                        }).To <Hsv>().V * 100;
                        sdiag4 = (new Rgb {
                            R = image.R[x - dist, y + dist], G = image.G[x - dist, y + dist], B = image.B[x - dist, y + dist]
                        }).To <Hsv>().V * 100;
                    }

                    double ssx1 = 0;
                    double ssx2 = 0;
                    if (x - smallDist > 0 && x + smallDist < image.Width)
                    {
                        ssx1 = (new Rgb {
                            R = image.R[x - smallDist, y], G = image.G[x - smallDist, y], B = image.B[x - smallDist, y]
                        }).To <Hsv>().V * 100;
                        ssx2 = (new Rgb {
                            R = image.R[x + smallDist, y], G = image.G[x + smallDist, y], B = image.B[x + smallDist, y]
                        }).To <Hsv>().V * 100;
                    }

                    double ssy1 = 0;
                    double ssy2 = 0;
                    if (y - smallDist > 0 && y + smallDist < image.Height)
                    {
                        ssy1 = (new Rgb {
                            R = image.R[x, y - smallDist], G = image.G[x, y - smallDist], B = image.B[x, y - smallDist]
                        }).To <Hsv>().V * 100;
                        ssy2 = (new Rgb {
                            R = image.R[x, y + smallDist], G = image.G[x, y + smallDist], B = image.B[x, y + smallDist]
                        }).To <Hsv>().V * 100;
                    }

                    double ssdiag1 = 0;
                    double ssdiag2 = 0;
                    if (y - smallDist > 0 && y + smallDist < image.Height && x - smallDist > 0 && x + smallDist < image.Width)
                    {
                        ssdiag1 = (new Rgb {
                            R = image.R[x - smallDist, y - smallDist], G = image.G[x - smallDist, y - smallDist], B = image.B[x - smallDist, y - smallDist]
                        }).To <Hsv>().V * 100;
                        ssdiag2 = (new Rgb {
                            R = image.R[x + smallDist, y + smallDist], G = image.G[x + smallDist, y + smallDist], B = image.B[x + smallDist, y + smallDist]
                        }).To <Hsv>().V * 100;
                    }

                    double ssdiag3 = 0;
                    double ssdiag4 = 0;
                    if (y - smallDist > 0 && y + smallDist < image.Height && x - smallDist > 0 && x + smallDist < image.Width)
                    {
                        ssdiag3 = (new Rgb {
                            R = image.R[x + smallDist, y - smallDist], G = image.G[x + smallDist, y - smallDist], B = image.B[x + smallDist, y - smallDist]
                        }).To <Hsv>().V * 100;
                        ssdiag4 = (new Rgb {
                            R = image.R[x - smallDist, y + smallDist], G = image.G[x - smallDist, y + smallDist], B = image.B[x - smallDist, y + smallDist]
                        }).To <Hsv>().V * 100;
                    }

                    double msx1 = 0;
                    double msx2 = 0;
                    if (x - mediumDist > 0 && x + mediumDist < image.Width)
                    {
                        msx1 = (new Rgb {
                            R = image.R[x - mediumDist, y], G = image.G[x - mediumDist, y], B = image.B[x - mediumDist, y]
                        }).To <Hsv>().V * 100;
                        msx2 = (new Rgb {
                            R = image.R[x + mediumDist, y], G = image.G[x + mediumDist, y], B = image.B[x + mediumDist, y]
                        }).To <Hsv>().V * 100;
                    }

                    double msy1 = 0;
                    double msy2 = 0;
                    if (y - mediumDist > 0 && y + mediumDist < image.Height)
                    {
                        msy1 = (new Rgb {
                            R = image.R[x, y - mediumDist], G = image.G[x, y - mediumDist], B = image.B[x, y - mediumDist]
                        }).To <Hsv>().V * 100;
                        msy2 = (new Rgb {
                            R = image.R[x, y + mediumDist], G = image.G[x, y + mediumDist], B = image.B[x, y + mediumDist]
                        }).To <Hsv>().V * 100;
                    }

                    double msdiag1 = 0;
                    double msdiag2 = 0;
                    if (y - mediumDist > 0 && y + mediumDist < image.Height && x - mediumDist > 0 && x + mediumDist < image.Width)
                    {
                        msdiag1 = (new Rgb {
                            R = image.R[x - mediumDist, y - mediumDist], G = image.G[x - mediumDist, y - mediumDist], B = image.B[x - mediumDist, y - mediumDist]
                        }).To <Hsv>().V * 100;
                        msdiag2 = (new Rgb {
                            R = image.R[x + mediumDist, y + mediumDist], G = image.G[x + mediumDist, y + mediumDist], B = image.B[x + mediumDist, y + mediumDist]
                        }).To <Hsv>().V * 100;
                    }

                    double msdiag3 = 0;
                    double msdiag4 = 0;
                    if (y - mediumDist > 0 && y + mediumDist < image.Height && x - mediumDist > 0 && x + mediumDist < image.Width)
                    {
                        msdiag3 = (new Rgb {
                            R = image.R[x + mediumDist, y - mediumDist], G = image.G[x + mediumDist, y - mediumDist], B = image.B[x + mediumDist, y - mediumDist]
                        }).To <Hsv>().V * 100;
                        msdiag4 = (new Rgb {
                            R = image.R[x - mediumDist, y + mediumDist], G = image.G[x - mediumDist, y + mediumDist], B = image.B[x - mediumDist, y + mediumDist]
                        }).To <Hsv>().V * 100;
                    }

                    var results = new List <double>();

                    results.Add(Math.Abs(sx1 - sx2) * Math.Abs(ssx1 - ssx2) * Math.Abs(msx1 - msx2));
                    results.Add(Math.Abs(sy1 - sy2) * Math.Abs(ssy1 - ssy2) * Math.Abs(msy1 - msy2));
                    results.Add(Math.Abs(sdiag1 - sdiag2) * Math.Abs(ssdiag1 - ssdiag2) * Math.Abs(msdiag1 - msdiag2));
                    results.Add(Math.Abs(sdiag3 - sdiag4) * Math.Abs(ssdiag3 - ssdiag4) * Math.Abs(msdiag3 - msdiag4));


                    outputData.C[x, y] = SafeAdd(0, (int)results.Max() / 20);
                }
            }

            return(outputData);
        }
Exemple #22
0
        internal static GrayscaleStandardImage SaturationLaplacien(StandardImage image)
        {
            var outputData = new GrayscaleStandardImage()
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
            };

            var dist       = 4;
            var mediumDist = 2;
            var smallDist  = 1;

            for (int x = dist; x < image.Width - dist; x++)
            {
                for (int y = dist; y < image.Height - dist; y++)
                {
                    var sx1 = (new Rgb {
                        R = image.R[x - dist, y], G = image.G[x - dist, y], B = image.B[x - dist, y]
                    }).To <Hsv>().S * 100;
                    var sx2 = (new Rgb {
                        R = image.R[x + dist, y], G = image.G[x + dist, y], B = image.B[x + dist, y]
                    }).To <Hsv>().S * 100;

                    var sy1 = (new Rgb {
                        R = image.R[x, y - dist], G = image.G[x, y - dist], B = image.B[x, y - dist]
                    }).To <Hsv>().S * 100;
                    var sy2 = (new Rgb {
                        R = image.R[x, y + dist], G = image.G[x, y + dist], B = image.B[x, y + dist]
                    }).To <Hsv>().S * 100;

                    var sdiag1 = (new Rgb {
                        R = image.R[x - dist, y - dist], G = image.G[x - dist, y - dist], B = image.B[x - dist, y - dist]
                    }).To <Hsv>().S * 100;
                    var sdiag2 = (new Rgb {
                        R = image.R[x + dist, y + dist], G = image.G[x + dist, y + dist], B = image.B[x + dist, y + dist]
                    }).To <Hsv>().S * 100;

                    var sdiag3 = (new Rgb {
                        R = image.R[x + dist, y - dist], G = image.G[x + dist, y - dist], B = image.B[x + dist, y - dist]
                    }).To <Hsv>().S * 100;
                    var sdiag4 = (new Rgb {
                        R = image.R[x - dist, y + dist], G = image.G[x - dist, y + dist], B = image.B[x - dist, y + dist]
                    }).To <Hsv>().S * 100;

                    var ssx1 = (new Rgb {
                        R = image.R[x - smallDist, y], G = image.G[x - smallDist, y], B = image.B[x - smallDist, y]
                    }).To <Hsv>().S * 100;
                    var ssx2 = (new Rgb {
                        R = image.R[x + smallDist, y], G = image.G[x + smallDist, y], B = image.B[x + smallDist, y]
                    }).To <Hsv>().S * 100;

                    var ssy1 = (new Rgb {
                        R = image.R[x, y - smallDist], G = image.G[x, y - smallDist], B = image.B[x, y - smallDist]
                    }).To <Hsv>().S * 100;
                    var ssy2 = (new Rgb {
                        R = image.R[x, y + smallDist], G = image.G[x, y + smallDist], B = image.B[x, y + smallDist]
                    }).To <Hsv>().S * 100;

                    var ssdiag1 = (new Rgb {
                        R = image.R[x - smallDist, y - smallDist], G = image.G[x - smallDist, y - smallDist], B = image.B[x - smallDist, y - smallDist]
                    }).To <Hsv>().S * 100;
                    var ssdiag2 = (new Rgb {
                        R = image.R[x + smallDist, y + smallDist], G = image.G[x + smallDist, y + smallDist], B = image.B[x + smallDist, y + smallDist]
                    }).To <Hsv>().S * 100;

                    var ssdiag3 = (new Rgb {
                        R = image.R[x + smallDist, y - smallDist], G = image.G[x + smallDist, y - smallDist], B = image.B[x + smallDist, y - smallDist]
                    }).To <Hsv>().S * 100;
                    var ssdiag4 = (new Rgb {
                        R = image.R[x - smallDist, y + smallDist], G = image.G[x - smallDist, y + smallDist], B = image.B[x - smallDist, y + smallDist]
                    }).To <Hsv>().S * 100;

                    var msx1 = (new Rgb {
                        R = image.R[x - mediumDist, y], G = image.G[x - mediumDist, y], B = image.B[x - mediumDist, y]
                    }).To <Hsv>().S * 100;
                    var msx2 = (new Rgb {
                        R = image.R[x + mediumDist, y], G = image.G[x + mediumDist, y], B = image.B[x + mediumDist, y]
                    }).To <Hsv>().S * 100;

                    var msy1 = (new Rgb {
                        R = image.R[x, y - mediumDist], G = image.G[x, y - mediumDist], B = image.B[x, y - mediumDist]
                    }).To <Hsv>().S * 100;
                    var msy2 = (new Rgb {
                        R = image.R[x, y + mediumDist], G = image.G[x, y + mediumDist], B = image.B[x, y + mediumDist]
                    }).To <Hsv>().S * 100;

                    var msdiag1 = (new Rgb {
                        R = image.R[x - mediumDist, y - mediumDist], G = image.G[x - mediumDist, y - mediumDist], B = image.B[x - mediumDist, y - mediumDist]
                    }).To <Hsv>().S * 100;
                    var msdiag2 = (new Rgb {
                        R = image.R[x + mediumDist, y + mediumDist], G = image.G[x + mediumDist, y + mediumDist], B = image.B[x + mediumDist, y + mediumDist]
                    }).To <Hsv>().S * 100;

                    var msdiag3 = (new Rgb {
                        R = image.R[x + mediumDist, y - mediumDist], G = image.G[x + mediumDist, y - mediumDist], B = image.B[x + mediumDist, y - mediumDist]
                    }).To <Hsv>().S * 100;
                    var msdiag4 = (new Rgb {
                        R = image.R[x - mediumDist, y + mediumDist], G = image.G[x - mediumDist, y + mediumDist], B = image.B[x - mediumDist, y + mediumDist]
                    }).To <Hsv>().S * 100;
                    var results = new List <double>();

                    results.Add(Math.Abs(sx1 - sx2) * Math.Abs(ssx1 - ssx2) * Math.Abs(msx1 - msx2));
                    results.Add(Math.Abs(sy1 - sy2) * Math.Abs(ssy1 - ssy2) * Math.Abs(msy1 - msy2));
                    results.Add(Math.Abs(sdiag1 - sdiag2) * Math.Abs(ssdiag1 - ssdiag2) * Math.Abs(msdiag1 - msdiag2));
                    results.Add(Math.Abs(sdiag3 - sdiag4) * Math.Abs(ssdiag3 - ssdiag4) * Math.Abs(msdiag3 - msdiag4));


                    outputData.C[x, y] = SafeAdd(0, (int)results.Max() / 20);
                }
            }

            return(outputData);
        }