我正在执行物体运动追踪,试图识别物体的前后方。该物体不对称,这意味着轮廓的质心比后面更靠近前面。根据这些信息,我按以下方式处理:
Draw contours of object
Find centroid
centroidx, centroidy = int(moments['m10']/moments['m00']), int(moments['m10']/moments['m00'])
Draw bounding ellipse
cv2.fitEllipse(contour)
Calculate major axis length as follows (and as shown in the figure)
MAx, MAy = int(0.5 * ellipseMajorAxisx*math.sin(ellipseAngle)), int(0.5 * ellipseMajorAxisy*math.cos(ellipseAngle))
Calculate beginning and ending x, y coordinates of the major axis
MAxtop, MAytop = int(ellipseCentrex + MAx), int(ellipseCentrey + MAy) MAxbot, MAybot = int(ellipseCentrex - MAx), int(ellipseCentrey - MAy)
Identify which of the points is closer to the centroid of the contour
distancetop = math.sqrt((centroidx - MAxtop)**2 + (centroidy - MAytop)**2) distancebot = math.sqrt((centroidx - MAxbot)**2 + (centroidy - MAybot)**2) min(distancetop, distancebot)
编辑:当我把椭圆错看成一个垂直的点时,它其实是我的椭圆的镜像。这种情况也会出现在x值上,而不仅仅是y值。
在按照ssm下面的建议后,我大部分时间都得到了期望的点。有时候点还是会出错,但很快就会“回弹”到正确位置。例如,在这种情况下,这是一些帧:
顺便说一下,上述图片是通过使用以下代码“修正”角度后得到的:
if angle > 90:
angle = 180 - angle
如果我不进行校正,有时候会出现错误的点,如下所示,对于相同的帧。
![angle not corrected](https://istack.dev59.com/3q4IE.webp)
(椭圆内部的白点是轮廓的质心,而椭圆上或外部的点是我得到的点)
MAytop, MAybot = MAybot, MAytop
来交换两个变量的值。 - David Zwickercv2
也是如此(尽管我没有检查过cv2文档)。而你正在以顺时针方式从y轴测量角度。 - ssm