简体   繁体   English

在 opencv 中捕获并保存图像

[英]capture and save image in opencv

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

Screenshot of the result:结果截图: 在此处输入图像描述

Goal:目标:

I need the save the image only if the green circle is inside the red circle and the saved image should not contain the circles.只有当绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈时,我才需要保存图像。

If the green circle is not inside the red circle, it must not save the image.如果绿色圆圈不在红色圆圈内,则不能保存图像。

Taking the answer to this question as the basis: Check if circle inside circle以本题答案为依据: Check if circle inside circle

x1, y1 -> Position of red circle x1, y1 -> Position 红圈

x2, y2 -> Position of green circle x2, y2 -> 绿色圆圈的 Position

c1 -> radius of red circle c1 -> 红色圆圈的半径

c2 -> radius of green circle c2 -> 绿色圆圈的半径

import math

And after importing then you need to change the following导入后,您需要更改以下内容

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

Edited a bit to fulfill the goal in the comments (live feed with circles, saved image without circles)编辑了一下以实现评论中的目标(带圆圈的实时提要,没有圆圈的保存图像)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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