Exemplo n.º 1
0
        public static void RunOld(string[] args)
        {
            var intrinsicfile = args.FirstOrDefault() ?? Calibration.KinectDefaultFileName;
            var camIntrinsic = Utils.DeSerializeObject<CalibrationResult>(intrinsicfile);
            var projFile = args.Skip(1).FirstOrDefault() ?? Calibration.ProjectorDefaultFileName;
            var projIntrinsic = Utils.DeSerializeObject<CalibrationResult>(projFile);
            KinectSensor sensor = KinectSensor.KinectSensors.First();
            Camera cam = new Camera(sensor, ColorImageFormat.RgbResolution1280x960Fps12);
            Projector proj = new Projector();
            var keyl = new KeyboardListener(proj.window.Keyboard);
            double offsetx = 0, offsety = 0, scale = 0.5;
            bool proceed = false, quit = false;
            keyl.AddBinaryAction(0.02, -0.02, OpenTK.Input.Key.Up, OpenTK.Input.Key.Down, new OpenTK.Input.Key[0], (f) => offsety += f);
            keyl.AddBinaryAction(0.02, -0.02, OpenTK.Input.Key.Left, OpenTK.Input.Key.Right, new OpenTK.Input.Key[0], (f) => offsetx -= f);
            keyl.AddBinaryAction(0.02, -0.02, OpenTK.Input.Key.Up, OpenTK.Input.Key.Down, new OpenTK.Input.Key[] { Key.ShiftLeft }, (f) => scale += f);
            keyl.AddAction(() => proceed = true, Key.Space);
            keyl.AddAction(() => quit = proceed = true, Key.Q);
            PointF[] corners;
            proj.DrawBackground(Color.Black);
            while (true)
            {
                Console.WriteLine("Make sure the kinect can see the board");
                Console.ReadLine();
                corners = StereoCalibration.GetCameraCorners(cam.TakePicture(3), new Size(7, 4), false);
                if (corners.All(c => c != null))
                {
                    break;
                }
                else
                    Console.WriteLine("Could not find corners");
            }
            PointF[] projCorners;
            var projectedCorners = proj.DrawCheckerboard(new Size(8, 5), 0, 0, 0, scale, offsetx, offsety);
            while (true)
            {
                Console.WriteLine("Make sure the kinect can see the projection");
                while (!proceed)
                {
                    projectedCorners = proj.DrawCheckerboard(new Size(8, 5), 0, 0, 0, scale, offsetx, offsety);
                    proj.window.ProcessEvents();
                }
                projCorners = StereoCalibration.GetCameraCorners(cam.TakePicture(3), new Size(7, 4), false);
                if (corners.All(c => c != null))
                {
                    break;
                }
                else
                    Console.WriteLine("Could not find any corners, make sure the checkerboard is visible to all Kinects.");
            }

            var camResult = StereoCalibration.CalibrateCamera(corners, new Size(7, 4), 0.05f, camIntrinsic);
            var transform = StereoCalibration.FindHomography(projCorners, projectedCorners);
            var projResult = StereoCalibration.CalibrateCamera(transform(corners), new Size(7, 4), 0.05f, projIntrinsic);
            Utils.SerializeObject(camResult, intrinsicfile);
            Utils.SerializeObject(projResult, projFile);
            proj.Close();
        }
