捕获图像并将其保存在opencv中

问题描述 投票:-3回答:1
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

目标:

[我仅在绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈的情况下才需要保存图像。

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

python opencv face-recognition
1个回答
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)

为了满足评论中的目标进行了一些编辑(带圆圈的实时供稿,不带圆圈的已保存图像)

© www.soinside.com 2019 - 2024. All rights reserved.