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()
you probably won't even see any litter on the street, due to the background subtraction (it does not move, or ??)
which bounding box ?
The litter should move as it will be thrown from the cars. And the bounding box around the car.
don't you think, you explicitly need to detect cars , then ? or is it only a matter of size ?
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.
I've updated the question and added a screenshot. Screenshot
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?
@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.
I want to to detect if a set of pixel (Litter Object) is going outside the bounding box of the car.
Get another video that can see cars crossing lane.