I want to compute the centroid of rectangle, The coordinates of rectangle are as follows:
co_ord = (601, 1006,604, 1009) ## (xmin,ymin,xmax,ymax)
Can someone point to an easy way. Thanks
I want to compute the centroid of rectangle, The coordinates of rectangle are as follows:
co_ord = (601, 1006,604, 1009) ## (xmin,ymin,xmax,ymax)
Can someone point to an easy way. Thanks
The centroid of a rectangle with opposite corners (x1, y1)
and (x2, y2)
lies at the center of that rectangle ((x1+x2)/2, (y1+y2)/2)
First, I am assuming that by saying centroid, you mean center. Next, I assume that the coord tuple is in the format: (x, y, width, height). In that case, it would be done this way:
coord = (601, 1006, 604, 1009)
centerCoord = (coord[0]+(coord[2]/2), coord[1]+(coord[3]/2))
where centerCoord would be the coordinates of the center in the format (x, y).
If you have the right coord of the rectangle, you can easily compute the centroid point coordinates with a formula:
If you have the 2 opposite points of the rectangle, you can use this:
Computed centroid points:
Just a suggestion: You can write a checking part in your program. You should check the parameters which your program get. It's not required for the basic running but it would be better if the program checks the rectangle is a real rectangle.
I think this should brighten things up for you. I will use the openCV library to make the example more understandable.
To find a center you need to coordinates: x and y.
# Draw a rectangle on the image. pt1 = left upper corner, pt2 = right bottom corner
cv2.rectangle(img=your_image, pt1=(px1_width, px2_height), pt2=(px3_width, px4_height), color=(0, 255, 0), thickness=3)
# Calculate the center point of rectangle
x, y = (px1_width + px3_width) // 2, (px2_height + px4_height) // 2
# Draw a circle in the center of rectangle
cv2.circle(img=your_image, center=(x, y), radius=3, color=(0, 255, 0), thickness=3)