コード例 #1
0
        private IDictionary <Line, double> EvalLinesAtPoint(GrayscaleStandardImage image, Point point)
        {
            int step    = 1;
            var results = new Dictionary <Line, double>();

            for (int angle = 0; angle < 180 / step; angle++)
            {
                var    line       = GetLineFromAngleAndPoint(image, point, angle);
                var    linePixels = ImageHelper.GetLinePixels(line.P1.X, line.P1.Y, line.P2.X, line.P2.Y);
                double score      = 0;
                foreach (var pixel in linePixels)
                {
                    if (image.C[pixel.X, pixel.Y] == 255)
                    {
                        score++;
                    }
                }

                score           = score / linePixels.Count;
                line.WhiteRatio = score;
                results.Add(line, score);
            }

            return(results);
        }
コード例 #2
0
 public void ShouldGetLine()
 {
     var grayImage = new GrayscaleStandardImage()
     {
         Height = 500, Width = 350
     };
     var point = new Point(175, 250);
     var line  = _documentCornersDetectionService.GetLineFromAngleAndPoint(grayImage, point, 45);
 }
コード例 #3
0
        internal static GrayscaleStandardImage CreateGrayscaleStandardImage(int width, int height)
        {
            var standardImage = new GrayscaleStandardImage
            {
                Height = height,
                Width  = width,
                C      = new int[width, height],
                Area   = new Area(0, 0, width, height)
            };

            return(standardImage);
        }
コード例 #4
0
        internal IList <Point> GetPointsOfInterest(GrayscaleStandardImage image, FormType formType, Area area)
        {
            var allForms = CornersBuilder.GetCornerForms();

            var results =
                FormSimilarityHelper.Instance.SearchForForm(allForms.Where(f => f.Type == formType).ToList(), image,
                                                            area);

            // SORT TAKE HALF BEST
            results = results.OrderByDescending(s => s.Similarity).ToList();
            results = results.Take(results.Count / 2).ToList();
            return(GroupSimilarityResults(results));
        }
コード例 #5
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage ChangeConstrast(GrayscaleStandardImage image, int intensity)
        {
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    image.C[x, y] =
                        SafeAdd(image.C[x, y],
                                (int)((double)intensity / 100 * (image.C[x, y] - 127)));
                }
            }

            return(image);
        }
コード例 #6
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage ChangeLuminosity(GrayscaleStandardImage image, int intensity)
        {
            double luminosityCoeff = 1 + (double)intensity / 100;

            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    image.C[x, y] = SafeAdd(0, (int)(image.C[x, y] * luminosityCoeff));
                }
            }

            return(image);
        }
コード例 #7
0
        internal static Bitmap ConvertToBitmap(this GrayscaleStandardImage grayScaleStandardImage)
        {
            var bitmap = new Bitmap(grayScaleStandardImage.Width, grayScaleStandardImage.Height);


            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    bitmap.SetPixel(x, y, Color.FromArgb(grayScaleStandardImage.C[x, y], grayScaleStandardImage.C[x, y], grayScaleStandardImage.C[x, y]));
                }
            }

            return(bitmap);
        }
コード例 #8
0
        public GrayscaleStandardImage UniformizeDocument(GrayscaleStandardImage image)
        {
            image = ImageHelper.Erosion(image, 0);
            image = ImageHelper.Dilatation(image, 0);
            image = ImageHelper.Erosion(image, 0);
            image = ImageHelper.Dilatation(image, 0);
            image = ImageHelper.Erosion(image, 0);
            image = ImageHelper.Dilatation(image, 0);

            //image = ImageHelper.Dilatation(image, 1);
            //image = ImageHelper.Dilatation(image, 1);
            //image = ImageHelper.Erosion(image, 0.5);

            //var maskSize = 50;

            //for (int x = maskSize; x < image.Width - maskSize; x++)
            //{
            //    for (int y = maskSize; y < image.Height - maskSize; y++)
            //    {
            //        int sum = 0;
            //        for (int i = 0; i < maskSize; i++)
            //        {
            //            for (int j = 0; j < maskSize; j++)
            //            {
            //                if (i == 0 || j == 0 || i == maskSize - 1 || j == maskSize - 1)
            //                {
            //                    sum += image.C[x - (i - maskSize / 2), y - (j - maskSize / 2)];

            //                }
            //            }
            //        }

            //        if (image.C[x, y] == 0 && (maskSize - 1) * 4 * 255 == sum)
            //        {
            //            image.C[x, y] = 255;
            //        }
            //        else if (image.C[x, y] == 255 && sum == 0)
            //        {
            //            image.C[x, y] = 0;
            //        }
            //    }
            //}

            return(image);
        }
コード例 #9
0
        internal IList <Line> GetLines(IList <Point> interests, GrayscaleStandardImage image)
        {
            Dictionary <Line, double> linesResults = new Dictionary <Line, double>();

            foreach (var result in interests)
            {
                var lines = EvalLinesAtPoint(image, result);
                foreach (var line in lines)
                {
                    linesResults.Add(line.Key, line.Value);
                }
            }

            // GROUP LINES HERE
            var groupedLines = GroupLines(linesResults.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));

            return(groupedLines);
        }
