private void GenerateHistogramData()
        {
            if (this.Image != null)
            {
                Emgu.CV.Image <Bgr, byte> img = new Emgu.CV.Image <Bgr, byte>(this.Image as Bitmap);

                Emgu.CV.Image <Gray, byte>[] channels = img.Split();

                List <Color> colors = new List <Color>()
                {
                    Color.FromArgb(128, 0, 0, 255),
                    Color.FromArgb(128, 0, 255, 0),
                    Color.FromArgb(128, 255, 0, 0),
                };

                for (int c = 0; c < channels.Length; c++)
                {
                    Emgu.CV.DenseHistogram histo = new Emgu.CV.DenseHistogram(256, new RangeF(0, 255));
                    histo.Calculate <byte>(new Emgu.CV.Image <Gray, byte>[] { channels[c] }, false, null);
                    float[] values = histo.GetBinValues();

                    Series channelSeries = new Series()
                    {
                        ChartType = SeriesChartType.Column,
                        Color     = colors[c],
                    };

                    for (int i = 0; i < 256; i++)
                    {
                        channelSeries.Points.AddXY(i, values[i]);
                    }

                    VerticalLineAnnotation va = new VerticalLineAnnotation()
                    {
                        LineColor         = colors[c],
                        LineWidth         = 3,
                        AxisX             = chartHisto.ChartAreas[0].AxisX,
                        AxisY             = chartHisto.ChartAreas[0].AxisY,
                        AllowMoving       = true,
                        IsInfinitive      = true,
                        ClipToChartArea   = chartHisto.ChartAreas[0].Name,
                        AllowAnchorMoving = false,
                    };

                    this.chartHisto.Annotations.Add(va);
                    this.chartHisto.Series.Add(channelSeries);
                }
            }
        }
예제 #2
0
        public void Action(ScriptState state)
        {
            state.Image.ROI = ScriptState.FieldRectangle;
            var i = ToGray(state.Image);
            var ok = false;

            for (int y = 0; y < ScriptState.FieldSize.Height; ++y)
                for (int x = 0; x < ScriptState.FieldSize.Width; ++x)
                {
                    i.ROI = new Rectangle(new Point(x * 50, y * 50), new Size(50, 50));
                    var h = new Emgu.CV.DenseHistogram(256, new RangeF(0f, 255f));
                    h.Calculate(new[] { i }, false, null);
                    var valHist = new float[256];
                    h.MatND.ManagedArray.CopyTo(valHist, 0);
                    if (valHist[0] < 1000)
                    {
                        for (int n = 0; n < nums.Length; ++n)
                        {
                            if (nums[n] != null)
                            {
                                var r = i.Find(nums[n], Point.Empty);
                                if (!r.IsEmpty)
                                {
                                    state[x, y].Number = n >= 0 && n <= 9 ? n : n; // !!!! случай флага и незанятой клетки
                                    ok = true;
                                }
                            }
                        }
                    }
                    else state[x, y].Number = -1;
                }
            if (ok) state.ActionDone = true;
        }