首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在更改原点后找到对象的相对球面坐标?

如何在更改原点后找到对象的相对球面坐标?
EN

Stack Overflow用户
提问于 2021-10-09 23:50:46
回答 1查看 137关注 0票数 0

我有一个算法问题。我目前正在编写一个脚本,它可以从虚幻引擎内部的不同角度生成对象的图像,并将这些图像与对象的坐标配对。它的工作方式是,我将物体放在原点,然后生成随机的球面坐标来放置相机。然后我旋转我的相机朝向物体,并做一个额外的旋转,这样物体就可以位于相机视野中的任何地方。现在我想把我的相机作为原点,找出物体相对于图形的球面坐标。

目前,我正在尝试导出以下代码中的坐标。我首先指出,无论哪个是原点,对象和相机之间的径向距离都是相同的。然后,我使用的事实是,我的相机和我的对象之间的角度完全由相机放置结束时的额外旋转决定。最后,我尝试找到一个旋转,它将根据相机的角坐标以与图像中相同的方式定位对象(这样做是因为我想编码关于对象上除中心之外的点的信息)。例如,我当前使用一个1米的立方体作为占位符对象,并且我想要跟踪角落的坐标。我选择使用旋转,因为我可以使用它们来生成旋转矩阵,并使用它来转换坐标)。下面是我用来做这件事的代码(这里使用的是AirSim库,但你需要知道的是airsim.Pose()接受一个欧几里得位置坐标和一个四元数旋转作为参数来定位我的相机)。

代码语言:javascript
复制
PRECISION_ANGLE = 4 # Fractions of a degree used in generating random pitch, roll, and yaw values
PRECISION_METER = 100 # Fractions of a meter used in generating random distance values
RADIUS_MAX = 20 # Maximum distance from the obstacle to be expected
#TODO: Replace minimum distace with a test for detecting if the camera is inside the obstacle
RADIUS_MIN = 3 # Minimum distance from the obstacle to be expected. Set this value large enough so that the camera will not spawn inside the object

# Camera details should match settings.json
IMAGE_HEIGHT = 144
IMAGE_WIDTH = 256
FOV = 90
# TODO: Vertical FOV rounds down for generating random integers. Some pictures will not be created
VERT_FOV = FOV * IMAGE_HEIGHT // IMAGE_WIDTH

def polarToCartesian(r, theta, phi):
    return [
         r * math.sin(theta) * math.cos(phi),
         r * math.sin(theta) * math.sin(phi),
         r * math.cos(theta)]

while 1:
    # generate a random position for our camera
    r = random.randint(RADIUS_MIN * PRECISION_METER, RADIUS_MAX * PRECISION_METER) / PRECISION_METER
    phi = random.randint(0, 360 * PRECISION_ANGLE) / PRECISION_ANGLE
    theta = random.randint(0, 180 * PRECISION_ANGLE) / PRECISION_ANGLE
    # Convert polar coordinates to cartesian for AirSim
    pos = polarToCartesian(r, math.radians(theta), math.radians(phi))

    # Generate a random offset for the camera angle
    pitch = random.randint(0, VERT_FOV * PRECISION_ANGLE) / PRECISION_ANGLE - VERT_FOV / 2
    # TODO: Rotating the drone causes the obstacle to be removed from the image because the camera is not square
    #roll = random.randint(0, 360 * PRECISION_ANGLE) / PRECISION_ANGLE
    roll = 0
    yaw = random.randint(0, FOV * PRECISION_ANGLE) / PRECISION_ANGLE - FOV/2

    # Calculate coordinates of the center of the obstacle relative to the drone's new position and orientation
    obs_r = r
    obs_phi = yaw
    obs_theta = 90 - pitch
    # Convert polar coordinates to cartesian for AirSim
    obs_pos = polarToCartesian(obs_r, math.radians(obs_theta), math.radians(obs_phi))

    # Record rotational transformation on obstacle for calculating coordinates of key locations relative to the center
    obs_phi_offset = -phi
    obs_theta_offset = 270 - theta

    # Move the camera to our calculated position
    camera_pose = airsim.Pose(airsim.Vector3r(pos[0], pos[1], pos[2]), airsim.to_quaternion(math.radians(90 - theta + pitch), math.radians(roll), math.radians(phi + 180 + yaw))) #radians

这个算法实现正确了吗?我还能用什么其他方法找到我的对象的坐标?我是否应该在虚幻引擎中做一些事情来获得我的坐标,而不是通过算法来做到这一点(尽管它需要快速)?

EN

回答 1

Stack Overflow用户

发布于 2021-10-13 18:53:29

Vector3(i,j,k)对原点的翻译就是对原始输出的翻译。

代码语言:javascript
复制
camera_pose = airsim.Pose(airsim.Vector3r(pos[0] + i, pos[1] + j, pos[2] + k), airsim.to_quaternion(math.radians(90 - theta + pitch), math.radians(roll), math.radians(phi + 180 + yaw))) #radians
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69511289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档