16

I have a set of points [(x0,y0), (x1,y1), .. ]

And a set of bounding rectangles produced using the cv2.boundingRect(someContour) function. Where each bounding box is an iterable object with four coordinates (a,b,c,d). My questions are:

  1. what is the meaning of these four numbers?.

  2. how to check if each given point is contained within the bounding rect?

I know that opencv for C++ has the 'contains' method but it doesnt exist for python.

Nimrodshn
  • 859
  • 3
  • 13
  • 29

2 Answers2

19
  1. a,b are the top-left coordinate of the rectangle and (c,d) be its width and height. OpenCV Contour Features
  2. to judge a point(x0,y0) is in the rectangle, just to check if a < x0 < a+c and b < y0 < b + d
Hooting
  • 1,681
  • 11
  • 20
  • Thanks a lot! very helpful! – Nimrodshn Oct 11 '15 at 15:32
  • @AlanKazbekov the question is tagged `python` and the python bindings don't support the Rect class – j b Nov 21 '19 at 09:34
  • @Hooting this actually incorrect since OpenCV rectangles are _inclusive_ with respect to left and top edges, so the code should be `a <= x0 < a+c and b <= y0 < b + d`. See https://docs.opencv.org/master/d2/d44/classcv_1_1Rect__.html#details – j b Nov 21 '19 at 09:36
7
def rectContains(rect,pt):
    logic = rect[0] < pt[0] < rect[0]+rect[2] and rect[1] < pt[1] < rect[1]+rect[3]
    return logic

rect = (a,b,c,d)

rectContains(rect,pt)
S. Goffin
  • 113
  • 1
  • 5