Example #1
0
        /// <summary>
        /// Calibration is finished, starting PenTracker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartTracking(object sender, EventArgs e)
        {
            var mCtor = _mapperType.GetConstructor(new Type[] { typeof(Grid) });

            _mapper = (AbstractPointMapper)mCtor.Invoke(new Grid[] { CalibratorGrid });
            Console.WriteLine("Calbration completed");
            _penTracker.Start();
            if (CalibrationComplete != null)
            {
                CalibrationComplete(this, null);
            }
        }
        private async Task CalibThread(FrameReadyEventArgs e)
        {
            Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing);
            e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep < 10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg);
            if (_errors > 100)
            {
                //calibration not possible
                return;
            }
            if (_calibrationStep == CalibrationFrames && !_drawing)
            {
                _cc.FrameReady -= BaseCalibration; // TODO
                //Grid.Calculate();
                _vs.Close();
                CalibrationCompleted(this, new EventArgs());
            }
            else
            {
                if (_calibrationStep > 2)
                {
                    if (_drawing)
                    {
                        // draw diffimage
                        _drawing = false;
                        _vs.Clear();
                        FillRects();
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                    }
                    else
                    {
                        // analyse diffimage
                        _calibrationStep++;
                        _drawing = true;
                        _vs.Clear();
                        FillRects();
                        _calibrationStep--;
                        var gbm = diffFilter.Apply(e.Frame.Bitmap);
                        gbm.Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
#if DEBUG
                        actImg = (Bitmap)gbm.Clone();
#endif
                        var d     = UnmanagedImage.FromManagedImage(gbm);
                        var stats = new ImageStatistics(d);
                        var gcf   = new ColorFiltering(new IntRange(0, 255),
                                                       new IntRange((int)(stats.GreenWithoutBlack.Mean + stats.GreenWithoutBlack.StdDev + 5), 255),
                                                       new IntRange(0, 255));
                        var bcf = new ColorFiltering(new IntRange(0, 255),
                                                     new IntRange(0, 255),
                                                     new IntRange((int)(stats.BlueWithoutBlack.Mean + stats.BlueWithoutBlack.StdDev), 255));
                        //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Median + " Blue: " + stats.BlueWithoutBlack.Median);
                        //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Mean + " Blue: " + stats.BlueWithoutBlack.Mean);
                        var bf = new Difference(gcf.Apply(d));
                        bcf.ApplyInPlace(d);
                        bf.ApplyInPlace(d);
                        d.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
                        stats = new ImageStatistics(d);
                        gcf   = new ColorFiltering(new IntRange(0, 255),
                                                   new IntRange((int)stats.GreenWithoutBlack.Mean, 255),
                                                   new IntRange(0, 255));
                        bcf = new ColorFiltering(new IntRange(0, 255),
                                                 new IntRange(0, 255),
                                                 new IntRange((int)stats.BlueWithoutBlack.Mean, 255));
                        // split channels
                        var bbm = bcf.Apply(d);
                        bbm.ToManagedImage().Save(@"C:\temp\daforge\bimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp);
                        gcf.ApplyInPlace(d);
                        d.ToManagedImage().Save(@"C:\temp\daforge\gimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp);
                        var gblobCounter = new BlobCounter
                        {
                            ObjectsOrder         = ObjectsOrder.YX,
                            MaxHeight            = 60,
                            MinHeight            = 15,
                            MaxWidth             = 60,
                            MinWidth             = 15,
                            FilterBlobs          = true,
                            CoupledSizeFiltering = false
                        };
                        gblobCounter.ProcessImage(d);
                        var bblobCounter = new BlobCounter
                        {
                            ObjectsOrder         = ObjectsOrder.YX,
                            MaxHeight            = 60,
                            MinHeight            = 15,
                            MaxWidth             = 60,
                            MinWidth             = 15,
                            FilterBlobs          = true,
                            CoupledSizeFiltering = false
                        };
                        bblobCounter.ProcessImage(bbm);
                        ProcessBlobs(gblobCounter, 0);
                        ProcessBlobs(bblobCounter, 1);
#if DEBUG
                        actImg.Save(@"C:\temp\daforge\squares\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg);
#endif
                        _calibrationStep++;
                    }
                }
                else
                {
                    switch (_calibrationStep)
                    {
                    case 2:
                        var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap);
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg);
                        bm = diffFilter.Apply(bm);
                        var gf = new GaussianBlur(9.0, 3);
                        gf.ApplyInPlace(bm);
                        var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255),
                                                    new IntRange(20, 255));
                        cf.ApplyInPlace(bm);
                        var blobCounter = new BlobCounter {
                            ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true
                        };
                        blobCounter.ProcessImage(bm);
                        bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg);
                        var blobs = blobCounter.GetObjectsInformation();
                        if (blobs.Any())
                        {
                            int             i = 0;
                            List <IntPoint> corners;
                            do
                            {
                                corners =
                                    PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++]));
                            } while (corners.Count != 4);
                            RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners);
                            Grid.TopLeft     = new Point(corners[0].X, corners[0].Y);
                            Grid.TopRight    = new Point(corners[1].X, corners[1].Y);
                            Grid.BottomLeft  = new Point(corners[2].X, corners[2].Y);
                            Grid.BottomRight = new Point(corners[3].X, corners[3].Y);
                            if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 &&
                                Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 &&
                                Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 &&
                                Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 &&
                                Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X &&
                                Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y &&
                                Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y)
                            {
                                _calibrationStep++;
                                _drawing = true;
                                _vs.Clear();
                                FillRects();
                                Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y));
                                Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y));
                                Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y));
                                Grid.AddPoint(new Point(_vs.Width, _vs.Height),
                                              new Point(corners[3].X, corners[3].Y));
                                //_mapper.PredictFromCorners();
                            }
                            else
                            {
                                _calibrationStep = 0;
                                _errors++;
                            }
                        }
                        else
                        {
                            _calibrationStep = 0;
                            _errors++;
                            _vs.Draw();
                        }
                        break;

                    case 1:
                        diffFilter.OverlayImage = e.Frame.Bitmap;
                        //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg);
                        //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255));
                        _vs.Clear();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1))
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;

                    case 0:
                        Grid            = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height);
                        Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height);
                        _mapper         = new CornerBarycentricMapper(Grid);
                        var thread = new Thread(() =>
                        {
                            _vs.Show();
                            _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0));
                        });
                        thread.SetApartmentState(ApartmentState.STA);
                        thread.Start();
                        thread.Join();
                        for (int y = 0; y < Columncount; y++)
                        {
                            for (int x = 0; x < Rowcount; x++)
                            {
                                if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)
                                {
                                    _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight),
                                                (int)_sqrwidth, (int)_sqrheight,
                                                Color.FromArgb(255, 255, 255, 255));
                                }
                            }
                        }
                        _vs.Draw();
                        _calibrationStep++;
                        break;
                    }
                }
            }
            Debug.WriteLine("Releasing");
            await Task.Delay(500);

            _sem.Release();
        }