コード例 #10
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage Average(GrayscaleStandardImage image1, GrayscaleStandardImage image2)
        {
            var outputData = new GrayscaleStandardImage()
            {
                Height = image1.Height,
                Width  = image1.Width,
                C      = new int[image1.Width, image1.Height],
            };

            for (int x = 0; x < image1.Width; x++)
            {
                for (int y = 0; y < image1.Height; y++)
                {
                    outputData.C[x, y] = (image1.C[x, y] + image2.C[x, y]) / 2;
                }
            }

            return(outputData);
        }
コード例 #11
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        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);
        }
コード例 #12
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage Hat(GrayscaleStandardImage image, int hat)
        {
            for (int x = 0; x < image.Width; x++)
            {
                for (int y = 0; y < image.Height; y++)
                {
                    if (image.C[x, y] >= hat)
                    {
                        image.C[x, y] = 255;
                    }
                    else
                    {
                        image.C[x, y] = 0;
                    }
                }
            }

            return(image);
        }
コード例 #13
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);
        }
コード例 #14
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage Dilatation(GrayscaleStandardImage image, double dilatationCoeff)
        {
            var outputData = new GrayscaleStandardImage()
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
            };

            for (int x = 3; x < image.Width - 3; x++)
            {
                for (int y = 3; y < image.Height - 3; y++)
                {
                    int newValue = 0;
                    if (image.C[x, y] == 0)
                    {
                        int sum = 0;
                        for (int i = 0; i < 6; i++)
                        {
                            for (int j = 0; j < 6; j++)
                            {
                                sum += (int)(image.C[x + i - 3, y + j - 3] * (1 - dilatationCoeff));
                            }
                        }

                        if (sum > 15 * 255)
                        {
                            newValue = 255;
                        }
                    }
                    else
                    {
                        newValue = 255;
                    }

                    outputData.C[x, y] = SafeAdd(0, newValue);
                }
            }

            return(outputData);
        }
コード例 #15
0
        internal static GrayscaleStandardImage ConvertToGrayScaleStandardImage(this Bitmap bitmap)
        {
            var grayscaleImageData = new GrayscaleStandardImage
            {
                Height = bitmap.Height,
                Width  = bitmap.Width,
                C      = new int[bitmap.Width, bitmap.Height],
                Area   = new Area(0, 0, bitmap.Width, bitmap.Height)
            };

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    Color pixelColor = bitmap.GetPixel(x, y);
                    grayscaleImageData.C[x, y] = (pixelColor.R + pixelColor.G + pixelColor.B) / 3;
                }
            }

            return(grayscaleImageData);
        }
コード例 #16
0
        internal IList <Point> GetCorners(IList <Line> lines, GrayscaleStandardImage image)
        {
            ConcurrentBag <Point> bestCorners = new ConcurrentBag <Point>();
            var potentialCorners = new List <Point>();

            // GET BEST PAIR OF LINE
            for (int i = 0; i < lines.Count - 1; i++)
            {
                for (int j = i + 1; j < lines.Count; j++)
                {
                    var intersection = new LineIntersection(lines[i], lines[j]);

                    if (intersection.IntersectionPoint.X > 0 && intersection.IntersectionPoint.X < image.Width &&
                        intersection.IntersectionPoint.Y > 0 &&
                        intersection.IntersectionPoint.Y < image.Height)
                    {
                        bestCorners.Add(intersection.IntersectionPoint);
                    }
                }
            }

            return(bestCorners.ToList());
        }
コード例 #17
0
        public Line GetLineFromAngleAndPoint(GrayscaleStandardImage image, Point point, double angle)
        {
            double theta = angle * 2 * Math.PI / 360;
            double alpha = Math.Cos(theta);
            double beta  = Math.Sin(theta);
            //point = new Point(point.X, image.Height - point.Y);

            double bottomIntersect = point.X + (0 - point.Y) * alpha / beta;
            double topIntersect    = point.X + (image.Height - 1 - point.Y) * alpha / beta;
            double leftIntersect   = point.Y + (0 - point.X) * beta / alpha;
            double rightIntersect  = point.Y + (image.Width - 1 - point.X) * beta / alpha;

            List <Point> intersectionPoints = new List <Point>();

            intersectionPoints.Add(new Point((int)Math.Round(bottomIntersect), 0));
            intersectionPoints.Add(new Point((int)Math.Round(topIntersect), image.Height - 1));
            intersectionPoints.Add(new Point(0, (int)Math.Round(leftIntersect)));
            intersectionPoints.Add(new Point(image.Width - 1, (int)Math.Round(rightIntersect)));

            var innerIntersectionPoints = intersectionPoints.Where(p => p.X >= 0 && p.X < image.Width && p.Y >= 0 && p.Y < image.Height).ToList();

            return(new Line(innerIntersectionPoints[0], innerIntersectionPoints[1]));
        }
