python - 在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()
目标:
只有当绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈时,我才需要保存图像。
如果绿色圆圈不在红色圆圈内,则不能保存图像。
解决方案
以这个问题的答案为依据: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)
进行了一些编辑以实现评论中的目标(带圆圈的实时提要,不带圆圈的已保存图像)
推荐阅读
- asp.net-mvc - 为什么项目在 Visual Studio 2015 中没有运行并给出 id 错误?
- visual-c++ - 调试 DLL 加载失败,找不到指定的程序错误
- c# - 格式化的单元格值不等于单元格值,即使是因为格式化
- java - 正则表达式适用于在线验证器,但在 java checkstyle 中失败
- hadoop - 如何将 mapreduce 结果加载到 Hive 中?
- java - 我的 sql 异常:主键更新查询的重复条目
- android - Drawable 在列表视图中未正确打印
- docker - 使用 Klee docker 镜像时如何在 home/klee 下准备好源代码文件
- c# - 如果 Content-Type 为“text/plain”,则 GET RestSharp 请求返回空值,而如果 Content-Type 为“application/json”,则返回数据
- r - 使用 ggplot2 在 R 中绘制空间线