Example #3
0
        /// <summary>
        /// Calibration is finished, starting PenTracker
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartTracking(object sender, EventArgs e)
        {
            //_pictureProvider.ShowConfigurationDialog();
            //_pictureProvider.FrameReady += _camera_FrameReady; // TODO siehe _camera_FrameReady
            //_calibrator.Grid.Calculate();
            var mCtor = _mapperType.GetConstructor(new Type[] { typeof(Grid) });

            _mapper = (AbstractPointMapper)mCtor.Invoke(new Grid[] { CalibratorGrid });
            //_mapper = (AbstractPointMapper)Activator.CreateInstance(_mapperType,
            //    BindingFlags.CreateInstance, null, new Grid[] { CalibratorGrid });
            //if (_mapperType.Equals(typeof(LinearMapper))) // check for runtimeType
            //{
            //    ((LinearMapper)_mapper).Calculate();
            //}
#if DEBUG
            var bm = new Bitmap(640, 480);
            var sw = new Stopwatch();
            //for (int i = CalibratorGrid.TopLeft.X; i < CalibratorGrid.BottomRight.X; i++)
            //sw.Start();
            //try
            //{
            //    for (int i = 0; i < 640; i++)
            //    {
            //        Debug.WriteLine(i);
            //        for (int j = 0; j < 480; j++)
            //            //for (int j = CalibratorGrid.TopLeft.Y; j < CalibratorGrid.BottomRight.Y; j++)
            //        {
            //            //Debug.WriteLineIf(i == 639,j);
            //            if (CalibratorGrid.Contains(new Point(i, j)))
            //            {
            //                var position = _mapper.FromPresentation(i, j);
            //                if (position.X >= 0 && position.Y >= 0)
            //                    bm.SetPixel(i, j,
            //                                Color.FromArgb(255, (position.Y + 8192) % 160, //(position.Y + 8192)%160, 255));
            //                        (position.Y + 4096) % 256, 255));
            //            }
            //        }
            //    }
            //    sw.Stop();
            //}
            //catch (Exception ex)
            //{
            //    Debug.WriteLine(ex.Message);
            //}
            if (_mapperType.Equals(typeof(FineBarycentricMapper))) //check for runntimeType
            {
                var fbc = (FineBarycentricMapper)_mapper;
                var sum = 0.0;
                using (
                    var fs =
                        new StreamWriter(new FileStream(@"C:\Temp\aforge\perf.csv", FileMode.Append, FileAccess.Write)))
                {
                    foreach (var i in fbc.NeighbourUsedCount)
                    {
                        fs.Write(i + ",");
                        Console.Write(i + ", ");
                        sum += i;
                    }
                    fs.WriteLine();
                    fs.Flush();
                }
                Console.WriteLine("Average time per interpolation: " + ((double)sw.ElapsedMilliseconds / sum) + "ms");
            }
            bm.Save(@"C:\temp\daforge\grid.bmp", ImageFormat.MemoryBmp);
#endif
            Console.WriteLine("Calbration completed");
            _penTracker.Start();
            //_calibrator.Grid.PredictFromCorners();
        }