title: How to calculate centroid of a bouding box in python opencv
body: <p>I am doing inference and detecting hands and I need to calculate the centroid of the bounding box of the detected hand.</p>
  <p>Below is its coordinates:</p>
  <pre><code>left = 215 
  bottom = 447 
  right = 325 
  top = 367
  </code></pre>
  <p>and this is how I am drawing the rectangle</p>
  <pre><code>cv2.rectangle(frame, (int(obj.Left), int(obj.Bottom)), (int(obj.Right), int(obj.Top)), (0, 0, 255), 2)
  </code></pre>
  <p>Now in order to calculate the centroid of the bounding box, I am using below lines of code:</p>
  <pre><code>w = int(obj.Right - obj.Left)
  h = int(obj.Bottom - obj.Top)
  cx = int(obj.Top + h/2)
  cy = int(obj.Left + w/2)
  print(cx, cy)
  </code></pre>
  <p>So first I am calculating the width and height of the bounding box. Then dividing the height and width by 2 and adding them to top  and left respectively to calculate the centroid. But this doesn't seems to be working as you can see the white dot (represented in red circle) is far away from detected hand</p>
  <p><a href="https://i.stack.imgur.com/XX8Od.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XX8Od.png" alt="enter image description here" /></a></p>
  <p>Can anyone please give some suggestions on how to improve it. Thanks</p>