OpenCV提供了一个称为BackgroundSubtractor的类,在分割前景和背景时很方便。
在OpenCV3中有三种背景分割器:K-Nearest-Neighbors(KNN)、Mixture of Gaussians(MOG2)、Geometric Multigid(GMG)
背景分割博文1
与OpenCV+Python图像分割区别
BackgroundSubtractor类专门为视频而设计的,在帧之间作比较,并且记录帧历史。该类的另外一个特性是可以计算出阴影区域。
各种背景分割算法比较paper
MOG2
An Improved Adaptive Background Mixture Model for Realtime Tracking with Shadow Detection 2002
Improved Adaptive Gaussian Mixture Model for Background Subtraction 2004 Zoran Zivkovi
def createBackgroundSubtractorMOG2(history=None, varThreshold=None, detectShadows=None):
"""
createBackgroundSubtractorMOG2([, history[, varThreshold[, detectShadows]]]) -> retval
. @brief Creates MOG2 Background Subtractor
.
. @param history Length of the history. history越大,learningRate越小,背景更新越慢
. @param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
. to decide whether a pixel is well described by the background model. This parameter does not
. affect the background update. 方差阈值,用于判断当前像素是前景还是背景
. @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
. speed a bit, so if you do not need this feature, set the parameter to false.
"""
0 背景 127 阴影 255 前景
GMG
def createBackgroundSubtractorGMG(initializationFrames=None, decisionThreshold=None):
"""
createBackgroundSubtractorGMG([, initializationFrames[, decisionThreshold]]) -> retval
. @brief Creates a GMG Background Subtractor
.
. @param initializationFrames number of frames used to initialize the background models.
. @param decisionThreshold Threshold value, above which it is marked foreground, else background.
"""
KNN
def createBackgroundSubtractorKNN(history=None, dist2Threshold=None, detectShadows=None):
"""
createBackgroundSubtractorKNN([, history[, dist2Threshold[, detectShadows]]]) -> retval
. @brief Creates KNN Background Subtractor
.
. @param history Length of the history.
. @param dist2Threshold Threshold on the squared distance between the pixel and the sample to decide
. whether a pixel is close to that sample. This parameter does not affect the background update.
. @param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
. speed a bit, so if you do not need this feature, set the parameter to false.
"""
实例
mog = cv2.createBackgroundSubtractorMOG2()
# gmg = cv2.bgsegm.createBackgroundSubtractorGMG(initializationFrames=1)
# bs = cv2.createBackgroundSubtractorKNN(detectShadows=True)
while self._window_manager.is_window_created:
self._capture_manager.enter_frame()
frame = self._capture_manager.frame
gmask = mog.apply(frame)
binary = cv2.threshold(gmask.copy(), 244, 255, cv2.THRESH_BINARY)[1]
dilated = cv2.dilate(binary, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)), iterations=2)
image, contours, hier = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c) > 1600:
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
self._capture_manager.frame = frame
网友评论