简体   繁体   中英

How to save image when two bounding box touching

How can I save images when two boxes touch each other, I want when the bounding box faces touch the cigarette bounding box, the image will be saved as a whole.

This is the code :

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()

you can check point coordinate if in another box or not:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM