public void WriteAndCapture()
        {
            using var image = Image("lenna.png");

            {
                using var writer = new VideoWriter("dummy1.avi", FourCCValues.MJPG, 10, image.Size());
                Assert.True(writer.IsOpened());
                writer.Write(image);
                writer.Write(image);
                writer.Write(image);
            }

            using var capture = new VideoCapture("dummy1.avi");
            Assert.True(capture.IsOpened());
            Assert.Equal("CV_MJPEG", capture.GetBackendName());
            Assert.Equal(3, capture.FrameCount);

            using var frame1 = new Mat();
            using var frame2 = new Mat();
            using var frame3 = new Mat();
            using var frame4 = new Mat();
            Assert.True(capture.Read(frame1));
            Assert.True(capture.Read(frame2));
            Assert.True(capture.Read(frame3));
            Assert.False(capture.Read(frame4));
            Assert.False(frame1.Empty());
            Assert.False(frame2.Empty());
            Assert.False(frame3.Empty());
            Assert.True(frame4.Empty());

            Assert.Equal(image.Size(), frame1.Size());
        }
Beispiel #2
0
 private bool WaitCancel(bool frg)
 {
     Invoke((MethodInvoker) delegate
     {
         Mat VF = Fr.Clone();
         if (frg)
         {
             pictureBox1.Image = BitmapConverter.ToBitmap(Fr);
             if (RecFrg)
             {
                 VW.Write(VF);
             }
             else if (VW != null && VW.IsOpened())
             {
                 VW.Release();
             }
             pictureBox1.Refresh();
         }
     });
     if (frg)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
        public void GetSetOption()
        {
            using var writer = new VideoWriter("dummy2.avi", FourCCValues.MJPG, 10, new Size(640, 480));
            Assert.True(writer.IsOpened());

            writer.Set(VideoWriterProperties.Quality, 50);
            Assert.Equal(50, writer.Get(VideoWriterProperties.Quality), 3);
        }
Beispiel #4
0
 public void stopCamera()
 {
     if (capture.IsOpened())
     {
         capture.Release();
         if (cameraThread != null && cameraThread.IsAlive)
         {
             cameraThread.Abort();
             cameraThread.Join();
         }
         writer.Release();
     }
     if (writer.IsOpened())
     {
         writer.Release();
     }
 }
Beispiel #5
0
        // ======================================


        // ======================================
        // Exit button : 終了ボタン
        private void 終了EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            stop = true;
            while (Draw_Thread.IsBusy)
            {
                Application.DoEvents();
            }
            if (writer.IsOpened())
            {
                writer.Dispose();
            }
            System.Diagnostics.Process.Start("EXPLORER.EXE", @"C:\Temp");
            Application.Exit();
        }
Beispiel #6
0
        public void WriteAndCapture()
        {
            const string fileName = "dummy1.avi";

            try
            {
                using var image = Image("lenna.png");

                {
                    using var writer = new VideoWriter(fileName, VideoCaptureAPIs.OPENCV_MJPEG, FourCC.MJPG, 10, image.Size());
                    Assert.True(writer.IsOpened());
                    writer.Write(image);
                    writer.Write(image);
                    writer.Write(image);
                }

                using var capture = new VideoCapture(fileName);
                Assert.True(capture.IsOpened());

                var backendName = capture.GetBackendName();
                testOutputHelper.WriteLine($"[{nameof(WriteAndCapture)}] BackendName: {backendName}");
                Assert.True(backendName == "MSMF" || backendName == "CV_MJPEG" || backendName == "FFMPEG",
                            $"Unexpected VideoWriter backend: {backendName}");

                Assert.Equal(3, capture.FrameCount);

                using var frame1 = new Mat();
                using var frame2 = new Mat();
                using var frame3 = new Mat();
                using var frame4 = new Mat();
                Assert.True(capture.Read(frame1));
                Assert.True(capture.Read(frame2));
                Assert.True(capture.Read(frame3));
                Assert.False(capture.Read(frame4));
                Assert.False(frame1.Empty());
                Assert.False(frame2.Empty());
                Assert.False(frame3.Empty());
                Assert.True(frame4.Empty());

                Assert.Equal(image.Size(), frame1.Size());
            }
            finally
            {
                DeleteFile(fileName);
            }
        }
Beispiel #7
0
        public void GetSetOption()
        {
            const string fileName = "dummy2.avi";

            try
            {
                using var writer = new VideoWriter(fileName, VideoCaptureAPIs.OPENCV_MJPEG, FourCC.MJPG, 10, new Size(640, 480));
                Assert.True(writer.IsOpened());
                Assert.Equal("CV_MJPEG", writer.GetBackendName());

                Assert.True(writer.Set(VideoWriterProperties.Quality, 50), "VideoWriter.Set failed");
                Assert.Equal(50, writer.Get(VideoWriterProperties.Quality), 3);
            }
            finally
            {
                DeleteFile(fileName);
            }
        }