Exemplo n.º 2
0
        public override void Load(ProgramWindow parent)
        {
            parent.MakeCurrent();
            base.Load(parent);
            GL.Disable(EnableCap.Dither);
            GL.Enable(EnableCap.Texture2D);
            GL.Enable(EnableCap.Blend);
            GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.DstAlpha);
            GL.ClearColor(System.Drawing.Color.FromArgb(0,0,0,0));
            var vs = parent.CreateShader(ShaderType.VertexShader, VSLighting);
            var fs = parent.CreateShader(ShaderType.FragmentShader, FSLighting);
            program = parent.CreateProgram(vs, fs);
            GL.DeleteShader(vs);
            GL.DeleteShader(fs);
            dynamicProgram.Load();

            textures = new MultipleTextures(this);
            Bitmap white = new Bitmap(101, 101);
            QuickDraw.Start(white)
                .Fill(Color.White).Finish();
            Bitmap gridBot = new Bitmap(101, 101);
            QuickDraw.Start(gridBot)
                .All((x, y) =>
                {
                    var lpp = 10;
                    var xm = x % lpp;
                    var ym = y % lpp;
                    if (xm == 0 || ym == 0)
                        return Color.Gray;
                    return Color.FromArgb(150, 50, 50, 50);
                }, false).Finish();
            Bitmap[] maps = new Bitmap[] {
                white, gridBot
            };
            textures.Load(maps);

            if (lightStudio != null)
                lights = lightStudio.Lights;
            else
                lights = new MoveableLights(8);

            dynamicRenderer = new Renderer(dynamics = (Dynamight.ImageProcessing.CameraCalibration.Range.OfInts(6)).Select(_ => new DynamicRenderable()).ToArray());
            staticRenderer = new Renderer(null, (new Renderable[] {
                Grid = new Renderable() {
                    Visible = false,
                    Shape = new Quad(new Vector3(0,0,0), 10, new Vector3(1,0,0), new Vector3(0,0,1), (v) => textures.Transform(v, 1)),
                    Animatable = new Translator()
                },
            }).Concat(LightsObjects = lights.Select(l => new LightRenderable(l, (v) => textures.Transform(v, 0))).ToArray()).ToArray());
            windowSize = parent.Size;
            SetupCamera();

            staticRenderer.Load();
            dynamicRenderer.Load();
            staticRenderer.Start();
            dynamicRenderer.Start();

            keyl = new KeyboardListener(parent.Keyboard);

            keyl.AddAction(() => Selection = (Selection == null ? (int?)1 : null), Key.C);

            keyl.AddAction(() => Grid.Visible = !Grid.Visible, Key.G);

            keyl.AddBinaryAction(0.01f, -0.01f, Key.Right, Key.Left, null, (f) => MoveX(f));
            keyl.AddBinaryAction(0.01f, -0.01f, Key.Down, Key.Up, null, (f) => MoveY(f));
            keyl.AddBinaryAction(0.01f, -0.01f, Key.Down, Key.Up, new Key[] { Key.ShiftLeft }, (f) => MoveZ(f));

            keyl.AddBinaryAction(0.05f, -0.05f, Key.Right, Key.Left, new Key[] { Key.ControlLeft }, (f) => MoveX(f));
            keyl.AddBinaryAction(0.05f, -0.05f, Key.Down, Key.Up, new Key[] { Key.ControlLeft }, (f) => MoveY(f));
            keyl.AddBinaryAction(0.05f, -0.05f, Key.Down, Key.Up, new Key[] { Key.ShiftLeft, Key.ControlLeft }, (f) => MoveZ(f));

            GL.Enable(EnableCap.Lighting);
            GL.Enable(EnableCap.Light0);
            GL.Enable(EnableCap.Light1);
            GL.Enable(EnableCap.Light2);
            GL.Enable(EnableCap.Light3);
            GL.Enable(EnableCap.Light4);
            GL.Enable(EnableCap.Light5);
            GL.Enable(EnableCap.Light6);
            GL.Enable(EnableCap.Light7);
        }
