[英]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()
目標:
只有當綠色圓圈在紅色圓圈內並且保存的圖像不應包含圓圈時,我才需要保存圖像。
如果綠色圓圈不在紅色圓圈內,則不能保存圖像。
以本題答案為依據: Check if circle inside circle
x1, y1 -> Position 紅圈
x2, y2 -> 綠色圓圈的 Position
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)
編輯了一下以實現評論中的目標(帶圓圈的實時提要,沒有圓圈的保存圖像)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.