Detect if a set pixels crossing the boundary of the bounding box

asked 2019-10-08 02:23:55 -0500

khaalidi gravatar image

updated 2019-10-08 03:43:52 -0500

I'm trying to to do litterbug detection on a highway, and for that I want to detect the cars first, and then If a set of pixels were detected crossing the boundary of the bounding box, it should be marked as litter. But I don't know how to detect this when when an object crossing the bounding box in OpenCV.

import cv2
import numpy as np

cap = cv2.VideoCapture('CarsDrivingUnderBridge.mp4')
fgbg =  cv2.bgsegm.createBackgroundSubtractorMOG()

while True:
    ret,frame =
    fgmask = fgbg.apply(frame)

    if not ret:
    frame_r = cv2.resize(frame, (640, 480))
    fgmask_r = cv2.resize(fgmask, (640, 480))

    contours,h = cv2.findContours(fgmask_r, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_SIMPLE)
    # contours,h = cv2.findContours(fgmask_r, cv2.RETR_TREE , cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contours:
        area = cv2.contourArea(cnt)
        # print (area)
        if area <100:
        x,y,w,h = cv2.boundingRect(cnt)
        offset = 3    

    cv2.imshow('Origional', frame_r)
    cv2.imshow('fg', fgmask_r)

    if cv2.waitKey(75) & 0xFF == ord('q'):

image description

edit retag flag offensive close merge delete



you probably won't even see any litter on the street, due to the background subtraction (it does not move, or ??)

berak gravatar imageberak ( 2019-10-08 02:48:32 -0500 )edit

detected crossing the boundary of the bounding box,

which bounding box ?

berak gravatar imageberak ( 2019-10-08 02:54:01 -0500 )edit

The litter should move as it will be thrown from the cars. And the bounding box around the car.

khaalidi gravatar imagekhaalidi ( 2019-10-08 03:07:58 -0500 )edit

don't you think, you explicitly need to detect cars , then ? or is it only a matter of size ?

berak gravatar imageberak ( 2019-10-08 03:11:46 -0500 )edit

No, I can detect cars well. What I want is If a set of pixels were detected crossing the boundary of the bounding box around that , it gets marked as litter. I don't know how to detect if an object goes outside the contours area.

khaalidi gravatar imagekhaalidi ( 2019-10-08 03:41:35 -0500 )edit

I've updated the question and added a screenshot. Screenshot

khaalidi gravatar imagekhaalidi ( 2019-10-08 03:44:40 -0500 )edit

Do you meant when entered crossing first boundary line, it well detected bounding box and after second crossing boundary line. the bounding box will not be detected?

supra56 gravatar imagesupra56 ( 2019-10-08 08:23:47 -0500 )edit

@berak. He wanted undetected before first bounday line and after second boundary line. I have done this counting vehicles. It is just like counting peoples.

supra56 gravatar imagesupra56 ( 2019-10-08 08:30:35 -0500 )edit

I want to to detect if a set of pixel (Litter Object) is going outside the bounding box of the car.

khaalidi gravatar imagekhaalidi ( 2019-10-09 00:00:09 -0500 )edit

Get another video that can see cars crossing lane.

supra56 gravatar imagesupra56 ( 2019-10-09 06:06:22 -0500 )edit