-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathEx6MOGMotionDetector.scala
72 lines (53 loc) · 2.11 KB
/
Ex6MOGMotionDetector.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* Copyright (c) 2011-2019 Jarek Sacha. All Rights Reserved.
*
* Author's e-mail: jpsacha at gmail.com
*/
package opencv_cookbook.chapter11
import opencv_cookbook.OpenCVUtils._
import org.bytedeco.javacv.{CanvasFrame, FFmpegFrameGrabber, OpenCVFrameConverter}
import org.bytedeco.opencv.global.opencv_imgproc._
import org.bytedeco.opencv.global.opencv_video._
import org.bytedeco.opencv.opencv_core._
import javax.swing.WindowConstants
import scala.util.Using
/** The second example example for section "Extracting the foreground objects in video" in Chapter 10, page 272.
*
* This version of foreground segmentation is using a more elaborate approach based on modeling background as a
* mixture of Gaussians. It is using OpenCV class `BackgroundSubtractorMOG`.
*
* This version of the example is implemented using OpenCV C API.
*
* @see opencv_cookbook.chapter11.Ex1ReadVideoSequence
*/
object Ex6MOGMotionDetector extends App {
// Open video video file
val grabber = new FFmpegFrameGrabber("data/bike.avi")
// Open video video file
grabber.start()
// Prepare window to display frames
val canvasFrame = new CanvasFrame("Extracted Foreground")
canvasFrame.setCanvasSize(grabber.getImageWidth, grabber.getImageHeight)
// Exit the example when the canvas frame is closed
canvasFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
// Time between frames in the video
val delay = math.round(1000d / grabber.getFrameRate)
// foreground binary image
val foreground = new Mat()
Using.resource(new OpenCVFrameConverter.ToMat()) { frameConverter =>
// Mixture of Gaussians approach
val mog = createBackgroundSubtractorMOG2()
for (frame <- Iterator.continually(grabber.grab()).takeWhile(_ != null)) {
val inputMat = frameConverter.convert(frame)
// update the background
// and return the foreground
mog(inputMat, foreground, 0.01)
// Complement the image
threshold(foreground, foreground, 128, 255, THRESH_BINARY_INV)
canvasFrame.showImage(toBufferedImage(foreground))
// Delay
Thread.sleep(delay)
}
}
grabber.release()
}