Exemplo n.º 1
0
        public VideoWriter()
        {
            // (1)カメラに対するキャプチャ構造体を作成する
            using (CvCapture capture = CvCapture.FromCamera(0))
            {
                // (2)キャプチャサイズを取得する(この設定は,利用するカメラに依存する)
                int width = capture.FrameWidth;
                int height = capture.FrameHeight;
                double fps = 15;//capture.Fps;
                // (3)ビデオライタ構造体を作成する
                using (CvVideoWriter writer = new CvVideoWriter("cap.avi", FourCC.Prompt, fps, new CvSize(width, height)))
                using (CvFont font = new CvFont(FontFace.HersheyComplex, 0.7, 0.7))
                using (CvWindow window = new CvWindow("Capture", WindowMode.AutoSize))
                {
                    // (4)カメラから画像をキャプチャし,ファイルに書き出す
                    for (int frames = 0; ; frames++)
                    {
                        IplImage frame = capture.QueryFrame();
                        string str = string.Format("{0}[frame]", frames);
                        frame.PutText(str, new CvPoint(10, 20), font, new CvColor(0, 255, 100));
                        writer.WriteFrame(frame);
                        window.ShowImage(frame);

                        int key = CvWindow.WaitKey((int)(1000 / fps));
                        if (key == '\x1b')
                        {
                            break;
                        }
                    }
                }
            }

        }
Exemplo n.º 2
0
        /// <summary>
        /// 一つのフレームをビデオファイルに書き込む/追加する
        /// </summary>
        /// <param name="writer">ビデオライタクラス</param>
        /// <param name="image">書き込まれるフレーム</param>
        /// <returns></returns>
#else
        /// <summary>
        /// Writes/appends one frame to video file.
        /// </summary>
        /// <param name="writer">video writer structure. </param>
        /// <param name="image">the written frame.</param>
        /// <returns></returns>
#endif
        public static int WriteFrame(CvVideoWriter writer, IplImage image)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            return(NativeMethods.cvWriteFrame(writer.CvPtr, image.CvPtr));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 一つのフレームをビデオファイルに書き込む/追加する
        /// </summary>
        /// <param name="writer">ビデオライタクラス</param>
        /// <param name="image">書き込まれるフレーム</param>
        /// <returns></returns>
#else
        /// <summary>
        /// Writes/appends one frame to video file.
        /// </summary>
        /// <param name="writer">video writer structure. </param>
        /// <param name="image">the written frame.</param>
        /// <returns></returns>
#endif
        public static int WriteFrame(CvVideoWriter writer, IplImage image)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (image == null)
            {
                throw new ArgumentNullException("image");
            }
            var ret = NativeMethods.cvWriteFrame(writer.CvPtr, image.CvPtr);

            GC.KeepAlive(writer);
            GC.KeepAlive(image);
            return(ret);
        }
Exemplo n.º 4
0
        /// <summary>
        /// basic constructor
        /// </summary>
        public File(bool _save, bool _read, int _interval)
        {
            if (_read == false)
            {
                if (_save == true)
                {
                    double fps = (double)1000 / _interval;

                    this.writer = new CvVideoWriter(
                        this.saveMachineVisionDialog(),
                        FourCC.MJPG,
                        fps,
                        new CvSize(640, 480)
                        );
                }

                this.cap = CvCapture.FromCamera(CaptureDevice.DShow, 0);
            }
            else
            {
                this.cap = CvCapture.FromFile(this.readMachineVisionDialog());
            }
        }
