我正在尝试在高速公路上进行垃圾虫检测,因此我想先检测汽车,然后如果检测到一组像素越过边界框的边界,则应将其标记为垃圾。但是我不知道当对象(一组像素)越过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()
答案 0 :(得分:1)
请按照以下步骤操作:
overlap = cv.bitwise_and(bbox, A)
。cv.countNonZero(overlap)
。如果有,则检测到。