Open() public method

Creates video writer structure.
public Open ( string fileName, FourCC fourcc, double fps, Size frameSize, bool isColor = true ) : void
fileName string Name of the output video file.
fourcc FourCC 4-character code of codec used to compress the frames. For example, "PIM1" is MPEG-1 codec, "MJPG" is motion-jpeg codec etc. /// Under Win32 it is possible to pass null in order to choose compression method and additional compression parameters from dialog.
fps double Framerate of the created video stream.
frameSize Size Size of video frames.
isColor bool If it is true, the encoder will expect and encode color frames, otherwise it will work with grayscale frames (the flag is currently supported on Windows only).
return void
Esempio n. 1
0
        private void th_grab(int height = 0, int width = 0, int snap_wait = 500)
        {
            try
            {
                // Set the acquisition mode to free running continuous acquisition when the camera is opened.
                camera.CameraOpened += Configuration.AcquireContinuous;

                // Open the connection to the camera device.
                camera.Open();

                if (width == 0 || width > camera.Parameters[PLCamera.Width].GetMaximum())
                {
                    camera.Parameters[PLCamera.Width].SetValue(camera.Parameters[PLCamera.Width].GetMaximum());
                }
                else if (width < camera.Parameters[PLCamera.Width].GetMinimum())
                {
                    camera.Parameters[PLCamera.Width].SetValue(camera.Parameters[PLCamera.Width].GetMinimum());
                }
                else
                {
                    camera.Parameters[PLCamera.Width].SetValue(width);
                }

                if (height == 0 || width > camera.Parameters[PLCamera.Height].GetMaximum())
                {
                    camera.Parameters[PLCamera.Height].SetValue(camera.Parameters[PLCamera.Height].GetMaximum());
                }
                else if (height < camera.Parameters[PLCamera.Height].GetMinimum())
                {
                    camera.Parameters[PLCamera.Height].SetValue(camera.Parameters[PLCamera.Height].GetMinimum());
                }
                else
                {
                    camera.Parameters[PLCamera.Height].SetValue(height);
                }

                camera.Parameters[PLCamera.CenterX].SetValue(true);
                camera.Parameters[PLCamera.CenterY].SetValue(true);

                camera.StreamGrabber.Start();

                if (saveTracked)
                {
                    var    expected = new OpenCvSharp.Size(1920, 1374);
                    string filename = "D:\\save\\" + DateTime.Now.ToString("M.dd-HH.mm.ss") + ".avi";
                    videoWriter.Open(filename, OpenCvSharp.FourCCValues.XVID, 14, expected, false);
                }
                if (saveOrigin)
                {
                    var    expected = new OpenCvSharp.Size(1920, 1374);
                    string filename = "D:\\save\\" + DateTime.Now.ToString("M.dd-HH.mm.ss") + ".origin.avi";
                    originWriter.Open(filename, OpenCvSharp.FourCCValues.XVID, 14, expected, false);
                }
                if (saveHisto)
                {
                    var    expected = new OpenCvSharp.Size(256, 300);
                    string filename = "D:\\save\\" + DateTime.Now.ToString("M.dd-HH.mm.ss") + ".histo.avi";
                    histoWriter.Open(filename, OpenCvSharp.FourCCValues.XVID, 14, expected, false);
                }
                if (saveHeatmap)
                {
                    var    expected = new OpenCvSharp.Size(1920, 1374);
                    string filename = "D:\\save\\" + DateTime.Now.ToString("M.dd-HH.mm.ss") + ".heatmap.avi";
                    heatmapWriter.Open(filename, OpenCvSharp.FourCCValues.XVID, 14, expected, true);
                }

                while (grabbing)
                {
                    camera.Parameters[PLCamera.Gain].SetValue(valueGain);
                    camera.Parameters[PLCamera.ExposureTime].SetValue(valueExpTime);
                    IGrabResult grabResult = camera.StreamGrabber.RetrieveResult(5000, TimeoutHandling.ThrowException);

                    using (grabResult)
                    {
                        if (grabResult.GrabSucceeded)
                        {
                            // convert image from basler IImage to OpenCV Mat
                            Mat img = convertIImage2Mat(grabResult);

                            // convert image from BayerBG to RGB
                            Cv2.CvtColor(img, img, ColorConversionCodes.BayerBG2GRAY);
                            Cv2.Resize(img, img, new OpenCvSharp.Size(1920, 1374), 0, 0, InterpolationFlags.Linear);

                            Mat histo   = new Mat();
                            Mat heatmap = new Mat();
                            Mat dst     = img.Clone();

                            // Apply Histogram
                            histo = cvProcess.histogram(dst);

                            // Apply ColorMap
                            Cv2.ApplyColorMap(dst, heatmap, ColormapTypes.Rainbow);

                            // Apply Background map subtraction
                            Cv2.Subtract(dst, -5, dst);

                            if (saveOrigin)
                            {
                                originWriter.Write(img);
                            }

                            // Create Tracked Image
                            dst = Iso11146(img, dst);

                            Cv2.Resize(dst, dst, new OpenCvSharp.Size(1920, 1374), 0, 0, InterpolationFlags.Linear);
                            if (saveTracked)
                            {
                                videoWriter.Write(dst);
                            }
                            if (saveHisto)
                            {
                                histoWriter.Write(histo);
                            }
                            if (saveHeatmap)
                            {
                                heatmapWriter.Write(heatmap);
                            }

                            // resize image  to fit the imageBox
                            Cv2.Resize(dst, dst, new OpenCvSharp.Size(960, 687), 0, 0, InterpolationFlags.Linear);
                            Cv2.Resize(heatmap, heatmap, new OpenCvSharp.Size(256, 183), 0, 0, InterpolationFlags.Linear);

                            Cv2.Rectangle(dst, new OpenCvSharp.Rect(axis_x, axis_y, axis_scale, axis_scale), Scalar.White, 1);

                            // display images
                            BitmapToImageSource(dst);
                            BitmapHistoToImageSource(histo);
                            BitmapHeatmapToImageSource(heatmap);
                        }
                        else
                        {
                            System.Windows.MessageBox.Show("Error: {0} {1}" + grabResult.ErrorCode, grabResult.ErrorDescription);
                        }
                    }
                    count++;
                    if (count > 500)
                    {
                        count    = 0;
                        tracking = false;
                    }

                    Thread.Sleep(snap_wait);
                }
                videoWriter.Release();
                originWriter.Release();
                histoWriter.Release();
                heatmapWriter.Release();
                camera.StreamGrabber.Stop();
                camera.Close();
            }
            catch (Exception exception)
            {
                if (camera.IsOpen)
                {
                    camera.Close();
                }

                System.Windows.MessageBox.Show("Exception: {0}" + exception.Message);
            }
        }
