Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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 = cap.read()
    fgmask = fgbg.apply(frame)

    if not ret:
        break 
    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:
            continue
        x,y,w,h = cv2.boundingRect(cnt)
        offset = 3    
        cv2.rectangle(frame_r,(x-offset,y-offset),(x+w+offset,y+h+offset),(0,255,0),2)


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


    if cv2.waitKey(75) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

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

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 = cap.read()
    fgmask = fgbg.apply(frame)

    if not ret:
        break 
    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:
            continue
        x,y,w,h = cv2.boundingRect(cnt)
        offset = 3    
        cv2.rectangle(frame_r,(x-offset,y-offset),(x+w+offset,y+h+offset),(0,255,0),2)


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


    if cv2.waitKey(75) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

image description