LoginSignup
3
7

More than 5 years have passed since last update.

背景差分, python, mog, gmg, opencv3.1

Posted at

ネットで書かれているものだとそのまま使えなかったのでちょっとだけ.
bgsegmを追加しただけですが.
mog2は何故か上手くいかなかったのでなし.

環境

Python 3.5.2
opencv '3.1.0'

check方法 (python)

$ python --version
Python 3.5.2 :: Anaconda 2.4.1 (x86_64)

check方法 (opencv)

$ python
Python 3.5.2 |Anaconda 2.4.1 (x86_64)| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2 
>>> cv2.__version__
'3.1.0'

codes

mog.py
# coding=utf-8
import cv2
cap = cv2.VideoCapture(0)
fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()

while True:
    ret, src = cap.read()
    fgmask = fgbg.apply(src, learningRate=0.01)
    dst = src.copy()
    dst = cv2.bitwise_and(src, src, mask=fgmask)

    cv2.imshow('frame',dst)

    k = cv2.waitKey(30) & 0xff
    if k == 27:  # ESC key
        break
cap.release()
cv2.destroyAllWindows()
gmg.py
# coding=utf-8
import cv2
cap = cv2.VideoCapture(0)
fgbg = cv2.bgsegm.createBackgroundSubtractorGMG()

while True:
    ret, src = cap.read()
    fgmask = fgbg.apply(src, learningRate=0.01)
    dst = src.copy()
    dst = cv2.bitwise_and(src, src, mask=fgmask)

    cv2.imshow('frame',dst)

    k = cv2.waitKey(30) & 0xff
    if k == 27:  # ESC key
        break
cap.release()
cv2.destroyAllWindows()

results

mog
https://youtu.be/GSFa-6DObLI

gmg
https://youtu.be/Qaq1Y8w5Bjk

refs

check opencv ver
http://qiita.com/PeaceAndHiLight/items/8372c5719ca73aa11d46

3
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
7