Ask Your Question
0

How to provide mask to cv2.accumulateWeighted() method

asked 2018-04-02 01:29:57 -0600

GPrathap gravatar image

updated 2018-04-02 01:31:43 -0600

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

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-04-02 03:15:04 -0600

berak gravatar image

updated 2018-04-02 03:16:13 -0600

you need a uchar mask, not an float64 one, so make it:

mask = np.zeros_like(frame, dtype=np.uint8)

edit flag offensive delete link more

Comments

Noo this does not work, I tried both of them. Neither of them didn't work. This is the error it is being got,

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

Traceback (most recent call last):

GPrathap gravatar imageGPrathap ( 2018-05-06 09:27:34 -0600 )edit

it means, your frame is empty. because the movie's over.

please check the ret value from cap.read(), ALWAYS:

berak gravatar imageberak ( 2018-05-06 09:56:04 -0600 )edit

Hi @berak, noo when I remove mask it worked, when I add mask it is trowing above exception. I am using opencv - 3.4.0 as well.

GPrathap gravatar imageGPrathap ( 2018-05-06 10:03:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-02 01:29:57 -0600

Seen: 843 times

Last updated: Apr 02 '18