How to check if cv2.circle lies inside cv2.rectangle in opencv python

Viewed 977

I have an opencv application where I am detecting face. I have drawn a circle on frame and I need to check if the circle lies inside the bounding box of the face or not. Below is the code to draw circle

cv2.circle(frame, (293, 141), 5, (0, 255, 0), -1)

which means x is 293 and y is 141 for circle. Below is the code for face detection:

face_bbox = face.detect_face(frame)
    if face_bbox is not None:
        (f_startX, f_startY, f_endX, f_endY) = face_bbox.astype("int")
        cv2.rectangle(frame, (f_startX, f_startY), (f_endX, f_endY), (0, 0, 0), 2)

f_startX, f_startY, f_endX, f_endY are the coordinates of the rectangle drawn for face. Now I need to check if circle lies between the bounding box or not. For this I have put below condition:

if f_startX < 293 < f_endX and f_startY > 141 > f_endY:
   print("Circle is inside face")
else:
  print("Circle is outside face")

But this seems not to be working. I am also a bit confuse as to how I should compare the x and y of face with x and y of circle. Can anyone please suggest me a good way. Please help. Thanks

2 Answers

I got it working and this is the condition I use:

if f_startX < 293 < f_endX and f_startY < 141 < f_endY:
    print("Circle is inside face")
else:
    print("Circle is outside face")

I am putting a very generic solution. Replace the variable accordingly.

#circle center
center_x = some_value
center_y = some_value
radius = some_value
(f_startX, f_startY, f_endX, f_endY) = face_bbox.astype("int")

if (center_x + radius < f_endX) and (center_y + radius < f_endY) and\ 
(center_x - radius > f_startX) and (center_y - radius >  f_startY):
     print("Inside")
else:
     print("Outside")
Related