Exemplo n.º 3
0
        public static void Run(string[] args)
        {
            var projfile = args.Skip(1).FirstOrDefault() ?? Calibration.ProjectorDefaultFileName;
            if (!File.Exists(projfile))
            {
                Console.WriteLine("Either calib file could not be found.");
                return;
            }

            var pc = Utils.DeSerializeObject<CalibrationResult>(projfile);

            var window = ProgramWindow.OpenOnSecondary();

            var program = new LightStudioProgram(0.08f);
            window.SetProgram(program);

            var overviewWindow = new ProgramWindow(750, 50, 1280, 960);
            overviewWindow.Load();
            overviewWindow.ResizeGraphics();
            OverviewProgram overview = new OverviewProgram(program);
            overviewWindow.SetProgram(overview);

            var format = DepthImageFormat.Resolution80x60Fps30;
            var inputs = KinectSensor.KinectSensors.Where(k => k.Status == KinectStatus.Connected).Select(k =>
            {
                k.Start();
                return new
                {
                    sensor = k,
                    depth = new DepthCamera(k, format),
                    skeleton = new SkeletonCamera(k),
                    calibrator = new KinectCalibrator(Utils.DeSerializeObject<CalibrationResult>(k.UniqueKinectId.Substring(k.UniqueKinectId.Length - 16) + ".xml"))
                };
            });
            float[] data = Utils.DeSerializeObject<float[]>(LightningFastApp.IR2RGBFILE) ?? MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.Identity(4).ToColumnWiseArray();
            MathNet.Numerics.LinearAlgebra.Generic.Matrix<float> D2C = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, data);

            var keyl = new KeyboardListener(window.Keyboard);
            SkeletonPoint[] skeletons = null;
            // Action H hide background:
            bool hideBackground = false;
            float zCutoff = 3.4f;
            float xadj = 0, yadj = 0.0f, zadj = 0;
            float incr = 0.01f;
            bool adjust = true;
            program.SetProjection(pc);
            Action<float> xfoo = (f) =>
            {
                if (!adjust)
                    return;
                xadj += f;
                program.SetProjection(pc);
            };
            Action<float> yfoo = (f) =>
            {
                if (!adjust)
                    return;
                yadj += f;
                program.SetProjection(pc);
            };
            Action<float> zfoo = (f) =>
            {
                if (!adjust)
                    return;
                zadj += f;
                program.SetProjection(pc);
            };
            keyl.AddAction(() => adjust = !adjust, Key.A);
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, null, (i) => yfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Left, Key.Right, null, (i) => xfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ShiftLeft }, (i) => zfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ControlLeft }, (i) => yfoo(i * incr * 3));
            keyl.AddBinaryAction(1, -1, Key.Left, Key.Right, new Key[] { Key.ControlLeft }, (i) => xfoo(i * incr * 3));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ShiftLeft, Key.ControlLeft }, (i) => zfoo(i * incr * 3));

            keyl.AddAction(() =>
            {
                hideBackground = !hideBackground;
                if (skeletons != null && skeletons.Length > 0)
                {
                    zCutoff = skeletons.Min(sp => sp.Z);
                }
                else
                    zCutoff = 0;
            }, OpenTK.Input.Key.H);

            program.SetProjection(pc, inputs.Select(i => i.calibrator).First().GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(xadj, yadj, zadj));

            while (true)
            {
                var points = inputs.Select(inp => new
                {
                    Calibrator = inp.calibrator,
                    Skeletons = inp.depth.Get(100).Where(p => p.HasValue && p.Value.Index > 0)
                        .Select(p => inp.sensor.CoordinateMapper.MapDepthPointToSkeletonPoint(format, p.Value.Point))
                        .ToArray()
                });

                program.SetPositions(points.Take(1).SelectMany(v => v.Skeletons.Select(sp => new Vector3(sp.X, sp.Y, sp.Z))));

                overview.SetPointCloud(0, points.SelectMany(p =>
                    {
                        return p.Skeletons.Select(sp =>
                        {
                            var gp = p.Calibrator.ToGlobal(sp);
                            return new DynamicVertex(new Vector3(gp[0], gp[1], gp[2]));
                        });
                    }).ToArray());

                window.RenderFrame();
                overviewWindow.RenderFrame();
                window.ProcessEvents();
                overviewWindow.ProcessEvents();
            }
        }
