我想计算矩形的质心,以下是矩形的坐标:
co_ord = (601, 1006,604, 1009) ## (xmin,ymin,xmax,ymax)
有人能指出一种简单的方法吗?谢谢。
co_ord = (601, 1006,604, 1009) ## (xmin,ymin,xmax,ymax)
有人能指出一种简单的方法吗?谢谢。
对于坐标为(x1, y1)
和(x2, y2)
的矩形,其重心位于该矩形的中心点((x1+x2)/2, (y1+y2)/2)
首先,我认为你所说的中心点是指中心。其次,我假设坐标元组的格式为:(x,y,宽度,高度)。那么,应该这样做:
coord = (601, 1006, 604, 1009)
centerCoord = (coord[0]+(coord[2]/2), coord[1]+(coord[3]/2))
其中centerCoord将以(x, y)的格式表示中心点的坐标。
我想这会让你的事情变得更加明朗。我将使用openCV库使示例更容易理解。
要找到中心,您需要坐标:x和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)