Door blockage detection using OpenCV

I’m trying to use computer vision techniques to identify scene changes for a fixed camera. Something like this:

What are some approaches to do this? Can we use OpenCV to do that? Any ideas?

The scene is static. Somewhere I saw a user trains the app with multiple images of normal scenes, and the app monitors the amount of pixel change from the trained images. And it issues an alarm when the amount of pixel change exceeds a threshold for a specified time period. But it shouldsn’t be that simple. A human may walk around or shadows and sunlight shouldn’t be a scene change and thus, will not be an anomaly.

# Python code for Background subtraction using OpenCV
import numpy as np
import cv2

cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2()

while (1):
    ret, frame =

    fgmask = fgbg.apply(frame)

    cv2.imshow('fgmask', fgmask)
    cv2.imshow('frame', frame)

    k = cv2.waitKey(30) & 0xff
    if k == 27:


@t.jasmin111 these two posts are not addressed to you or your question.

@berak I’ve dealt with the issue. the system flagged several of that user’s posts, which frequently contained youtube links and links to the same thread on some other forum. this post here may be useful (i’ve restored it) but the general contributions were a chore to read.

1 Like

back to the topic…

yes, background subtraction can help, but that class of algorithms only detects changes. there are no semantics in background subtraction. lighting changes are a big issue for background subtraction.

you could train background subtraction once, then freeze it.

you’d collect training data, i.e. a varied set of pictures (different lighting), not just one video where adjacent frames mostly look the same.

when the bgsub (even the naive ones in opencv) was “trained” on this, you’d set the training rate to zero.

I don’t think it’s that simple. I need bounding boxes of changed object. Plus, lighting changes and humans walking shouldn’t be captured. So some temporal processing as well.

Ok. But do we have any pre-trained models for that? Altenratively, are there any general object detection model that can detect ANY objects? I can use it to understand what object is missing/added later.

I saw a camera detecting door blockage. Basically if the exit door is blocked by an object, it detects it as an anomaly. How can we do it? Is it possible to do it using OpenCV?

Remember, it doesn’t catch light or shadow changes, so it understands object blockage.

Sure. Instead of detecting a thing that blocks the door it would be easier the check that the camera sees the whole blue door.

How to detect the door? It’s not always blue and like this!

so you have multiple doors, or these people paint that door a different color every day?

Of course, I’m not going to detect only this door! Any door of any colors/shapes. But most likely just one door. I can take a few initial images of the scene to setup the detections, if needed

May be if your problem is well defined there is no need of “of course”


Use Canny Edge Detection for the “good state” template and a difference sample - a door and the cross hatch will have detectable edges, generally no matter the lighting or time of day.

Some video motion detection (optical flow?) can filter out when you “test” for the expected edges - a time-out would reduce false alarms, for example 1-2 minutes (adjustable) of “no motion” in the ROI before testing for the edges should eliminate people standing in your detection zone. You can take a dense optical flow evaluation of the video to create a map of motion in the frame and then also apply Canny Edge Detection to the resultant output of the optical flow process to create some boundaries on moving objects in the video frame - correlation between edges in the original image and edges from the optical flow motion detection give a fairly high probability of that being a solid object - one can then use that resultant solid object for other interesting things like video tripwires, and all kinds of useful things for security/safety purposes.

Item abandoned, item taken, entering or exiting a space (and counting), and on and on.