如何检查边界框是否在AOI上以及何时离开.OpenCV python



所以我要做的是使用OpenCV来检测盒子和make以及感兴趣的区域。当一个盒子越过AOI时,它会将其算作一次使用。这很好,但它不断增加每帧的使用量。我只想在检测到使用时这样做一次。

所以我在这里做的是:

  1. 从背景减法中找到轮廓。作品
  2. 获取检测到的每个blob的边界框。作品
  3. 在每个AOI框上循环,并使用并集上的交集来查看存在任何重叠
  4. 我将AOI列表中计数的布尔值设置为True。只有在一开始就是假的
  5. 如果它被计算在内,我会忽略它
  6. 如果并集上没有交集,那么我将计数设置为错误

这里的问题是,它要么继续计算每帧的使用量,要么只计算一次,再也不会计算了。我想计算它的每一个独特的使用,而不是每一帧。这就是我到目前为止所拥有的。

# # loop over the contours
for c in contours:
# Filter out the blobs that are too small to be considered cars.
contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
# compute the bounding box for the contour
(x, y, w, h) = cv2.boundingRect(c)
#Find each Area Of Interest usage
#detectUsage(area_of_interest, c, frame)
for aoi in area_of_interest:
#Check if there is a overlap between the detected contour and AOI
if gvp.machineUseCheck(c, aoi[1]):
print("{} in use".format(aoi[0]))
cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
if aoi[2] == False:
aoi[2] = True
print("{} set to True".format(aoi[0]))
elif aoi[2] == True:
print("{} Already true".format(aoi[0]))
elif gvp.machineUseCheck(c, aoi[1]) == False:
aoi[2] = False
print("{} not in use".format(aoi[0]))
#Get all the centers for the bounding boxes
center = (int(x + w / 2), int(y + h / 2))
cv2.circle(frame, center, 4, (0, 0, 255), -1)

我发现我所需要做的就是稍微重组for循环。

for aoi in area_of_interest:
cv2.putText(frame, "AOI Value:{}".format(aoi[2]), (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 255, 0), 2)
cv2.putText(frame, "{} Count:{}".format(aoi[0], str(aoi[3])), (10, aoi[4]), cv2.FONT_HERSHEY_SIMPLEX,
0.6, (0, 255, 0), 2)
i = 0
for c in contours:
# Filter out the blobs that are too small to be considered cars.
contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
# compute the bounding box for the contour
(x, y, w, h) = cv2.boundingRect(c)
center = (int(x + w / 2), int(y + h / 2))
cv2.circle(frame, center, 4, (0, 0, 255), -1)
if gvp.machineUseCheck(c, aoi[1]):
if not aoi[2]:
aoi[2] = True
aoi[3] += 1
cv2.putText(frame, "Machine IN USE:", (10, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
(0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
break
elif i == len(c):
aoi[2] = False
break
i += 1

最新更新