在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()
结果截图:
目标:
只有当绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈时,我才需要保存图像。
如果绿圈不在红圈里面,一定不能保存图片
以本题答案为依据:
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)
进行了一些编辑以实现评论中的目标(带圆圈的实时提要,不带圆圈的保存图像)
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()
结果截图:
目标:
只有当绿色圆圈在红色圆圈内并且保存的图像不应包含圆圈时,我才需要保存图像。
如果绿圈不在红圈里面,一定不能保存图片
以本题答案为依据:
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)
进行了一些编辑以实现评论中的目标(带圆圈的实时提要,不带圆圈的保存图像)