用?OpenCVSharp 4.5?跑一遍?OpenCV?官方教程。
原?OpenCV?官方教程链接:OpenCV: How to Use Background Subtraction Methods
using System;
using OpenCvSharp;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class tutorial61:ITutorial
{
public void Run()
{
string videoFile = @"I:\csharp\videos\vtest.avi";
BackgroundSubtractorMOG pBackSub = BackgroundSubtractorMOG.Create();
//BackgroundSubtractorKNN pbAckSub_knn = BackgroundSubtractorKNN.Create();
VideoCapture capture = new VideoCapture(videoFile);
Mat frame = new Mat();
Mat fgMask = new Mat();
while(true)
{
capture.Read(frame);
if (frame.Empty())
break;
pBackSub.Apply(frame, fgMask);
Cv2.Rectangle(frame, new Point(10, 2), new Point(100, 20), Scalar.White, -1);
double ss = capture.Get(VideoCaptureProperties.PosFrames);
Cv2.PutText(frame, ss.ToString(), new Point(15, 15), HersheyFonts.HersheySimplex, 0.5, Scalar.Black);
Cv2.ImShow("Frame", frame);
Cv2.ImShow("FG Mask", fgMask);
int key = Cv2.WaitKey(30);
if (key == 'q' || key == 27)
break;
}
}
}
}
?
|