Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to project 3d mesh points to 2d points. #222

Open
tengshaofeng opened this issue Jun 6, 2024 · 0 comments
Open

how to project 3d mesh points to 2d points. #222

tengshaofeng opened this issue Jun 6, 2024 · 0 comments

Comments

@tengshaofeng
Copy link

tengshaofeng commented Jun 6, 2024

@geopavlakos @dimtzionas @vchoutas @MichaelJBlack @nghorbani Dears,When I finish the fitting, I print the camera parameter like:
image

So I write a code like this
"
def get_camera_intrinsic(focal_length, camera_center):
K = np.array([
[focal_length, 0, camera_center[0]],
[0, focal_length, camera_center[1]],
[0, 0, 1]
], dtype=np.float32)
return K

def project_points(mesh_points, camera_transl, camera_intrinsic):
R = np.eye(3)
t = camera_transl.reshape(3, 1)
T = np.hstack((R, t))
ones = np.ones((mesh_points.shape[0], 1))
homogenous_points = np.hstack((mesh_points, ones))
camera_points = homogenous_points.dot(T.T)
projected_points = camera_points.dot(camera_intrinsic.T)
projected_points_2d = projected_points[:, :2] / projected_points[:, 2, np.newaxis]
return projected_points_2d[:, :2] # 只取x, y坐标

相机参数

camera_center = np.array([800., 600.], dtype=np.float32)
camera_transl = np.array([-0.04409692, 0.26306236, 8.006923], dtype=np.float32)
focal_length = 5000.0
K = get_camera_intrinsic(focal_length, camera_center)
out_mesh = trimesh.load_mesh("../OUTPUT_FOLDER/meshes/01_img/000.obj")
mesh_points = np.array(out_mesh.vertices)

投影三维点到二维

projected_points_2d = project_points(mesh_points, camera_transl, K)
projected_points_2d[:, 1] = H - 1 - projected_points_2d[:, 1]

打印投影结果

print(projected_points_2d)
color = (0, 0, 255) # BGR格式

对每个二维点进行绘制

img = cv2.imread("../DATA_FOLDER/images/01_img.jpg")
for point in projected_points_2d.astype(int):
cv2.circle(img, tuple(point), radius=1, color=color, thickness=-1) # thickness=-1 表示填充圆
cv2.imwrite('projected_points_cv2.png', img)
"
But I get result like this:
projected_points_cv2

What is wrong? Can u help me to fix my code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant