当两个框彼此接触时如何保存图像,我希望当边界框的面接触香烟边界框时,图像将被保存为一个整体。
这是代码:
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()
答案 0 :(得分:0)
您可以检查点坐标是否在另一个框中:
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)