Ejemplo n.º 1
0
        /**********************************************************
        * redirect output to file for logging
        * take scene background images for image subtraction algorithm
        * drone takeoff
        * main vido processing loop
        **********************************************************/
        public void StartButton_Click(object sender, EventArgs e)
        {
            // Change output to a file
            FileStream   ostrm;
            StreamWriter writer;
            TextWriter   oldOut = Console.Out;

            try
            {
                File.Delete(eventLogFile);
                ostrm  = new FileStream(eventLogFile, FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(ostrm);
            }
            catch (Exception er)
            {
                Console.WriteLine("Cannot open EventLog.txt for writing");
                Console.WriteLine(er.Message);
                return;
            }
            Console.SetOut(writer);

            if (capLeft == null)
            {
                capLeft = new VideoCapture(Camera_Selection_Left.SelectedIndex);
            }
            if (capRight == null)
            {
                capRight = new VideoCapture(Camera_Selection_Right.SelectedIndex);
            }
            if (capLeft.IsOpened && capRight.IsOpened) // check that both cameras working
            {
                // set video parameters
                videoFourcc = (int)capLeft.GetCaptureProperty(CapProp.FourCC);
                videoWidth  = 2 * (int)capLeft.GetCaptureProperty(CapProp.FrameWidth);
                videoHeight = 2 * (int)capLeft.GetCaptureProperty(CapProp.FrameHeight);
                videoFps    = 25;
                videoWrite  = new VideoWriter(videoPath, videoFourcc, videoFps, new Size(videoWidth, videoHeight), true);
                MessageBox.Show("Press ESCAPE key to close the program");
            }
            else
            {
                throw new Exception("Error opening a camera");
            }

            // Calibrate cameras
            Calibration();

            rmt = new Tello();

            // Obtaining and showing first frame of loaded video(used as the base for difference detection)
            backgroundFrame_l = capLeft.QueryFrame();
            backgroundFrame_r = capRight.QueryFrame();

            Mat backgroundLeftRemap  = new Mat();
            Mat backgroundRightRemap = new Mat();

            // Apply transformation to rectify background images
            CvInvoke.Remap(backgroundFrame_l, backgroundLeftRemap, rmapx1, rmapy1, Inter.Linear);
            CvInvoke.Remap(backgroundFrame_r, backgroundRightRemap, rmapx2, rmapy2, Inter.Linear);
            Mat backgroundLeftCrop  = new Mat(backgroundLeftRemap, Rec1);
            Mat backgroundRightCrop = new Mat(backgroundRightRemap, Rec2);

            //Mat backgroundLeftCrop = backgroundLeftRemap.Clone();
            //Mat backgroundRightCrop = backgroundRightRemap.Clone();

            //CvInvoke.Imshow(BackgroundFrameWindowName_l, backgroundLeftCrop);
            //CvInvoke.Imshow(BackgroundFrameWindowName_r, backgroundRightCrop);

            // Drone takeoff from the ground
            CvInvoke.Resize(telloFrame, telloFrame, new Size(100, 100));
            CvInvoke.Imshow("Press Here", telloFrame);
            rmt.Takeoff();
            CvInvoke.WaitKey(5000);
            System.Media.SystemSounds.Exclamation.Play();

            //Handling video frames(image processing and contour detection)
            VideoProcessingLoop(capLeft, backgroundLeftCrop, capRight, backgroundRightCrop, rmapx1, rmapy1, rmapx2, rmapy2, Rec1, Rec2);
        }