Skip to content
shimat edited this page Jan 19, 2019 · 1 revision
Size dsize = new Size(640, 480);

// Opens a camera device
using (VideoCapture capture = new VideoCapture(0))
// Read movie frames and write them to VideoWriter 
using (VideoWriter writer = new VideoWriter("out.avi", -1, capture.Fps, dsize))
using (Mat frame = new Mat())
using (Mat gray = new Mat())
using (Mat canny = new Mat())
using (Mat dst = new Mat())
{
    Console.WriteLine("Converting each movie frames...");
    while (true)
    {
        // Read image
        capture.Read(frame);
        if (frame.Empty())
            break;

        Console.CursorLeft = 0;
        Console.Write("{0} / {1}", capture.PosFrames, capture.FrameCount);

        // grayscale -> canny -> resize
        Cv2.CvtColor(frame, gray, ColorConversion.BgrToGray);
        Cv2.Canny(gray, canny, 100, 180);
        Cv2.Resize(canny, dst, dsize, 0, 0, Interpolation.Linear);
        // Write mat to VideoWriter
        writer.Write(dst);
    } 
}
Clone this wiki locally