blender的最大优点是可以用python进行交互。
经过一番google,我写了一组脚本,输出每一帧中无人机的屏幕坐标,作为训练用的ground truth.
get_obj_coord.py
# small helper script for CV+ML trainning ground truth data generation.
# by Qin Yongliang
import bpy
import bpy_extras
# get coordinate of object relative to screen.
def get_obj_coord(object_name):
target = bpy.data.objects[object_name]
# get object's location relative to world.
target_loc = target.matrix_world.to_translation()
cam = bpy.data.objects['Camera']
scene = bpy.context.scene
# relative to screen, where (0,0) represents left bottom of camera frame.
co2d = bpy_extras.object_utils.world_to_camera_view(scene,cam,target_loc)
return co2d
# get coordinate of object named 'Drone'.
def get_drone_coord():
c = get_obj_coord('Drone')
print('drone at screen coord:',c)
return c
然后运行下面的脚本:
import get_obj_coord as g
import bpy
scene = bpy.context.scene
fs = scene.frame_start
fe = scene.frame_end
def get_drone_coord_list(startframe,endframe):
list = []
for i in range(startframe,endframe):
scene.frame_set(i)
c = g.get_drone_coord()
list.append(c)
return list
file = open("c:/coords.csv", "w")
list = get_drone_coord_list(fs,fe)
file.write('x,y,z\n')
for c in list:
file.write('{},{},{}\n'.format(c.x,c.y,c.z))
file.close()
效果:
其中x和y是屏幕坐标,(0,0)是取景框左下角,(1,1)是取景框右上角。z是从相机到目标的距离。