Esempio n. 1
0
        private void Try(Kamera kam)
        {
            PointF[] imageCorners = new PointF[]
            {
                new PointF(110, 110),
                new PointF(220, 420),
                new PointF(520, 1130),
                new PointF(1340, 1440),
                new PointF(1510, 340),
                new PointF(440, 210),
                new PointF(320, 200),
            };

            MCvPoint3D32f[] worldCorners = new MCvPoint3D32f[]
            {
                new MCvPoint3D32f(110, 110, 0),
                new MCvPoint3D32f(220, 420, 0),
                new MCvPoint3D32f(520, 1130, 0),
                new MCvPoint3D32f(1340, 1440, 0),
                new MCvPoint3D32f(1510, 340, 0),
                new MCvPoint3D32f(440, 210, 0),
                new MCvPoint3D32f(320, 200, 0),
            };                    

            kam.CalibrateExtrinsics(imageCorners, worldCorners);
        }
Esempio n. 2
0
        private async void CalibrateCamera()
        {
            var sqw = 50;
            var M = 8;
            var N = 7;
            var W = m.Size.Width;
            var H = m.Size.Height;
            var checkers = new Checkers(M, N, new Point((W - M * sqw) / 2 + 0, (H - N * sqw) / 2 - 0), new Size(sqw, sqw));
            
            var cap = CLEyeCapture.AllDevices.FirstOrDefault();
            if (cap == null)
                return;

            var kam = new Kamera(cap.Name, cap.Uuid, cap.Width, cap.Height);
            if (kam.HasCalibratedIntrinsics)
            {
                Try(kam);
                return;
            }
            else
            {
                List<PointF[]> imageCorners = new List<PointF[]>();
                List<MCvPoint3D32f[]> worldCorners = new List<MCvPoint3D32f[]>();

                for (; ; )
                {
                    // r.Present(null, m);
                    r.Render<Chessboard2d>(checkers);

                    var key = await KeyPress(m, p, "{0} Photos", imageCorners.Count);
                    if (key == Keys.F1)
                        break;

                    using (var pic = cap.TryDequeue())
                        if (pic != null)
                            using (var gray = pic.Bgra.Convert<Gray, byte>())
                            using (var bgr = pic.Bgra.Convert<Bgr, byte>())
                            {
                                PointF[] foundCorners;
                                var calibChessboardType = CALIB_CB_TYPE.FILTER_QUADS | CALIB_CB_TYPE.ADAPTIVE_THRESH;
                                var found = CameraCalibration.FindChessboardCorners(gray, checkers.Saddles, calibChessboardType, out foundCorners);
                                if (found)
                                {
                                    var window = new Size(sqw * 100 / 10, sqw * 100 / 10);
                                    var zeroZone = new Size(-1, -1);
                                    var criteria = new MCvTermCriteria(21, 0.0001);
                                    gray.FindCornerSubPix(new PointF[][] { foundCorners }, window, zeroZone, criteria);
                                }

                                CvInvoke.cvDrawChessboardCorners(bgr.Ptr, checkers.Saddles, foundCorners, foundCorners.Length, patternWasFound: (found ? 1 : 0));
                                bgr.Save(Path.Combine(Global.TmpDir, cap.Name + imageCorners.Count.ToString() + ".png"));

                                if (found)
                                {
                                    imageCorners.Add(foundCorners);
                                    worldCorners.Add(checkers.GetWorldPointsCopy());
                                }
                            }
                }

                kam.CalibrateIntrinsics(imageCorners.ToArray(), worldCorners.ToArray());
            }
        }