forked from citrusvanilla/multiplewavetracking_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwt_preprocessing.py
87 lines (63 loc) · 2.24 KB
/
mwt_preprocessing.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
##
## Near-shore Wave Tracking
## mwt_preprocessing.py
##
## Created by Justin Fung on 9/1/17.
## Copyright 2017 justin fung. All rights reserved.
##
## ========================================================
"""Routine for preprocessing video frames.
Method of preprocessing is:
-1. resize image
-2. extract foreground
-3. denoise image
"""
from __future__ import division
import cv2
# Resize factor (downsize) for analysis:
RESIZE_FACTOR = 0.25
# Number of frames that constitute the background history:
BACKGROUND_HISTORY = 900
# Number of gaussians in BG mixture model:
NUM_GAUSSIANS = 5
# Minimum percent of frame considered background:
BACKGROUND_RATIO = 0.7
# Morphological kernel size (square):
MORPH_KERN_SIZE = 3
# Init the background modeling and foreground extraction mask.
mask = cv2.bgsegm.createBackgroundSubtractorMOG(
history=BACKGROUND_HISTORY,
nmixtures=NUM_GAUSSIANS,
backgroundRatio=BACKGROUND_RATIO,
noiseSigma=0)
# Init the morphological transformations for denoising kernel.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,
(MORPH_KERN_SIZE, MORPH_KERN_SIZE))
def _resize(frame):
"""Resizing function utilizing OpenCV.
Args:
frame: A frame from a cv2.video_reader object to process
Returns:
resized_frame: the frame, resized
"""
resized_frame = cv2.resize(frame,
None,
fx=RESIZE_FACTOR,
fy=RESIZE_FACTOR,
interpolation=cv2.INTER_AREA)
return resized_frame
def preprocess(frame):
"""Preprocesses video frames through resizing, background
modeling, and denoising.
Args:
input: A frame from a cv2.video_reader object to process
Returns:
output: the preprocessed frame
"""
# 1. Resize the input.
output = _resize(frame)
# 2. Model the background and extract the foreground with a mask.
output = mask.apply(output)
# 3. Apply the morphological operators to suppress noise.
output = cv2.morphologyEx(output, cv2.MORPH_OPEN, kernel)
return output