Exemplo n.º 4
0
        public static void RunOld(string[] args)
        {
            var camfile = args.FirstOrDefault() ?? Calibration.KinectDefaultFileName;
            var projfile = args.Skip(1).FirstOrDefault() ?? Calibration.ProjectorDefaultFileName;
            if (!File.Exists(camfile) || !File.Exists(projfile))
            {
                Console.WriteLine("Either calib file could not be found.");
                return;
            }
            var cc = Utils.DeSerializeObject<CalibrationResult>(camfile);
            var pc = Utils.DeSerializeObject<CalibrationResult>(projfile);

            var window = ProgramWindow.OpenOnSecondary();

            var program = new LightStudioProgram(0.08f);
            window.SetProgram(program);

            var overviewWindow = new ProgramWindow(750, 50, 1280, 960);
            overviewWindow.Load();
            overviewWindow.ResizeGraphics();
            OverviewProgram overview = new OverviewProgram(program);
            overviewWindow.SetProgram(overview);

            KinectSensor sensor = KinectSensor.KinectSensors.First();
            var format = DepthImageFormat.Resolution80x60Fps30;
            DepthCamera depthCam = new DepthCamera(sensor, format);
            KinectCalibrator kc = new KinectCalibrator(cc);
            SkeletonCamera scam = new SkeletonCamera(sensor);
            sensor.Start();
            float[] data = Utils.DeSerializeObject<float[]>(LightningFastApp.IR2RGBFILE) ?? MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.Identity(4).ToColumnWiseArray();
            MathNet.Numerics.LinearAlgebra.Generic.Matrix<float> D2C = MathNet.Numerics.LinearAlgebra.Single.DenseMatrix.OfColumnMajor(4, 4, data);
            //var adjustment = OpenTK.Matrix4.CreateTranslation(0f, 0.14f, 0.00f);
            //program.SetProjection(pc, kc.GetModelView(D2C), adjustment);

            //program.SetProjection(pc); //, kc.GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(0f, 0.14f, 0.06f));
            //var rs = Range.OfDoubles(0.5, -0.5, 0.04);
            //program.SetPositions(rs.SelectMany(x => rs.Select(y => new Vector3((float)x, (float)y, 1.7f))).ToArray());
            //while (true)
            //{
            //    window.RenderFrame();
            //    window.ProcessEvents();
            //}
            var keyl = new KeyboardListener(window.Keyboard);
            SkeletonPoint[] skeletons = null;
            // Action H hide background:
            bool hideBackground = false;
            float zCutoff = 3.4f;
            float xadj = 0, yadj = 0.0f, zadj = 0;
            float incr = 0.01f;
            bool adjust = true;
            program.SetProjection(pc, kc.GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(xadj, yadj, zadj));
            Action<float> xfoo = (f) =>
            {
                if (!adjust)
                    return;
                xadj += f;
                program.SetProjection(pc, kc.GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(xadj, yadj, zadj));
            };
            Action<float> yfoo = (f) =>
            {
                if (!adjust)
                    return;
                yadj += f;
                program.SetProjection(pc, kc.GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(xadj, yadj, zadj));
            };
            Action<float> zfoo = (f) =>
            {
                if (!adjust)
                    return;
                zadj += f;
                program.SetProjection(pc, kc.GetModelView(D2C), OpenTK.Matrix4.CreateTranslation(xadj, yadj, zadj));
            };
            keyl.AddAction(() => adjust = !adjust, Key.A);
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, null, (i) => yfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Left, Key.Right, null, (i) => xfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ShiftLeft }, (i) => zfoo(i * incr));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ControlLeft }, (i) => yfoo(i * incr * 3));
            keyl.AddBinaryAction(1, -1, Key.Left, Key.Right, new Key[] { Key.ControlLeft }, (i) => xfoo(i * incr * 3));
            keyl.AddBinaryAction(1, -1, Key.Up, Key.Down, new Key[] { Key.ShiftLeft, Key.ControlLeft }, (i) => zfoo(i * incr * 3));

            keyl.AddAction(() =>
            {
                hideBackground = !hideBackground;
                if (skeletons != null && skeletons.Length > 0)
                {
                    zCutoff = skeletons.Min(sp => sp.Z);
                }
                else
                    zCutoff = 0;
            }, OpenTK.Input.Key.H);
            while (true)
            {
                var points = depthCam.Get(1000).Where(p => p.HasValue && p.Value.Index > 0).Select(p => p.Value);
                skeletons = points.Select(p => sensor.CoordinateMapper.MapDepthPointToSkeletonPoint(format, p.Point)).ToArray();
                program.SetPositions(skeletons.Where(sp => !hideBackground || sp.Z < zCutoff).Select(sp => new Vector3(sp.X, sp.Y, sp.Z)).ToArray());
                overview.SetPointCloud(0, skeletons.Select(sp =>
                {
                    var gp = kc.ToGlobal(sp);
                    return new DynamicVertex(new Vector3(gp[0], gp[1], gp[2]));
                }).ToArray());
                window.RenderFrame();
                overviewWindow.RenderFrame();
                window.ProcessEvents();
                overviewWindow.ProcessEvents();
            }
        }