public Dictionary <int, List <MarkerStructure> > GetMarkerList()
        {
            int frame_number = 0;
            int total_frame  = (int)m_data.Get(OpenCvSharp.CaptureProperty.FrameCount);

            OpenCvSharp.Mat read_image =
                new OpenCvSharp.Mat(new OpenCvSharp.Size(m_data.FrameWidth, m_data.FrameHeight), OpenCvSharp.MatType.CV_8U);
            frame_number = 0;
            if (!m_data.IsOpened())
            {
                System.Windows.Forms.MessageBox.Show("not opened");
            }
#if DEBUG1
            var window    = new OpenCvSharp.Window("debug");
            int sleepTime = (int)Math.Round(1000 / m_data.Fps);
#endif
            while (true)
            {
                //m_data.Read(read_image);
                //m_data.Grab();
                OpenCvSharp.NativeMethods.videoio_VideoCapture_operatorRightShift_Mat(m_data.CvPtr, read_image.CvPtr);
                Dictionary <int, List <MarkerStructure> > tmp = MarkerTracker(read_image, frame_number);
#if DEBUG1
                if (!read_image.Empty())
                {
                    foreach (int key in tmp.Keys)
                    {
                        foreach (MarkerStructure aa in tmp[key])
                        {
                            OpenCvSharp.Cv2.Rectangle(read_image, new OpenCvSharp.Rect(
                                                          new OpenCvSharp.Point(aa.marker_position.X, aa.marker_position.Y), new OpenCvSharp.Size(aa.marker_size.Height, aa.marker_size.Width)), new OpenCvSharp.Scalar(255.0f, 255.0f, 0.0f), 3);
                        }
                    }
                    OpenCvSharp.Cv2.PutText(read_image, frame_number.ToString(), new OpenCvSharp.Point(10, 50), OpenCvSharp.HersheyFonts.HersheyPlain, 5.0f, new OpenCvSharp.Scalar(255.0f, 255.0f, 0.0f), 3);
                    window.ShowImage(read_image);
                    OpenCvSharp.Cv2.WaitKey(sleepTime);
                }
#endif
                if (tmp != null)
                {
                    marker_result = AddDictionary(marker_result, tmp);
                }
                frame_number++;
                if (read_image.Empty())
                {
                    break;
                }
            }

            foreach (int key in marker_result.Keys)
            {
                marker_result[key].Sort();
            }

            return(marker_result);
        }
Example #2
0
        public MainWindow()
        {
            InitializeComponent();

            headset = new FVRHeadset(EFVR_ClientCapabilities.Gaze | EFVR_ClientCapabilities.Orientation);

            headset.IsHardwareConnected(out bool isConnected);
            Debug.Assert(isConnected, "Hardware is not connected!");

            headset.IsHardwareReady(out bool isReady);
            Debug.Assert(isReady, "Hardware capabilities are not ready!");

            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() == true)
            {
                filename = dialog.FileName;
            }
            else
            {
                MessageBox.Show("File is not selected!");
                return;
            }

            video = new OpenCvSharp.VideoCapture(filename);
            if (!video.IsOpened())
            {
                MessageBox.Show("Video is not valid!");
                return;
            }

            video_width  = (int)video.Get(OpenCvSharp.CaptureProperty.FrameWidth);
            video_height = (int)video.Get(OpenCvSharp.CaptureProperty.FrameHeight);

            OpenCvSharp.Cv2.NamedWindow("coord", OpenCvSharp.WindowMode.Normal);
            OpenCvSharp.Cv2.ResizeWindow("coord", 1600, 900);

            renderer = new EquirectToRect(new GLControl(), new System.Drawing.Size(1400, 900), 100 * (float)Math.PI / 180);
            renderer.Viewer.Paint += (sender, e) =>
            {
                renderer.Render(currentFrame);
            };
            renderer.Viewer.Dock = System.Windows.Forms.DockStyle.Fill;
            viewer.Child         = renderer.Viewer;

            renderingTimer          = new DispatcherTimer();
            renderingTimer.Interval = TimeSpan.FromMilliseconds(1);
            renderingTimer.Tick    += (s, e) =>
            {
                video.Read(currentFrame);
                if (currentFrame.Empty())
                {
                    video.Set(OpenCvSharp.CaptureProperty.PosFrames, 0);
                    return;
                }

                headset.GetHMDPose(out SFVR_Pose pose);

                using (OpenCvSharp.Mat coordmap = currentFrame.Clone())
                {
                    SFVR_Vec3 direction = pose.orientation * SFVR_Vec3.Forward;
                    GetCoord(direction, video_width, video_height, out int x, out int y);
                    OpenCvSharp.Cv2.Circle(coordmap, new OpenCvSharp.Point(x, y), 50, new OpenCvSharp.Scalar(0, 0, 255), -1);

                    OpenCvSharp.Cv2.ImShow("coord", coordmap);
                    OpenCvSharp.Cv2.WaitKey(1);
                }

                SFVR_Vec3 eulerAngle = pose.orientation.EulerAngles;

                renderer.Pitch = -MathHelper.DegreesToRadians(eulerAngle.x);
                renderer.Yaw   = MathHelper.DegreesToRadians(eulerAngle.y);
                renderer.Roll  = MathHelper.DegreesToRadians(eulerAngle.z);

                renderer.Viewer.Invalidate();
            };
            renderingTimer.Start();
        }