如何在python中检查点是否在椭圆中

Q2Learn

如何在python的椭圆形内找到一个点?我在函数中写出了方程式,然后绘制了一张黑色的图片,椭圆为白色,点为蓝色。然后,我正在计算该点是否在图片中显示的椭圆内,但函数返回的是false。我想念什么?

谢谢!

# Create a black image for ellipse ROI
img = np.zeros((240, 320, 3), np.uint8)

# ellipse
ellipse_center_x = 160
ellipse_center_y = 120
ellipse_axis_x = 100
ellipse_axis_y = 20
ellipse_angle = -40
start_angle = 0
end_angle = 360

# detection point example
xCenter = 135
yCenter = 135

def pointInEllipse(img, xp, yp, x, y, axis_x, axis_y, angle):
    
    # # WITH StackOverflow Answer Edits
    angle_rad = math.radians(angle)
    cosa = math.cos(angle_rad)
    sina = math.sin(angle_rad)

    # # Equation of point within angled ellipse
    a = (((cosa * (xp - x) + sina * (yp - y)) ** 2) / (axis_x**2))
    b = (((sina * (xp - x) - cosa * (yp - y)) ** 2) / (axis_y**2))

    result = a+b

    img = cv2.ellipse(img, (x, y), (axis_x, axis_y), angle, 0, 360, (255, 255, 255), 1)

    if result <= 1:
        img = cv2.circle(img, (xp, yp), 10, (255, 0, 0), -1)
        print(result)
        cv2.imwrite('/tmp/ellipse2.png', img)
        return True
    else:
        img = cv2.circle(img, (xp, yp), 10, (255, 0, 0), -1)
        print(result)
        cv2.imwrite('/tmp/ellipse2.png', img)
        return False

print(pointInEllipse(img, xCenter, yCenter, ellipse_center_x, ellipse_center_y, ellipse_axis_x, ellipse_axis_y,ellipse_angle))

在此处输入图片说明

mkrieger1

cosasina值是错误的:math.sinmath.cos期望弧度(0到2π为一个完整的圆)他们的观点,但你传递一个角度。

要将角度从度转换为弧度,请使用以下math.radians功能:

angle_rad = math.radians(angle)
cosa = math.cos(angle_rad)
sina = math.sin(angle_rad)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章