File size: 7,465 Bytes
17cd746 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
import os
import cv2
import numpy as np
import open3d as o3d
# import pyrender
# from pyrender import mesh, DirectionalLight, Material, PerspectiveCamera
os.environ['__GL_THREADED_OPTIMIZATIONS'] = '1'
cord_list = []
with open('./cord.txt', 'r') as f:
lines = f.readlines()
for line in lines:
m = line.split()
x = int(m[0])
y = int(m[1])
x = 1000 - x
y = 1000 - y
cord_list.append([x, y])
# 假设TXT文件的路径
output_folder = '/media/gyalex/Data/face_det_dataset/rgbd_data/rgbd'
if not os.path.exists(output_folder):
os.mkdir(output_folder)
for idx in range(32, 33):
txt_file_path = '/media/gyalex/Data/face_det_dataset/rgbd_data/PointImage'+ str(idx) + '.txt'
_, name = os.path.split(txt_file_path)
print(txt_file_path)
with open(txt_file_path, 'r') as file:
points = []
rgb_list = []
ori_rgb_list = []
normal_list = []
# 逐行读取数据
for line in file:
# 去除行尾的换行符并分割字符串
x, y, z, r, g, b, nx, ny, nz, w = line.split()
# 将字符串转换为浮点数
x = float(x)
y = float(y)
z = float(z)
r = float(r)
g = float(g)
b = float(b)
nx = float(nx)
ny = float(ny)
nz = float(nz)
# 将点添加到列表中
points.append((x, y, z))
rgb_list.append((r/255.0, g/255.0 , b/255.0))
normal_list.append((nx, ny, nz))
ori_r = int(r)
ori_g = int(g)
ori_b = int(b)
ori_rgb_list.append((ori_r, ori_g , ori_b))
np_points = np.asarray(points)
np_points_a = np_points
np_colors = np.asarray(rgb_list)
np_normals = np.asarray(normal_list)
np_colors_ori = np.asarray(ori_rgb_list)
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np_points)
pcd.colors = o3d.utility.Vector3dVector(np_colors)
pcd.normals = o3d.utility.Vector3dVector(np_normals)
map_dict = {}
image = np.ones((1000, 1000, 3),dtype=np.uint8)*255
for i in range(np.array(pcd.points).shape[0]):
x = np.array(pcd.points)[i,0]+400
y = np.array(pcd.points)[i,1]+400
image[int(x),int(y),:] = (np.array(pcd.colors)[i,:]*255).astype(np.uint8)
image[int(x+1),int(y),:] = (np.array(pcd.colors)[i,:]*255).astype(np.uint8)
image[int(x),int(y+1),:] = (np.array(pcd.colors)[i,:]*255).astype(np.uint8)
image[int(x-1),int(y),:] = (np.array(pcd.colors)[i,:]*255).astype(np.uint8)
image[int(x),int(y-1),:] = (np.array(pcd.colors)[i,:]*255).astype(np.uint8)
map_dict[str(int(x)) + '_' + str(int(y))] = i
map_dict[str(int(x+1)) + '_' + str(int(y))] = i
map_dict[str(int(x)) + '_' + str(int(y+1))] = i
map_dict[str(int(x-1)) + '_' + str(int(y))] = i
map_dict[str(int(x)) + '_' + str(int(y-1))] = i
# if [int(y), int(x)] in cord_list:
# image[int(x),int(y),:] = np.array([0, 255, 0])
# if [int(y), int(x+1)] in cord_list:
# image[int(x+1),int(y),:] = np.array([0, 255, 0])
# if [int(y+1), int(x)] in cord_list:
# image[int(x),int(y+1),:] = np.array([0, 255, 0])
# if [int(y), int(x-1)] in cord_list:
# image[int(x-1),int(y),:] = np.array([0, 255, 0])
# if [int(y-1), int(x)] in cord_list:
# image[int(x),int(y-1),:] = np.array([0, 255, 0])
# if [int(y-1), int(x-1)] in cord_list:
# image[int(x-1),int(y-1),:] = np.array([0, 255, 0])
# if [int(y+1), int(x+1)] in cord_list:
# image[int(x+1),int(y+1),:] = np.array([0, 255, 0])
h_list = []
for m in cord_list:
a, b = m[0], m[1]
c = image[int(b),int(a),:][0]
flag = False
if image[int(b),int(a),:][1] != 255:
h_list.append(str(int(b))+'_'+str(int(a)))
flag = True
else:
if image[int(b)-2,int(a)-2,:][1] != 255:
h_list.append(str(int(b)-2)+'_'+str(int(a)-2))
flag = True
elif image[int(b)+2,int(a)+2,:][1] != 255:
h_list.append(str(int(b)+2)+'_'+str(int(a)+2))
flag = True
elif image[int(b),int(a)-3,:][1] != 255:
h_list.append(str(int(b))+'_'+str(int(a)-3))
flag = True
# if flag == False:
# cc = image[int(b),int(a),:][1]
# cv2.circle(image, (465,505), 2, (0, 255, 0), -1)
# cv2.imshow('win', image)
# cv2.waitKey(0)
with open('pid.txt', 'w') as f:
for h in h_list:
pid = map_dict[h]
s = str(pid) + '\n'
f.write(s)
np_colors[pid,:] = np.array([0, 255, 0])
f.close()
pcd0 = o3d.geometry.PointCloud()
pcd0.points = o3d.utility.Vector3dVector(np_points)
pcd0.colors = o3d.utility.Vector3dVector(np_colors)
pcd0.normals = o3d.utility.Vector3dVector(np_normals)
o3d.io.write_point_cloud('aa.ply', pcd0)
mm = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image3 = cv2.flip(mm, -1)
# cv2.imwrite('./rgb.png', image3)
with open('./cord.txt', 'r') as f:
lines = f.readlines()
for line in lines:
m = line.split()
x = int(m[0])
y = int(m[1])
x = 1000 - x
y = 1000 - y
cv2.circle(image, (x,y), 2, (0, 255, 0), -1)
idx = map_dict[str(x)+'_'+str(y)]
a = 0
# cv2.imshow("win", image)
# cv2.waitKey(0)
# import matplotlib.pyplot as plt
# plt.imshow(image)
# plt.show()
# save_pcd_path = os.path.join(output_folder, name[:-3]+'ply')
# # o3d.io.write_point_cloud(save_pcd_path, pcd)
# # render
# import trimesh
# # fuze_trimesh = trimesh.load('/home/gyalex/Desktop/PointImage32.obj')
# # mesh = pyrender.Mesh.from_trimesh(fuze_trimesh)
# mesh = pyrender.Mesh.from_points(np_points, np_colors_ori, np_normals)
# import math
# camera = PerspectiveCamera(yfov=math.pi / 3, aspectRatio=1.0)
# camera_pose = np.array([[-1.0, 0.0, 0.0, 0], \
# [0.0, 1.0, 0.0, 0], \
# [0.0, 0.0, -1.0, 0], \
# [0.0, 0.0, 0.0, 1.0]])
# # 创建场景
# scene = pyrender.Scene()
# scene.add(mesh)
# scene.add(camera, pose=camera_pose)
# # light = pyrender.SpotLight(color=np.ones(3), intensity=3.0, innerConeAngle=np.pi/16.0, outerConeAngle=np.pi/6.0)
# # scene.add(light, pose=camera_pose)
# # 渲染场景
# renderer = pyrender.OffscreenRenderer(viewport_width=1280, viewport_height=1024)
# color, depth = renderer.render(scene)
# # # 设置场景和光源
# # scene = pyrender.Scene()
# # scene.add(point_cloud_mesh, 'point_cloud')
# # camera = PerspectiveCamera(yfov=45.0, aspectRatio=1.0)
# # scene.add(camera)
# # # 渲染场景
# # renderer = pyrender.OffscreenRenderer(viewport_width=1280, viewport_height=1024)
# # color, depth = renderer.render(scene)
# # 保存渲染结果为图片
# import cv2
# cv2.imshow('win', color)
# rgb_img = cv2.imread('/media/gyalex/Data/face_det_dataset/rgbd_data/color_32.bmp')
# cv2.imshow('win0', rgb_img)
# cv2.waitKey(0) |