Exemplo n.º 5
0
        private void FormatVideo()
        {
            cap = CvCapture.FromFile(fileName);
            uLastFrame = (UInt16)cap.FrameCount;
            img = cap.QueryFrame();

            CvVideoWriter newVideo = new CvVideoWriter("videoFormated.avi", FourCC.Default ,cap.Fps, img.Size);

            setProgressBarMargins(0, uLastFrame);

            for (int i = 1; i < uLastFrame; i++ )
            {
                img = cap.QueryFrame();

                if (img == null) break;

                newVideo.WriteFrame(img);
                setProgressBarValue(i);

            }

            newVideo.Dispose();
            setProgressBarValue(0);
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            //  CreateCameraCaptureの引数はカメラのIndex(通常は0から始まる)
            using (var capture = Cv.CreateCameraCapture(0))
            {
                Console.WriteLine("Hit any key to quit");

                /*
                double fps=12.0;
                int interval=1;
                double zoom=1.0;
                string OutputFile;
                */

                double fps ;
                int interval ;
                double zoom=1.0 ;
                string OutputFile;

                var opts = new Options();
                 bool isSuccess = CommandLine.Parser.Default.ParseArguments(args, opts);

                if(!isSuccess)
                {
                    opts.GetUsage();
                    Console.WriteLine(Environment.GetCommandLineArgs()[0] + "  -o Outputfilename(string) -f fps(double) -i CaptureInterval(int)");
                    Environment.Exit(0);
                }

                    fps = opts.fps;
                    interval = opts.interval;
                    zoom = opts.zoom;
                    OutputFile = opts.OutputFile;
                    Console.WriteLine(OutputFile);
                    if (fps > 30 | interval < 0.1)
                    {
                        Console.WriteLine(" :-p");
                        Environment.Exit(1);
                    }

                Int32 codec = 0; // コーデック(AVI)
                IplImage frame = new IplImage();

                /*
                double width = capture.FrameWidth/2;
                double height = capture.FrameHeight/2;

                //double width = 640, height = 240;
                Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, width);
                Cv.SetCaptureProperty(capture, CaptureProperty.FrameHeight, height);
                CvSize size = new CvSize((int)width, (int)height);
                CvVideoWriter vw = new CvVideoWriter(OutputFile, codec, fps, size, true);
                */

                int width = (int)(Cv.GetCaptureProperty(capture, CaptureProperty.FrameWidth)*zoom);
                int height = (int)(Cv.GetCaptureProperty(capture, CaptureProperty.FrameHeight)*zoom);

                //Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, width);
                //Cv.SetCaptureProperty(capture, CaptureProperty.FrameWidth, height);
                //Bitmap bitmap = new Bitmap(width, height);

                CvSize size = new CvSize(width, height);
                CvVideoWriter vw = new CvVideoWriter(OutputFile, codec, fps, size, true);

                //CvFont font = new CvFont(FontFace.HersheyTriplex, 0.7, 0.7);
                //(FontFace.HersheyPlain, 1.0, 1.0, 0, 2);

                double fontSize;
                if(width>600)
                     fontSize=1.0;
                else
                     fontSize=0.5;

                CvFont font = new CvFont(FontFace.HersheyPlain,fontSize,fontSize);

                //  何かキーを押すまでは、Webカメラの画像を表示し続ける
                while (Cv.WaitKey(1) == -1)
                {
                    System.Threading.Thread.Sleep(1000*interval);
                    //  カメラからフレームを取得
                    frame = Cv.QueryFrame(capture);
                    string str = DateTime.Now.ToString();

                    //  Window「Capture」を作って、Webカメラの画像を表示
                    if (frame != null)
                    {
                        frame.PutText(str, new CvPoint(10, 20), font, new CvColor(200,100,50));
                        Cv.ShowImage("Timelapse", frame);
                        //frame.SaveImage("result.bmp");
                       //bitmap = BitmapConverter.ToBitmap(frame);
                        //OpenCvSharp.IplImage ipl2 = (OpenCvSharp.IplImage)BitmapConverter.ToIplImage(bitmap);
                        vw.WriteFrame(frame);
                        // vw.WriteFrame(ipl2);
                        frame.Dispose();
                    }
                }

                Cv.DestroyWindow("Capture");
                vw.Dispose();
            }
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            CvFont font;
            Cv.InitFont(out font, FontFace.HersheyComplex, 0.5, 0.5);//.7 .7
            file.WriteLine("Match Time Per Frame");
            //*******************INPUT PATH SECTION
            using (CvCapture capture = CvCapture.FromFile("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\[ch03]103014_094500__103014_095700decompress-1_WMV V9.wmv"))//Works with wmv
            {

                if (capture == null)
                {
                    Console.WriteLine("ERROR! FILE NOT FOUND!");

                }
                while (true)
                {
                    counter++;
                    IplImage frame = null;
                    int c;
                    frame = capture.QueryFrame();
                    //***********Create the video the same size as the frame
                    if (counter == 1)
                    {
                        double w = frame.Width, h = frame.Height;//This is the width and height for recording.
                        video = Cv.CreateVideoWriter("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\Powerbox Video.avi", "XVID", 30, Cv.Size((int)w, (int)h));
                        //15 fps. The writer for the avi file.
                    }
                    //******************************************
                    if (frame == null)
                        break;
                    if (image == null)
                    {
                        image = Cv.CreateImage(frame.Size, BitDepth.U8, 3);
                        colorImage = Cv.CreateImage(image.Size, BitDepth.U8, 3);
                        gray = Cv.CreateImage(frame.Size, BitDepth.U8, 1);
                        prevGray = Cv.CreateImage(frame.Size, BitDepth.U8, 1);
                    }

                    if (counter == 200)//Saves a picture here for power box
                    {
                        picture = Cv.CreateImage(frame.Size, BitDepth.U8, 3);
                        Cv.Copy(frame, picture);
                        picture.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\PictureforPowerboxTemplate.bmp");//Save Images for Templates here
                        Cv.ShowImage("Picture", picture);

                    }
                    Cv.Copy(frame, image);
                    Cv.Copy(image, colorImage);

                    Cv.CvtColor(image, gray, ColorConversion.BgrToGray);
                    Cv.SWAP(ref prevGray, ref gray);

                    //******************************Template Matching
                    template = new IplImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\" + currentTemplate);
                    maxValueHolder = maxValue;
                    if (counter >= 200 && counter != 1080)//counter >= 520
                    {
                        if (xholder < 0 || yholder < 0)
                            break;
                        if (counter == 1116)//Save an image to use as a template 1260
                            image.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\NEWPowerboxTemplate2.bmp");
                        predictImage = image.GetSubImage(rect2);
                        destSize = new CvSize(predictImage.Width - template.Width + 1, predictImage.Height - template.Height + 1);
                        destImg = new IplImage(destSize, BitDepth.F32, 1);
                        //Measuring time for template matching.
                        watch.Start();
                        predictImage.MatchTemplate(template, destImg, MatchTemplateMethod.CCoeffNormed);
                        watch.Stop();
                        Console.WriteLine("Frame " + counter + " took " + watch.ElapsedMilliseconds + "ms");
                        file.WriteLine("Frame {0}: " + watch.ElapsedMilliseconds + "ms", counter);
                        watch.Reset();
                        //END SECTION
                        destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc);

                        if (currentX > 220 && currentX < 370 && currentY > 170 && currentY < 320)
                        {

                            perimeter = new CvRect(220, 170, 150, 150);
                            securityCounter++;
                            Console.WriteLine(securityCounter);
                            if (securityCounter > 0 && securityCounter <=300)
                            {
                                Cv.PutText(colorImage, "Target in Safe Zone...", new CvPoint(200, 150), font, CvColor.Green);
                                    colorImage.Rectangle(perimeter, CvColor.Green);

                            }
                            else if (securityCounter > 300 && securityCounter <= 600)
                            {
                                Cv.PutText(colorImage, "Possible Threat...", new CvPoint(200, 150), font, CvColor.Yellow);
                                colorImage.Rectangle(perimeter, CvColor.Yellow);

                            }
                            else if (securityCounter > 600)
                            {
                                Cv.PutText(colorImage, "Suspicious Target Detected!!!", new CvPoint(200, 150), font, CvColor.Red);
                                colorImage.Rectangle(perimeter, CvColor.Red);
                                status = "DANGER!";
                                markTarget = CvColor.Red;
                            }
                        }

                        Cv.PutText(colorImage, "(" + (xholder + maxLoc.X).ToString() + ", " +(yholder + maxLoc.Y).ToString() + ") " + status, new CvPoint(xholder + maxLoc.X, (yholder + maxLoc.Y) - 5), font, markTarget);
                        colorImage.Rectangle(xholder + maxLoc.X, yholder + maxLoc.Y, (xholder + maxLoc.X) + template.Width, (yholder + maxLoc.Y) + template.Height, markTarget);
                        picture.Rectangle(xholder + maxLoc.X + template.Width / 2, yholder + maxLoc.X + template.Height / 2, xholder + maxLoc.X + template.Width / 2 + 1, yholder + maxLoc.X + template.Height / 2 + 1, CvColor.Red);//Places dot at the center
                        currentX = xholder + maxLoc.X + template.Width / 2;
                        currentY = yholder + maxLoc.X + template.Height / 2;
                        if (previousX > 0 && previousY > 0)
                            picture.Line(previousX, previousY, currentX, currentY, CvColor.Red);
                        previousX = xholder + maxLoc.X + template.Width / 2;
                        previousY = yholder + maxLoc.X + template.Height / 2;
                        rect2 = new CvRect((xholder + maxLoc.X) - 5, (yholder + maxLoc.Y) - 5, template.Width + 10, template.Height +10);//5 5 10 10
                        Console.WriteLine("{0},{1}", xholder, yholder);
                        xholder = rect2.X;
                        yholder = rect2.Y;
                    }

                    else
                    {

                        destSize = new CvSize(image.Width - template.Width + 1, image.Height - template.Height + 1);
                        destImg = new IplImage(destSize, BitDepth.F32, 1);
                        //Measuring time
                        watch.Start();
                        colorImage.MatchTemplate(template, destImg, MatchTemplateMethod.CCoeffNormed);
                        watch.Stop();
                        Console.WriteLine("Frame " + counter + " took " + watch.ElapsedMilliseconds + "ms");
                        file.WriteLine("Frame {0}: " + watch.ElapsedMilliseconds + "ms", counter);
                        watch.Reset();
                        //End Section
                        destImg.MinMaxLoc(out minValue, out maxValue, out minLoc, out maxLoc);
                        Cv.PutText(colorImage, "(" + maxLoc.X.ToString() + "," + maxLoc.Y.ToString() + ") " + status, new CvPoint(maxLoc.X, maxLoc.Y - 5), font, CvColor.Green);
                        colorImage.Rectangle(maxLoc.X, maxLoc.Y, maxLoc.X + template.Width, maxLoc.Y + template.Height,markTarget);
                        rect2 = new CvRect(maxLoc.X - 10, maxLoc.Y - 10, template.Width + 20, template.Height + 20);//Original prediction size here

                        xholder = rect2.X;
                        yholder = rect2.Y;

                    }
                    if (counter == 403)//Saves a picture here
                    {
                        colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC515.png");
                    }
                    if (counter == 650)//Saves a picture here
                    {
                        colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC516.png");
                    }
                    if (counter == 1000)//Saves a picture here
                    {
                        colorImage.SaveImage("C:\\OpenCvSharp\\FallProgram7Camera3PowerBox\\KC517.png");
                    }
                    rect = new CvRect(maxLoc.X, maxLoc.Y, template.Width, template.Height);
                    newTemplate = image.GetSubImage(rect);
                    Cv.WaitKey(2);
                    if (maxValue >= bestMatch)
                    {
                        Console.WriteLine("NEW BEST MATCH");
                        bestMatch = maxValue;
                        //**********************IMPORTANT SAVES!!!!
                        newTemplate.SaveImage("C://OpenCvSharp//FallProgram7Camera3PowerBox//NEWPowerboxTemplate.bmp");
                        //**********************

                        if(counter > 1050)
                        currentTemplate = "PowerboxTemplate2.bmp";
                        else
                            currentTemplate = "PowerboxTemplate.bmp";

                    }
                    else
                    {
                        //currentTemplate = "S2ABTemplate0.bmp";
                        if (counter > 1050)
                            currentTemplate = "PowerboxTemplate2.bmp";
                        else
                            currentTemplate = "PowerboxTemplate.bmp";
                    }
                    //*************************************************
                    if (counter >= 135)
                        Cv.WriteFrame(video, colorImage);//Writes each frame to an avi file.
                    Cv.ShowImage("color Image", colorImage);

                    //********************IMPORTANT SAVE!!!
                    picture.SaveImage("C:\\OpenCvSharp\\SummerPractice32AVICapture\\Summer Practice 23 Results\\Walking Path.bmp");
                    //********************
                    //Cv.ShowImage("Test", image);

                    c = Cv.WaitKey(5);
                    if ((char)c == 27)
                        break;

                }
                file.Close();
                colorImage.SaveImage("C:\\OpenCvSharp\\SummerPractice32AVICapture\\Summer Practice 23 Results\\Frame.bmp");
                Cv.WaitKey(0);
                Cv.ReleaseVideoWriter(video);
                Cv.ReleaseCapture(capture);
                Cv.DestroyWindow("Test");
            }
        }