Beispiel #8
0
        private void ConvertButton_Click(object sender, EventArgs e)
        {
            convertButton.Enabled = false;

            notification.Text = "click convert button\n";

            // target
            file.TargetPath   = targetPath;
            notification.Text = "target path\n";

            // output
            file.OutputPath   = outputPath;
            notification.Text = "output path\n";

            // filename
            String fileName = fileNameInput.Text;

            if (fileNameInput.Equals(null))
            {
                fileName = "image";
            }

            file.FileName     = fileName;
            notification.Text = "file name\n";

            // FPS
            float fPS;

            if (imageperFrameInput.Text.Equals(null))
            {
                fPS = 5;
            }
            else
            {
                fPS = float.Parse(imageperFrameInput.Text);
            }

            file.FPS          = fPS;
            notification.Text = "fps\n";


            // output format
            String format = formatDropdown.SelectedItem.ToString().Split('(')[0];

            file.OutputFormat = "." + format;
            notification.Text = "output format : " + file.OutputFormat + "\n";

            switch (currentMode)
            {
            case (int)MODE.videotoImage:
                var capture = new VideoCapture(file.TargetPath[0]);

                // max frame
                int maxFrame = (int)Math.Round(capture.Fps / file.FPS);
                notification.Text = "videoFrame : " + capture.Fps + "\nfileFPS : " + file.FPS + "\nmaxFrame : " + maxFrame;
                if (maxFrame < 1)
                {
                    maxFrame = 1;
                }

                // size
                System.Drawing.Size size = new System.Drawing.Size();

                if (!checkBox.Checked && !widthInput.Text.Equals(null))
                {
                    size.Width = Int32.Parse(widthInput.Text);
                }
                else
                {
                    size.Width = capture.FrameWidth;
                }
                notification.Text = "width\n";

                if (!checkBox.Checked && !heightInput.Text.Equals(null))
                {
                    size.Height = Int32.Parse(heightInput.Text);
                }
                else
                {
                    size.Height = capture.FrameHeight;
                }
                notification.Text = "height\n";

                file.Size = size;

                bool flag = false;
                using (Mat image = new Mat())
                {
                    while (!flag)
                    {
                        notification.Text = "capture . . . " + count.ToString("D5");

                        for (int i = 0; i < maxFrame; i++)
                        {
                            capture.Read(image);

                            Mat resizing            = new Mat();
                            OpenCvSharp.Size resize = new OpenCvSharp.Size(file.Size.Width, file.Size.Height);
                            //Cv2.Resize(image, resizing, resize);

                            if (image.Empty())
                            {
                                flag = true;
                                break;
                            }
                            else if (i == 0)
                            {
                                //Cv2.ImWrite(@outputPath + "\\" + fileName + (++count).ToString("D5") + file.OutputFormat, resizing);
                                Cv2.ImWrite(@outputPath + "\\" + (++count).ToString("D5") + file.OutputFormat, image);
                                //samplePictureBox.Image.Save(outputPath + "\\" + fileName + (++count).ToString("D5") + ".png" , System.Drawing.Imaging.ImageFormat.Png);
                                //bitmapImage.Save(@outputPath + "\\" + fileName + count.ToString("D5") + ".bmp");
                            }
                        }

                        if (image.Empty())
                        {
                            break;
                        }
                    }
                }
                notification.Text  = "success! \n";
                notification.Text += "videoFrame : " + capture.Fps + "\nfileFPS : " + file.FPS + "\nmaxFrame : " + maxFrame;
                break;

            case (int)MODE.imagetoVideo:
                notification.Text  = "length : " + file.TargetPath.Length;
                notification.Text += "\nlink : " + @outputPath + "\\" + fileName + file.OutputFormat;
                notification.Text += "\nlink : " + file.TargetPath[3];

                // size
                System.Drawing.Size size2     = new System.Drawing.Size();
                OpenCvSharp.Size    videoSize = new OpenCvSharp.Size();

                if (!checkBox.Checked && !widthInput.Text.Equals(null))
                {
                    videoSize.Width = Int32.Parse(widthInput.Text);
                }
                else
                {
                    videoSize.Width = Cv2.ImRead(file.TargetPath[0]).Width;
                }
                notification.Text = "width\n";

                if (!checkBox.Checked && !heightInput.Text.Equals(null))
                {
                    videoSize.Height = Int32.Parse(heightInput.Text);
                }
                else
                {
                    videoSize.Height = Cv2.ImRead(file.TargetPath[0]).Height;
                }
                notification.Text = "height\n";

                size2.Width  = videoSize.Width;
                size2.Height = videoSize.Height;

                file.Size = size2;

                notification.Text = "size : " + videoSize.Width + ", " + videoSize.Height;

                VideoWriter videoWriter = new VideoWriter(@outputPath + "\\" + fileName + ".avi", FourCC.Default, file.FPS, videoSize);
                {
                    if (!videoWriter.IsOpened())
                    {
                        notification.Text = "could not open the output video file for writer\n";
                        return;
                    }

                    //videoWriter.Open(@"F:\20190709\abc.avi", "DIVX", file.FPS, videoSize);

                    notification.Text = "make video . . . ";

                    for (int i = 0; i < file.TargetPath.Length; i++)
                    {
                        Mat mat = Cv2.ImRead(file.TargetPath[i]);
                        if (mat.Empty())
                        {
                            break;
                        }

                        Mat resizing = new Mat();
                        Cv2.Resize(mat, resizing, videoSize);

                        videoWriter.Write(resizing);
                        samplePictureBox.Image = MatToBitmap(resizing);
                    }
                    videoWriter.Release();
                }
                notification.Text += "\nsuccess! \n";
                break;
            }

            convertButton.Enabled = true;
        }
        static void Main(string[] args)
        {
            var afWindow = new Window("Annotated Frame");
            var cdWindow = new Window("Contour Delta");

            VideoCapture capture = new VideoCapture("rtsp://10.0.0.104:554/1/h264major");

            int         frameIndex = 0;
            Mat         lastFrame  = new Mat();
            VideoWriter writer     = null;

            while (capture.IsOpened())
            {
                Mat frame = new Mat();

                if (!capture.Read(frame))
                {
                    break;
                }

                Mat grayFrame, dilatedFrame, edges, deltaCopyFrame = new Mat();
                Mat deltaFrame = new Mat();

                try
                {
                    frame = frame.Resize(new Size(0, 0), 0.33, 0.33);
                }
                catch (Exception e)
                {
                }
                grayFrame = frame.CvtColor(ColorConversionCodes.BGR2GRAY);
                grayFrame = grayFrame.GaussianBlur(new Size(21, 21), 0);

                if (frameIndex == 0)
                {
                    frameIndex++;

                    afWindow.Move(0, 0);
                    cdWindow.Move(0, grayFrame.Size().Height);

                    string fileName = "C:\\temp\\capture.avi";

                    string fcc = capture.FourCC;
                    double fps = capture.Get(CaptureProperty.Fps);

                    Size frameSize = new Size(grayFrame.Size().Width, grayFrame.Size().Height);

                    writer = new VideoWriter(fileName, fcc, fps, frameSize);
                    Console.Out.WriteLine("Frame Size = " + grayFrame.Size().Width + " x " + grayFrame.Size().Height);

                    if (!writer.IsOpened())
                    {
                        Console.Out.WriteLine("Error Opening Video File For Write");
                        return;
                    }

                    lastFrame = grayFrame;
                    continue;
                }
                else if (frameIndex % 50 == 0)
                {
                    frameIndex = 0;
                    lastFrame  = grayFrame;
                }

                frameIndex++;

                Cv2.Absdiff(lastFrame, grayFrame, deltaFrame);
                Cv2.Threshold(deltaFrame, deltaFrame, 50, 255, ThresholdTypes.Binary);

                int iterations = 2;
                Cv2.Dilate(deltaFrame, deltaFrame, new Mat(), new Point(), iterations);

                Point[][]        contours;
                HierarchyIndex[] hierarchy;

                Cv2.FindContours(deltaFrame, out contours, out hierarchy, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple, new Point(0, 0));

                var            countorsPoly = new Point[contours.Length][];
                List <Rect>    boundRect    = new List <Rect>();
                List <Point2f> center       = new List <Point2f>();
                List <float>   radius       = new List <float>();

                for (int i = 0; i < contours.Length; i++)
                {
                    countorsPoly[i] = Cv2.ApproxPolyDP(contours[i], 3, true);
                    if (countorsPoly.Length != 0)
                    {
                        boundRect.Insert(i, Cv2.BoundingRect(countorsPoly[i]));
                        Cv2.MinEnclosingCircle(countorsPoly[i], out Point2f centerObj, out float radiusObj);
                        center.Insert(i, centerObj);
                        radius.Insert(i, radiusObj);
                    }
                }

                for (int i = 0; i < contours.Length; i++)
                {
                    if (countorsPoly.Length != 0)
                    {
                        Scalar color = new Scalar(54, 67, 244);
                        //Cv2.DrawContours(frame, countorsPoly, i, color, 1, LineTypes.Link8, new HierarchyIndex[] { }, 0, new Point());
                        Cv2.Rectangle(frame, boundRect[i].TopLeft, boundRect[i].BottomRight, color, 2, LineTypes.Link8, 0);
                        //Cv2.Circle(frame, (int)center[i].X, (int)center[i].Y, (int)radius[i], color, 2, LineTypes.Link8, 0);
                    }
                }

                afWindow.ShowImage(frame);
                cdWindow.ShowImage(deltaFrame);

                writer.Write(frame);

                switch (Cv2.WaitKey(1))
                {
                case 27:
                    capture.Release();
                    writer.Release();
                    return;
                }
            }
        }