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; } } } } }
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); }
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(); } }