【问题标题】:How to save image when two bounding box touching两个边界框接触时如何保存图像
【发布时间】:2019-05-08 02:36:52
【问题描述】:

两个盒子相互接触时如何保存图像,我希望当边界框面接触香烟边界框时,图像将作为一个整体保存。

这是代码:

import cv2
import numpy as np
cigarette = cv2.CascadeClassifier('classifier/cigarette-cascade.xml')
faced = cv2.CascadeClassifier('classifier/face-detect.xml')

cam = cv2.VideoCapture(0)
while True:
    ret, img = cam.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faced.detectMultiScale(gray, 1.3, 5)
    cigarettes = cigarette.detectMultiScale(gray, 1.3, 5)
    for x,y,w,h in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    for (x, y, w, h) in cigarettes:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
    cv2.imshow('test', img)
    k = cv2.waitKey(1) & 0xff
    if k == 27:
        break
cv2.destroyAllWindows()

【问题讨论】:

  • 你可以使用 (RectA & RectB).area() 来判断两个区域是否相交
  • 你能给我举个例子吗?

标签: python opencv


【解决方案1】:

您可以检查点坐标是否在另一个框中:

cnt=0
for x,y,w,h in faces:
    for x_,y_,w_,h_ in cigarettes:
        if not len(set(range(x,x+w))&set(range(x_,x_+w_)))==0 and \
            not len(set(range(y,y+h))&set(range(y_,y_+h_)))==0:
            cnt +=1
            cv2.imwrite("save_%s.jpg"%cnt, img)

【讨论】:

  • 你能解释一下if x in range(x_,w_)是如何工作的吗?
猜你喜欢
  • 2012-12-02
  • 1970-01-01
  • 2019-05-11
  • 2021-12-16
  • 1970-01-01
  • 2018-04-03
  • 2020-12-31
  • 1970-01-01
  • 2021-07-20
相关资源
最近更新 更多