在opencv中捕获并保存图像

时间:2020-06-11 13:21:45

标签: python opencv face-recognition

import cv2
import face_recognition


cap = cv2.VideoCapture(0)


face_locations = []

while True:

    ret, frame = cap.read()


    rgb_frame = frame[:, :, ::-1]


    face_locations = face_recognition.face_locations(rgb_frame)


    for top, right, bottom, left in face_locations:



        cv2.circle(frame,(int((left + right) / 2), top),15,(0, 0, 255), 2)
    cv2.circle(frame,(350 ,  150),5,(0, 255, 0), 1)



    cv2.imshow('Video', frame)


    if cv2.waitKey(25) == 13:
        break


cap.release()
cv2.destroyAllWindows()

结果的屏幕截图: enter image description here

目标:

仅当绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈时,我才需要保存图像。

如果绿色圆圈不在红色圆圈内,则不能保存图像。

1 个答案:

答案 0 :(得分:1)

以该问题的答案为依据:Check if circle inside circle

x1,y1->红色圆圈的位置

x2,y2->绿色圆圈的位置

c1->红色圆圈的半径

c2->绿色圆圈的半径

import math

导入后,您需要更改以下内容

#frame without circles
frameToSave = frame

#add circles to frame in live feed
cv2.circle(frame,(int((left + right) / 2), top),15,(0, 0, 255), 2)
cv2.circle(frame,(350 ,  150),5,(0, 255, 0), 1)

x1 = int((left + right) / 2)
y1 = top
c1 = 15

x2 = 350
y2 = 150
c2 = 5

d = math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

if c1 > ( d + c2 ):
    print("green circle inside red circle")
    cv2.imwrite(filename,frameToSave)
else:
    print("green circle not inside or not fully inside red circle")
    cv2.imwrite(filename,frameToSave)

进行一些编辑以实现注释中的目标(带圆圈的实时供稿,不带圆圈的已保存图像)

相关问题