Пример #1
0
        public Bitmap ApplyMethod(Bitmap src)
        {
            var width  = src.Width;
            var height = src.Height;

            var otsu          = Otsu.ApplyOtsu(src);
            var erosionMethod = new Erosion();
            var erosion       = erosionMethod.ApplyErosion(otsu);
            var result        = new Bitmap(width, height);

            for (var i = 0; i < height; ++i)
            {
                for (var j = 0; j < width; ++j)
                {
                    if (erosion.GetPixel(j, i) != otsu.GetPixel(j, i))
                    {
                        result.SetPixel(j, i, Color.White);
                    }
                }
            }
            for (var i = 2; i < height - 2; ++i)
            {
                for (var j = 2; j < width - 2; ++j)
                {
                    if (result.GetPixel(j, i).R != 0)
                    {
                        dots.Add(new Point(j, i));
                    }
                }
            }

            for (var i = 0; i < dots.Count; i += 50)
            {
                var counts = new Dictionary <int, int>();
                for (var j = -i; j < dots.Count; j++)
                {
                    if (i + j < dots.Count && i + j != i && Math.Abs(dots[i].X - dots[j + i].X) < 25 && Math.Abs(dots[i].Y - dots[j + i].Y) < 25)
                    {
                        var neighbor = dots[i + j];
                        counts[j + i] = CountingVotes(dots[i], neighbor);
                    }
                }

                var greenPen = new Pen(Color.Green, 3);
                if (counts.Count == 0)
                {
                    continue;
                }
                var max = counts.Max(x => x.Value);
                if (max == 0)
                {
                    continue;
                }
                using (var graphics = Graphics.FromImage(result))
                {
                    var index  = counts.First(x => x.Value == max).Key;
                    var points = GetPoints(dots[i], dots[index], result);

                    if (points.Count > 0)
                    {
                        var last  = points.Last();
                        var first = points.First();

                        if (last.Y < first.Y)
                        {
                            Point temp = last;
                            last  = first;
                            first = temp;
                        }

                        double tg = (last.X - first.X) != 0 ? (double)(Math.Abs(last.Y - first.Y)) / Math.Abs(last.X - first.X) : double.MaxValue;
                        if ((last.X - first.X) != 0 && ((tg == 0 && (last.X - first.X) > 12) ||
                                                        Math.Abs(tg - Math.Sqrt(3)) < 0.4))
                        {
                            graphics.DrawLine(greenPen, first, last);
                            _lineSegments[first] = last;
                        }
                    }
                }
            }

            var ind             = 0;
            var prevYHorisontal = 0;
            var prev            = new Point();

            foreach (var point in _lineSegments.Keys)
            {
                if ((ind == 0 || (point.Y - prev.Y > 10)) && ((point.Y - _lineSegments[point].Y) / (point.X - _lineSegments[point].X) == 0) &&
                    (point.X - _lineSegments[point].X) < 0)
                {
                    var yellowPen = new Pen(Color.Yellow, 3);
                    using (var graphics = Graphics.FromImage(src))
                    {
                        if (ind == 0)
                        {
                            var firstGreenY = FindFirstGreen(result);
                            graphics.DrawRectangle(yellowPen, new Rectangle(new Point(point.X - 20, firstGreenY - 10), new Size(100, point.Y - firstGreenY + 10)));
                            prevYHorisontal = point.Y;
                        }
                        else
                        {
                            graphics.DrawRectangle(yellowPen, new Rectangle(new Point(point.X - 40, prevYHorisontal), new Size(75, point.Y - prevYHorisontal + 10)));
                        }
                        ind++;
                    }
                }
                prev = point;
            }
            return(src);
        }
Пример #2
0
        public Bitmap ApplyMethod(Bitmap src)
        {
            var width  = src.Width;
            var height = src.Height;

            var otsu          = Otsu.ApplyOtsu(src);
            var erosionMethod = new Erosion();
            var erosion       = erosionMethod.ApplyErosion(otsu);
            var result        = new Bitmap(width, height);

            for (var i = 0; i < height; ++i)
            {
                for (var j = 0; j < width; ++j)
                {
                    if (erosion.GetPixel(j, i) != otsu.GetPixel(j, i))
                    {
                        result.SetPixel(j, i, Color.White);
                    }
                }
            }
            for (var i = 2; i < height - 2; ++i)
            {
                for (var j = 2; j < width - 2; ++j)
                {
                    if (result.GetPixel(j, i).R != 0)
                    {
                        dots.Add(new Point(j, i));
                    }
                }
            }

            var maxR       = (int)Math.Sqrt(width * width + height * height);
            var phaseImage = new int[maxR, 180];
            var accuraccy  = 1;

            for (var i = 0; i < dots.Count; i += 1)
            {
                var infos = new List <PointInfo>();

                for (var f = 0; f < 180; ++f)
                {
                    for (var r = 0; r < maxR; ++r)
                    {
                        var rad = f * Math.PI / 180.0;
                        if (Math.Abs(dots[i].Y * Math.Sin(rad) + dots[i].X * Math.Cos(rad) - r) < accuraccy)
                        {
                            phaseImage[r, f]++;
                            infos.Add(new PointInfo(f, r));
                        }
                    }
                }
                _pointsInfo[dots[i]] = infos;
            }

            var maxPhaseValues = new List <int>();
            var thetas         = new List <double>();
            var Rs             = new List <int>();

            for (var f = 60; f < 120; f += 30)
            {
                for (var r = 0; r < maxR; ++r)
                {
                    var a = phaseImage[r, f];
                    if (a > 1)
                    {
                        maxPhaseValues.Add(phaseImage[r, f]);
                        thetas.Add(f);
                        Rs.Add(r);
                    }
                }
            }

            var RsUniq          = new List <double>();
            var horisontalValue = new Point(0, 0);

            for (var k = 0; k < maxPhaseValues.Count; ++k)
            {
                if (RsUniq.Contains(Rs[k]))
                {
                    continue;
                }

                RsUniq.Add(Rs[k]);
                var theta = (thetas[k] * Math.PI / 180.0);

                for (var i = 0; i < height; ++i)
                {
                    for (var j = 0; j < width; ++j)
                    {
                        var a    = i * Math.Sin(theta) + j * Math.Cos(theta);
                        var line = (a * 10) % 10 > 5 ? (int)(a + 1) : (int)a;
                        if (line == Rs[k])
                        {
                            var point = new Point(j, i);
                            if (dots.Contains(point) && Criteria(src.GetPixel(j, i), Color.FromArgb(255, 99, 78, 88)))
                            {
                                result.SetPixel(j, i, Color.FromArgb(255, 0, 255, 0));
                                if (thetas[k] == 90)
                                {
                                    horisontalValue = point;
                                }
                            }
                        }
                    }
                }
            }
            var yellowPen = new Pen(Color.Yellow, 3);

            using (var graphics = Graphics.FromImage(src))
            {
                var firstGreenY = FindFirstGreen(result);
                graphics.DrawRectangle(yellowPen, new Rectangle(new Point(horisontalValue.X - 20, firstGreenY - 10),
                                                                new Size(85, 85)));
                graphics.DrawRectangle(yellowPen, new Rectangle(new Point(horisontalValue.X - 40, horisontalValue.Y),
                                                                new Size(55, 55)));
            }
            return(src);
        }