Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to provide mask to cv2.accumulateWeighted() method

I want to provide mask into the cv2.accumulateWeighted() method. But in the documentation, it does not provide any information regarding mask. Thus, I have tried different variations. But any of them didn't work out.

    import cv2
    import numpy as np


    cascade_src = 'cars.xml'
    cap = cv2.VideoCapture('slow_traffic_small.mp4')
    car_cascade = cv2.CascadeClassifier(cascade_src)
    ret, background = cap.read()

    count = 1
    while(1):
        ret_, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = np.array(frame, dtype=np.float32)
        mask = np.zeros_like(frame)
        if(count == 1):
            des = frame.copy()
        cars = car_cascade.detectMultiScale(gray, 1.1, 1)
        for (x, y, w, h) in cars:
            mask[x:x+w, y:y+h] = 1
        des = cv2.accumulateWeighted(frame, des, 0.001, mask=mask)
        detectedFrame = cv2.absdiff(frame, des)
        cv2.imshow('frame', detectedFrame)
        count = 0
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break

This is the error I am always getting?

  OpenCV Error: Assertion failed (_mask.empty() || (_src.sameSize(_mask) && _mask.type() == 0)) in accumulateWeighted, file /io/opencv/modules/imgproc/src/accum.cpp, line 620

I also asked in this SO

How to provide mask to cv2.accumulateWeighted() method

I want to provide mask into the cv2.accumulateWeighted() method. But in the documentation, it does not provide any information regarding mask. Thus, I have tried different variations. But any of them didn't work out.

    import cv2
    import numpy as np


    cascade_src = 'cars.xml'
    cap = cv2.VideoCapture('slow_traffic_small.mp4')
    car_cascade = cv2.CascadeClassifier(cascade_src)
    ret, background = cap.read()

    count = 1
    while(1):
        ret_, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        frame = np.array(frame, dtype=np.float32)
        mask = np.zeros_like(frame)
        if(count == 1):
            des = frame.copy()
        cars = car_cascade.detectMultiScale(gray, 1.1, 1)
        for (x, y, w, h) in cars:
            mask[x:x+w, y:y+h] = 1
        des = cv2.accumulateWeighted(frame, des, 0.001, mask=mask)
        detectedFrame = cv2.absdiff(frame, des)
        cv2.imshow('frame', detectedFrame)
        count = 0
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break

This is the error I am always getting?

  OpenCV Error: Assertion failed (_mask.empty() || (_src.sameSize(_mask) && _mask.type() == 0)) in accumulateWeighted, file /io/opencv/modules/imgproc/src/accum.cpp, line 620

I also asked in this SO