当前位置: 首页>>代码示例>>Python>>正文


Python cv2.BackgroundSubtractorMOG方法代码示例

本文整理汇总了Python中cv2.BackgroundSubtractorMOG方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.BackgroundSubtractorMOG方法的具体用法?Python cv2.BackgroundSubtractorMOG怎么用?Python cv2.BackgroundSubtractorMOG使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cv2的用法示例。


在下文中一共展示了cv2.BackgroundSubtractorMOG方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def __init__(self, history = 200, nMixtures = 5, backgroundRatio = 0.7, noiseSigma = 15, learningRate = 0.7):
        
        try:
            import cv2            
        except ImportError:
            raise ImportError("Cannot load OpenCV library which is required by SimpleCV")
            return    
        if not hasattr(cv2, 'BackgroundSubtractorMOG'):
            raise ImportError("A newer version of OpenCV is needed")
            return            
        
        self.mError = False
        self.mReady = False        
        self.mDiffImg = None
        self.mColorImg = None
        self.mBlobMaker = BlobMaker()
        
        self.history = history
        self.nMixtures = nMixtures
        self.backgroundRatio = backgroundRatio
        self.noiseSigma = noiseSigma
        self.learningRate = learningRate
        
        self.mBSMOG = cv2.BackgroundSubtractorMOG(history, nMixtures, backgroundRatio, noiseSigma) 
开发者ID:sightmachine,项目名称:SimpleCV2,代码行数:26,代码来源:MOGSegmentation.py

示例2: __init__

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def __init__ ( self ):
        super( BackgroundRemove, self ).__init__()
        self._name = "Background Remove"

        self._speed = 0.01
        self._avg = None

        self._fgbg = cv2.BackgroundSubtractorMOG() 
开发者ID:jchrisweaver,项目名称:vidpipe,代码行数:10,代码来源:BackgroundRemove.py

示例3: __init__

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def __init__(self, history=10, numberMixtures=3, backgroundRatio=0.6, noise=20):
        """Init the color detector object.

        @param history lenght of the history
        @param numberMixtures The maximum number of Gaussian Mixture components allowed.
            Each pixel in the scene is modelled by a mixture of K Gaussian distributions.
            This value should be a small number from 3 to 5.
        @param backgroundRation define a threshold which specifies if a component has to be included
            into the foreground or not. It is the minimum fraction of the background model. 
            In other words, it is the minimum prior probability that the background is in the scene.
        @param noise specifies the noise strenght
        """
        self.BackgroundSubtractorMOG = cv2.BackgroundSubtractorMOG(history, numberMixtures, backgroundRatio, noise) 
开发者ID:mpatacchiola,项目名称:deepgaze,代码行数:15,代码来源:motion_detection.py

示例4: returnMask

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def returnMask(self, foreground_image):
        """Return the binary image after the detection process

        @param foreground_image the frame to check
        @param threshold the value used for filtering the pixels after the absdiff
        """
        return self.BackgroundSubtractorMOG.apply(foreground_image) 
开发者ID:mpatacchiola,项目名称:deepgaze,代码行数:9,代码来源:motion_detection.py

示例5: background_subtraction

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def background_subtraction(background_image, foreground_image):
    """Creates a binary image from a background subtraction of the foreground using cv2.BackgroundSubtractorMOG().
    The binary image returned is a mask that should contain mostly foreground pixels.
    The background image should be the same background as the foreground image except not containing the object
    of interest.

    Images must be of the same size and type.
    If not, larger image will be taken and downsampled to smaller image size.
    If they are of different types, an error will occur.

    Inputs:
    background_image       = img object, RGB or binary/grayscale/single-channel
    foreground_image       = img object, RGB or binary/grayscale/single-channel

    Returns:
    fgmask                 = background subtracted foreground image (mask)

    :param background_image: numpy.ndarray
    :param foreground_image: numpy.ndarray
    :return fgmask: numpy.ndarray
    """

    params.device += 1
    # Copying images to make sure not alter originals
    bg_img = np.copy(background_image)
    fg_img = np.copy(foreground_image)
    # Checking if images need to be resized or error raised
    if bg_img.shape != fg_img.shape:
        # If both images are not 3 channel or single channel then raise error.
        if len(bg_img.shape) != len(fg_img.shape):
            fatal_error("Images must both be single-channel/grayscale/binary or RGB")
        # Forcibly resizing largest image to smallest image
        print("WARNING: Images are not of same size.\nResizing")
        if bg_img.shape > fg_img.shape:
            width, height = fg_img.shape[1], fg_img.shape[0]
            bg_img = cv2.resize(bg_img, (width, height), interpolation=cv2.INTER_AREA)
        else:
            width, height = bg_img.shape[1], bg_img.shape[0]
            fg_img = cv2.resize(fg_img, (width, height), interpolation=cv2.INTER_AREA)

    bgsub = cv2.createBackgroundSubtractorMOG2()
    # Applying the background image to the background subtractor first.
    # Anything added after is subtracted from the previous iterations.
    _ = bgsub.apply(bg_img)
    # Applying the foreground image to the background subtractor (therefore removing the background)
    fgmask = bgsub.apply(fg_img)

    # Debug options
    if params.debug == "print":
        print_image(fgmask, os.path.join(params.debug_outdir, str(params.device) + "_background_subtraction.png"))
    elif params.debug == "plot":
        plot_image(fgmask, cmap="gray")

    return fgmask 
开发者ID:danforthcenter,项目名称:plantcv,代码行数:56,代码来源:background_subtraction.py

示例6: __init__

# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import BackgroundSubtractorMOG [as 别名]
def __init__(self, debug = False):
        
        self.capture = cv2.VideoCapture(0)
        if self.capture.isOpened():         # Checks the stream
            self.frameSize = (int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)),
                               int(self.capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)))        
        Constants.SCREEN_HEIGHT = self.frameSize[0]
        Constants.SCREEN_WIDTH = self.frameSize[1]
        self.bsmog = []
        self.bgAdapt = []

        history = 100 
        nGauss = 20 
        bgThresh = 0.2
        noise = 7
        
        for i in range(0,4):
            #self.bsmog.append(cv2.BackgroundSubtractorMOG())
            self.bsmog.append(cv2.BackgroundSubtractorMOG(history,nGauss,bgThresh,noise))
            self.bgAdapt.append(Constants.BG_ADAPT)
        
        self.debug = debug
        
        self.debugWindow0 = "Debug Window 0"
        self.debugWindow1 = "Debug Window 1"
        self.debugWindow2 = "Debug Window 2"
        self.debugWindow3 = "Debug Window 3"
        self.debugWindow4 = "Debug Window 4"
        self.debugWindow5 = "Debug Window 5"
        
        if self.debug:
            cv2.namedWindow(self.debugWindow0)
            cv2.namedWindow(self.debugWindow1)
            cv2.namedWindow(self.debugWindow2)
            cv2.namedWindow(self.debugWindow3)
            cv2.namedWindow(self.debugWindow4)
            cv2.namedWindow(self.debugWindow5)
        
        result, self.currentFrame = self.capture.read()        
        self.currentFrame = cv2.flip(self.currentFrame, 1)
        self.previousState = []
        self.currentState = []
        for i in range(0,4):
            self.saveBackground(self.currentFrame, i)
            self.previousState.append(False)
            self.currentState.append(False)
        self.t = False 
开发者ID:stbnps,项目名称:DanceCV,代码行数:49,代码来源:Input.py


注:本文中的cv2.BackgroundSubtractorMOG方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。