Esempio n. 2
0
    static void Main(string[] args)
    {
        if (args.Length != 3)
        {
            Console.WriteLine("Usage: ");
            Console.WriteLine("    ZED_SVO_Export A B C ");
            Console.WriteLine("Please use the following parameters from the command line:");
            Console.WriteLine(" A - SVO file path (input) : \"path/to/file.svo\"");
            Console.WriteLine(" B - AVI file path (output) or image sequence folder(output) : \"path/to/output/file.avi\" or \"path/to/output/folder\"");
            Console.WriteLine(" C - Export mode:  0=Export LEFT+RIGHT AVI.");
            Console.WriteLine("                   1=Export LEFT+DEPTH_VIEW AVI.");
            Console.WriteLine("                   2=Export LEFT+RIGHT image sequence.");
            Console.WriteLine("                   3=Export LEFT+DEPTH_VIEW image sequence.");
            Console.WriteLine("                   4=Export LEFT+DEPTH_16Bit image sequence.");
            Console.WriteLine(" A and B need to end with '/' or '\\'\n\n");
            Console.WriteLine("Examples: \n");
            Console.WriteLine("  (AVI LEFT+RIGHT)   ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 0");
            Console.WriteLine("  (AVI LEFT+DEPTH)   ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/file.avi\" 1");
            Console.WriteLine("  (SEQUENCE LEFT+RIGHT)   ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 2");
            Console.WriteLine("  (SEQUENCE LEFT+DEPTH)   ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 3");
            Console.WriteLine("  (SEQUENCE LEFT+DEPTH_16Bit)   ZED_SVO_Export \"path/to/file.svo\" \"path/to/output/folder\" 4");

            Environment.Exit(-1);
        }
        string   svoInputPath  = args[0];
        string   outputPath    = args[1];
        bool     outputAsVideo = true;
        APP_TYPE appType       = APP_TYPE.LEFT_AND_RIGHT;

        if (args[2].Equals("1") || args[2].Equals("3"))
        {
            appType = APP_TYPE.LEFT_AND_DEPTH;
        }
        if (args[2].Equals("4"))
        {
            appType = APP_TYPE.LEFT_AND_DEPTH_16;
        }
        // Check if exporting to AVI or SEQUENCE
        if (!args[2].Equals("0") && !args[2].Equals("1"))
        {
            outputAsVideo = false;
        }

        if (!outputAsVideo && !Directory.Exists(outputPath))
        {
            Console.WriteLine("Input directory doesn't exist. Check permissions or create it. " + outputPath);
            Environment.Exit(-1);
        }
        if (!outputAsVideo && outputPath.Substring(outputPath.Length - 1) != "/" && outputPath.Substring(outputPath.Length - 1) != "\\")
        {
            Console.WriteLine("Error: output folder needs to end with '/' or '\\'." + outputPath);
            Environment.Exit(-1);
        }

        // Create ZED Camera
        Camera zed = new Camera(0);

        //Specify SVO path parameters
        InitParameters initParameters = new InitParameters()
        {
            inputType       = INPUT_TYPE.SVO,
            pathSVO         = svoInputPath,
            svoRealTimeMode = true,
            coordinateUnits = UNIT.MILLIMETER
        };

        ERROR_CODE zedOpenState = zed.Open(ref initParameters);

        if (zedOpenState != ERROR_CODE.SUCCESS)
        {
            Environment.Exit(-1);
        }

        Resolution imageSize = zed.GetCalibrationParameters().leftCam.resolution;

        sl.Mat leftImage = new sl.Mat();
        leftImage.Create(imageSize, MAT_TYPE.MAT_8U_C4);
        OpenCvSharp.Mat leftImageOCV = SLMat2CVMat(ref leftImage, MAT_TYPE.MAT_8U_C4);

        sl.Mat rightImage = new sl.Mat();
        rightImage.Create(imageSize, MAT_TYPE.MAT_8U_C4);
        OpenCvSharp.Mat rightImageOCV = SLMat2CVMat(ref rightImage, MAT_TYPE.MAT_8U_C4);

        sl.Mat depthImage = new sl.Mat();
        depthImage.Create(imageSize, MAT_TYPE.MAT_32F_C1);
        OpenCvSharp.Mat depthImageOCV = SLMat2CVMat(ref depthImage, MAT_TYPE.MAT_8U_C4);

        OpenCvSharp.Mat imageSideBySide = new OpenCvSharp.Mat();
        if (outputAsVideo)
        {
            imageSideBySide = new OpenCvSharp.Mat((int)imageSize.height, (int)imageSize.width * 2, OpenCvSharp.MatType.CV_8UC3);
        }

        OpenCvSharp.VideoWriter videoWriter = new OpenCvSharp.VideoWriter();

        //Create Video writter
        if (outputAsVideo)
        {
            int fourcc = OpenCvSharp.VideoWriter.FourCC('M', '4', 'S', '2'); // MPEG-4 part 2 codec

            int frameRate = Math.Max(zed.GetInitParameters().cameraFPS, 25); // Minimum write rate in OpenCV is 25
            Console.WriteLine(outputPath);
            videoWriter.Open(outputPath, fourcc, frameRate, new OpenCvSharp.Size((int)imageSize.width * 2, (int)imageSize.height));
            if (!videoWriter.IsOpened())
            {
                Console.WriteLine("Error: OpenCV video writer cannot be opened. Please check the .avi file path and write permissions.");
                zed.Close();
                Environment.Exit(-1);
            }
        }

        RuntimeParameters rtParams = new RuntimeParameters();

        rtParams.sensingMode = SENSING_MODE.FILL;

        // Start SVO conversion to AVI/SEQUENCE
        Console.WriteLine("Converting SVO... press Q to interupt conversion");

        int nbFrames    = zed.GetSVONumberOfFrames();
        int svoPosition = 0;

        zed.SetSVOPosition(svoPosition);


        while (!exit_app)
        {
            exit_app = (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.Q) == true);
            ERROR_CODE err = zed.Grab(ref rtParams);
            if (err == ERROR_CODE.SUCCESS)
            {
                svoPosition = zed.GetSVOPosition();

                // Retrieve SVO images
                zed.RetrieveImage(leftImage, VIEW.LEFT);

                switch (appType)
                {
                case APP_TYPE.LEFT_AND_RIGHT:
                    zed.RetrieveImage(rightImage, VIEW.RIGHT);
                    break;

                case APP_TYPE.LEFT_AND_DEPTH:
                    zed.RetrieveImage(rightImage, VIEW.DEPTH);
                    break;

                case APP_TYPE.LEFT_AND_DEPTH_16:
                    zed.RetrieveMeasure(depthImage, MEASURE.DEPTH);
                    break;

                default:
                    break;
                }

                if (outputAsVideo)
                {
                    // Convert SVO image from RGBA to RGB
                    Cv2.CvtColor(leftImageOCV, imageSideBySide[new OpenCvSharp.Rect(0, 0, (int)imageSize.width, (int)imageSize.height)], ColorConversionCodes.BGRA2BGR);
                    Cv2.CvtColor(rightImageOCV, imageSideBySide[new OpenCvSharp.Rect((int)imageSize.width, 0, (int)imageSize.width, (int)imageSize.height)], ColorConversionCodes.BGRA2BGR);
                    // Write the RGB image in the video
                    videoWriter.Write(imageSideBySide);
                }
                else
                {
                    // Generate filenames
                    string filename1 = "";
                    filename1 = outputPath + "/left" + svoPosition + ".png";
                    string filename2 = "";
                    filename2 = outputPath + (appType == APP_TYPE.LEFT_AND_RIGHT ? "/right" : "/depth") + svoPosition + ".png";

                    // Save Left images
                    Cv2.ImWrite(filename1, leftImageOCV);

                    //Save depth
                    if (appType != APP_TYPE.LEFT_AND_DEPTH_16)
                    {
                        Cv2.ImWrite(filename2, rightImageOCV);
                    }
                    else
                    {
                        //Convert to 16 bit
                        OpenCvSharp.Mat depth16 = new OpenCvSharp.Mat();
                        depthImageOCV.ConvertTo(depth16, MatType.CV_16UC1);
                        Cv2.ImWrite(filename2, depth16);
                    }
                }

                // Display Progress
                ProgressBar((float)svoPosition / (float)nbFrames, 30);
            }
            else if (zed.GetSVOPosition() >= nbFrames - (zed.GetInitParameters().svoRealTimeMode ? 2 : 1))
            {
                Console.WriteLine("SVO end has been reached. Exiting now.");
                Environment.Exit(-1);
                exit_app = true;
            }
            else
            {
                Console.WriteLine("Grab Error : " + err);
                exit_app = true;
            }
        }
        if (outputAsVideo)
        {
            //Close the video writer
            videoWriter.Release();
        }

        zed.Close();
    }