using System; using System.Collections.Generic; using System.Linq; using System.Text; using OpenCvSharp; using System.Threading; namespace OpenCVSandbox { class Program { public static void Main(string[] args) { CvWindow rawVideo = new CvWindow("RawVideo"); CvWindow edgeVideo = new CvWindow("EdgeDetect"); CvWindow contourVideo = new CvWindow("Contours"); CvCapture capture = CvCapture.FromFile(@"E:\svn\Projects\AtariRobot\SampleVideos\PongSample1.avi"); int frameTime = (int)(1000 / capture.Fps); while (Cv.WaitKey(frameTime) < 0) { IplImage frame = capture.QueryFrame(); if (frame == null) { break; } rawVideo.Image = frame; IplImage smoothFrame = new IplImage(frame.Width, frame.Height, frame.Depth, frame.ElemChannels); Cv.Smooth(frame, smoothFrame); IplImage greyFrame = new IplImage(smoothFrame.Width, smoothFrame.Height, BitDepth.U8, 1); Cv.CvtColor(smoothFrame, greyFrame, ColorConversion.RgbToGray); smoothFrame.Dispose(); IplImage cannyFrame = new IplImage(frame.Width, frame.Height, BitDepth.U8, 1); Cv.Canny(greyFrame, cannyFrame, 100, 100); greyFrame.Dispose(); edgeVideo.Image = cannyFrame; CvMemStorage memStorage = new CvMemStorage(); CvSeq contourSet; Cv.FindContours(cannyFrame, memStorage, out contourSet); cannyFrame.Dispose(); IplImage contourFrame = new IplImage(frame.Width, frame.Height, frame.Depth, frame.ElemChannels); contourFrame.Zero(); Console.WriteLine(contourSet.Count()); for (CvSeq currentContour = contourSet; currentContour != null; currentContour = currentContour.HNext) { CvRect bounding = currentContour.BoundingRect(); if (bounding.Width < 3 || bounding.Height < 3) { continue; } Console.WriteLine(bounding); Cv.DrawContours(contourFrame, currentContour, CvColor.Random(), CvColor.Black, 0); } contourVideo.Image = contourFrame; contourFrame.Dispose(); } capture.Dispose(); rawVideo.Dispose(); edgeVideo.Dispose(); contourVideo.Dispose(); } } }