Last active
August 29, 2015 14:17
-
-
Save 0V/6f50d6d5d210b8d8cf6e to your computer and use it in GitHub Desktop.
OpenCvSharp で動的背景差分検出(From Web Camera)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using OpenCvSharp.CPlusPlus; | |
using System; | |
namespace OpenCvSharpSample | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DynamicBackgroundSubtractorCapture(0); | |
} | |
public static void DynamicBackgroundSubtractorCapture(int cameraId) | |
{ | |
using (var capture = new VideoCapture(cameraId)) | |
using (var captureWindow = new Window("Capture #" + cameraId)) | |
using (var resultGMGWindow = new Window("Capture #GMG #" + cameraId)) | |
using (var resultMoGWindow = new Window("Capture #MoG #" + cameraId)) | |
using (var resultMoG2Window = new Window("Capture #MoG2 #" + cameraId)) | |
using (var subtractorGMG = new BackgroundSubtractorGMG()) | |
using (var subtractorMoG = new BackgroundSubtractorMOG()) | |
using (var subtractorMoG2 = new BackgroundSubtractorMOG2()) | |
{ | |
if (!capture.IsOpened()) | |
{ | |
throw new InvalidOperationException("カメラが開けませんでした。"); | |
} | |
var mat = new Mat(); | |
var outputMat = new Mat(); | |
capture.FrameHeight = 640; | |
capture.FrameWidth = 480; | |
while (Cv2.WaitKey(1) < 0) | |
{ | |
capture.Read(mat); | |
captureWindow.ShowImage(mat); | |
subtractorGMG.Run(mat, outputMat); | |
// Cv2.BitwiseAnd(mat,outputMat.CvtColor(ColorConversion.GrayToBgr),outputMat); | |
resultGMGWindow.ShowImage(outputMat); | |
subtractorMoG.Run(mat, outputMat); | |
// Cv2.BitwiseAnd(mat, outputMat.CvtColor(ColorConversion.GrayToBgr), outputMat); | |
resultMoGWindow.ShowImage(outputMat); | |
subtractorMoG2.Run(mat, outputMat); | |
// Cv2.BitwiseAnd(mat, outputMat.CvtColor(ColorConversion.GrayToBgr), outputMat); | |
resultMoG2Window.ShowImage(outputMat); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment