def transformObject(v, vn, chScale, chObjAz, chPosition): #print ('utils.py:16 transformObject') #print ('v', type(v)) #import ipdb #ipdb.set_trace() if chScale.size == 1: scaleMat = geometry.Scale(x=chScale[0], y=chScale[0], z=chScale[0])[0:3, 0:3] elif chScale.size == 2: scaleMat = geometry.Scale(x=chScale[0], y=chScale[0], z=chScale[1])[0:3, 0:3] else: scaleMat = geometry.Scale(x=chScale[0], y=chScale[1], z=chScale[2])[0:3, 0:3] chRotAzMat = geometry.RotateZ(a=chObjAz)[0:3, 0:3] chRotAzMatX = geometry.RotateX(a=0)[0:3, 0:3] # transformation = scaleMat transformation = ch.dot(ch.dot(chRotAzMat, chRotAzMatX), scaleMat) invTranspModel = ch.transpose(ch.inv(transformation)) vtransf = [] vntransf = [] for mesh_i, mesh in enumerate(v): vtransf = vtransf + [ch.dot(v[mesh_i], transformation) + chPosition] vndot = ch.dot(vn[mesh_i], invTranspModel) vndot = vndot / ch.sqrt(ch.sum(vndot**2, 1))[:, None] vntransf = vntransf + [vndot] return vtransf, vntransf
def setupCamera_centauro(v, cameraParams, tf_world_2_camera): ''' Simplified setup for the centauro toy example or most of cases where we don't need to regress for camera parameters in : tf_world_2_camera (Preferably use Matrix44.look_at() to create this) ''' modelRotation = tf_world_2_camera[0:3, 0:3] chRod = opendr.geometry.Rodrigues(rt=modelRotation).reshape(3) chTranslation = tf_world_2_camera[0:3, 3] camera = ProjectPoints(v=v, rt=rotation, t=translation, f=1000 * cameraParams['chCamFocalLength'] * cameraParams['a'], c=cameraParams['c'], k=ch.zeros(5)) #print ('camera shape', camera.shape) #print ('camera ', camera) flipXRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0., 0.0], [0.0, 0., -1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) camera.openglMat = flipXRotation #Needed to match OpenGL flipped axis. # chMVMat construct might be wrong # From line 308 chInvCam = ch.inv(chMVMat) # It seems, chMVMat is tf_camera_2_world # In any case, the create_renderer function ignores this returned variable chMVMat = ch.inv(tf_world_2_camera) return camera, modelRotation, chMVMat
def setupCamera(v, chAz, chEl, chDist, objCenter, width, height): chCamModelWorld = computeHemisphereTransformation(chAz, chEl, chDist, objCenter) chMVMat = ch.dot(chCamModelWorld, np.array(mathutils.Matrix.Rotation(radians(270), 4, 'X'))) chInvCam = ch.inv(chMVMat) modelRotation = chInvCam[0:3, 0:3] chRod = opendr.geometry.Rodrigues(rt=modelRotation).reshape(3) chTranslation = chInvCam[0:3, 3] translation, rotation = (chTranslation, chRod) camera = ProjectPoints(v=v, rt=rotation, t=translation, f=1.12 * ch.array([width, width]), c=ch.array([width, height]) / 2.0, k=ch.zeros(5)) camera.openglMat = np.array(mathutils.Matrix.Rotation( radians(180), 4, 'X')) return camera, modelRotation, chMVMat
def transformObject(v, vn, chScale, chObjAz, chObjDisplacement, chObjRotation, targetPosition): scaleMat = geometry.Scale(x=chScale[0], y=chScale[1], z=chScale[2])[0:3, 0:3] chRotAzMat = geometry.RotateZ(a=-chObjAz)[0:3, 0:3] transformation = ch.dot(chRotAzMat, scaleMat) invTranspModel = ch.transpose(ch.inv(transformation)) objDisplacementMat = computeHemisphereTransformation( chObjRotation, 0, chObjDisplacement, np.array([0, 0, 0])) newPos = objDisplacementMat[0:3, 3] vtransf = [] vntransf = [] for mesh_i, mesh in enumerate(v): vtransf = vtransf + [ ch.dot(v[mesh_i], transformation) + newPos + targetPosition ] # ipdb.set_trace() # vtransf = vtransf + [v[mesh_i] + chPosition] vndot = ch.dot(vn[mesh_i], invTranspModel) vndot = vndot / ch.sqrt(ch.sum(vndot**2, 1))[:, None] vntransf = vntransf + [vndot] return vtransf, vntransf, newPos
def transformObjectFull(v, vn, chScale, chObjAz, chObjAx, chObjAz2, chPosition): if chScale.size == 1: scaleMat = geometry.Scale(x=chScale[0], y=chScale[0], z=chScale[0])[0:3, 0:3] elif chScale.size == 2: scaleMat = geometry.Scale(x=chScale[0], y=chScale[0], z=chScale[1])[0:3, 0:3] else: scaleMat = geometry.Scale(x=chScale[0], y=chScale[1], z=chScale[2])[0:3, 0:3] chRotAzMat = geometry.RotateZ(a=chObjAz)[0:3, 0:3] chRotAxMat = geometry.RotateX(a=-chObjAx)[0:3, 0:3] chRotAzMat2 = geometry.RotateZ(a=chObjAz2)[0:3, 0:3] transformation = ch.dot( ch.dot(ch.dot(chRotAzMat, chRotAxMat), chRotAzMat2), scaleMat) invTranspModel = ch.transpose(ch.inv(transformation)) vtransf = [] vntransf = [] for mesh_i, mesh in enumerate(v): vtransf = vtransf + [ch.dot(v[mesh_i], transformation) + chPosition] vndot = ch.dot(vn[mesh_i], invTranspModel) vndot = vndot / ch.sqrt(ch.sum(vndot**2, 1))[:, None] vntransf = vntransf + [vndot] return vtransf, vntransf
def chShapeParamsToNormals(N, landmarks, linT): T = ch.dot(linT, landmarks) invT = [] nLandmarks = landmarks.r.shape[0] for i in range(nLandmarks): R = T[4 * i:4 * i + 3, :3].T invR = ch.inv(R.T) invT = invT + [invR] invT = ch.vstack(invT) newNormals = ch.dot(N, invT) import opendr.geometry n = opendr.geometry.NormalizedNx3(newNormals) return newNormals
def setupCamera(v, cameraParams): chDistMat = geometry.Translate(x=0, y=cameraParams['Zshift'], z=cameraParams['chCamHeight']) chRotElMat = geometry.RotateX(a=-cameraParams['chCamEl']) chCamModelWorld = ch.dot(chDistMat, chRotElMat) flipZYRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 0, 1.0, 0.0], [0.0, -1.0, 0, 0.0], [0.0, 0.0, 0.0, 1.0]]) chMVMat = ch.dot(chCamModelWorld, flipZYRotation) chInvCam = ch.inv(chMVMat) modelRotation = chInvCam[0:3, 0:3] chRod = opendr.geometry.Rodrigues(rt=modelRotation).reshape(3) chTranslation = chInvCam[0:3, 3] translation, rotation = (chTranslation, chRod) camera = ProjectPoints(v=v, rt=rotation, t=translation, f=1000 * cameraParams['chCamFocalLength'] * cameraParams['a'], c=cameraParams['c'], k=ch.zeros(5)) flipXRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0., 0.0], [0.0, 0., -1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) camera.openglMat = flipXRotation #Needed to match OpenGL flipped axis. return camera, modelRotation, chMVMat
def setupCamera(v, cameraParams, is_ycb=False): chDistMat = geometry.Translate(x=0, y=cameraParams['Zshift'], z=cameraParams['chCamHeight']) #print ('chDistMat', chDistMat) chRotElMat = geometry.RotateX(a=-cameraParams['chCamEl']) chCamModelWorld = ch.dot(chDistMat, chRotElMat) flipZYRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, 0, 1.0, 0.0], [0.0, -1.0, 0, 0.0], [0.0, 0.0, 0.0, 1.0]]) chMVMat = ch.dot(chCamModelWorld, flipZYRotation) if is_ycb: chMVMat = ch.Ch(np.eye(4)) # np.save('extrinsics.npy', chMVMat, allow_pickle=False) chInvCam = ch.inv(chMVMat) modelRotation = chInvCam[0:3, 0:3] chRod = opendr.geometry.Rodrigues(rt=modelRotation).reshape(3) chTranslation = chInvCam[0:3, 3] translation, rotation = (chTranslation, chRod) # camera parameters format suitable for YCB video dataset if 'a' in cameraParams.keys(): # NOTE: Focal lenght is represented in mm and a is no.of pixels per mm _f = 1000 * cameraParams['chCamFocalLength'] * cameraParams['a'] else: # NOTE: Focal length is already in terms of pixels if np.any(cameraParams['chCamFocalLength'] < 1): import sys sys.exit( "Camera Focal length 'chCamFocalLength' is represented in number of pixels." ) _f = cameraParams['chCamFocalLength'] if 'k' in cameraParams.keys(): _k = cameraParams['k'] else: _k = ch.zeros(5) print('Using k', _k) camera = ProjectPoints(v=v, rt=rotation, t=translation, f=_f, c=cameraParams['c'], k=_k) # _f = 1000 * cameraParams['chCamFocalLength'] * cameraParams['a'] # _c = cameraParams['c'] #np.save('intrinsics.npy', camera.camera_mtx, allow_pickle=False) ##print ('camera shape', camera.shape) ##print ('camera ', camera) print('camera.camera_mtx', camera.camera_mtx) np.save('projection_matrix', camera.camera_mtx) #import ipdb #ipdb.set_trace() flipXRotation = np.array([[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0., 0.0], [0.0, 0., -1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]) camera.openglMat = flipXRotation #Needed to match OpenGL flipped axis. return camera, modelRotation, chMVMat