コード例 #18
0
        internal IList <Point> GetFinalCorners(GrayscaleStandardImage image, IDictionary <FormType, IList <Point> > potentialCorners)
        {
            var potentialDocumentCorners = new Dictionary <IList <Point>, double>();

            foreach (var topLeftCorner in potentialCorners[FormType.TopLeft])
            {
                foreach (var topRightCorner in potentialCorners[FormType.TopRight])
                {
                    foreach (var bottomRightCorner in potentialCorners[FormType.BottomRight])
                    {
                        foreach (var bottomLeftCorner in potentialCorners[FormType.BottomLeft])
                        {
                            var pixels = new List <Point>();
                            pixels.AddRange(ImageHelper.GetLinePixels(topLeftCorner.X, topLeftCorner.Y, topRightCorner.X, topRightCorner.Y));
                            pixels.AddRange(ImageHelper.GetLinePixels(topRightCorner.X, topRightCorner.Y, bottomRightCorner.X, bottomRightCorner.Y));
                            pixels.AddRange(ImageHelper.GetLinePixels(bottomRightCorner.X, bottomRightCorner.Y, bottomLeftCorner.X, bottomLeftCorner.Y));
                            pixels.AddRange(ImageHelper.GetLinePixels(bottomLeftCorner.X, bottomLeftCorner.Y, topLeftCorner.X, topLeftCorner.Y));

                            int whiteCount = 0;
                            foreach (var pixel in pixels)
                            {
                                if (image.C[pixel.X, pixel.Y] == 255)
                                {
                                    whiteCount++;
                                }
                            }

                            potentialDocumentCorners.Add(new List <Point> {
                                topLeftCorner, topRightCorner, bottomRightCorner, bottomLeftCorner
                            }, (double)whiteCount / pixels.Count);
                        }
                    }
                }
            }

            return(potentialDocumentCorners.Aggregate((l, r) => l.Value > r.Value ? l : r).Key);
        }
コード例 #19
0
        public IList <SimilarityResult> SearchForForm(IList <Form> forms, GrayscaleStandardImage image, Area area)
        {
            var scores = new List <SimilarityResult>();

            for (int i = area.From.X; i < area.To.X; i++)
            {
                for (int j = area.From.Y; j < area.To.Y; j++)
                {
                    if (i == 336 && j == 59)
                    {
                        int test = 0;
                    }
                    if (i == 344 && j == 122)
                    {
                        int test = 0;
                    }

                    var position = new Point(i, j);
                    if (image.C[i, j] == 255)
                    {
                        var results = new List <SimilarityResult>();
                        foreach (var form in forms)
                        {
                            results.Add(new SimilarityResult(position, EvalFormSimilarity(form, image, position)));
                        }

                        scores.Add(results.Aggregate((l, r) => l.Similarity > r.Similarity ? l : r));
                    }
                }
            }

            var orderedScores = scores.OrderByDescending(x => x.Similarity);

            var bestResult = orderedScores.First();

            return(scores);
        }
コード例 #20
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);
        }
コード例 #21
0
        public double EvalFormSimilarity(Form form, GrayscaleStandardImage image, Point position)
        {
            double score      = 0;
            int    whiteCount = 0;

            foreach (var white in form.Whites)
            {
                var whiteX = position.X + (white.X - form.Center.X);
                var whiteY = position.Y + (white.Y - form.Center.Y);

                if (whiteX >= 0 && whiteX < image.Width && whiteY >= 0 && whiteY < image.Height)
                {
                    int beginX = Math.Max(0, whiteX - Md);
                    int endX   = Math.Min(image.Width, whiteX + Md);

                    int beginY = Math.Max(0, whiteY - Md);
                    int endY   = Math.Min(image.Height, whiteY + Md);

                    var pixelScores = new List <double>();
                    for (int i = beginX; i < endX; i++)
                    {
                        for (int j = beginY; j < endY; j++)
                        {
                            pixelScores.Add((image.C[i, j] * _detectionMask[i - whiteX + Md, j - whiteY + Md] / 255));
                        }
                    }

                    score += pixelScores.Max();
                }
            }


            score = score / form.Whites.Count;

            return(score);
        }
コード例 #22
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        internal static GrayscaleStandardImage Laplacien(GrayscaleStandardImage image)
        {
            var matrice = new[, ]
            {
                { 0, -1, 0 },
                { -1, 4, -1 },
                { 0, -1, 0 }
            };


            var outputData = new GrayscaleStandardImage()
            {
                Height = image.Height,
                Width  = image.Width,
                C      = new int[image.Width, image.Height],
            };

            for (int x = 1; x < image.Width - 1; x++)
            {
                for (int y = 1; y < image.Height - 1; y++)
                {
                    int valueC = 0;
                    for (int i = 0; i < 3; i++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            valueC += image.C[x + i - 1, y + j - 1] * matrice[i, j];
                        }
                    }

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

            return(outputData);
        }
コード例 #23
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        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);
        }
コード例 #24
0
ファイル: ImageHelper.cs プロジェクト: Suprndm/nvision
        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);
        }