修复merge_tif bug,TODO: 加入conv_obj2代码(切分obj)
This commit is contained in:
parent
4828544ad6
commit
b69a610dd2
@ -250,11 +250,11 @@ class ImagePreprocessor:
|
||||
self.logger.info(
|
||||
f"网格 ({grid_id[0]},{grid_id[1]}) 包含 {len(points)} 张图像")
|
||||
|
||||
def merge_tif(self, grid_points: Dict[tuple, pd.DataFrame], produce_dem: bool):
|
||||
def merge_tif(self, grid_points: Dict[tuple, pd.DataFrame], mode: str, produce_dem: bool):
|
||||
"""合并所有网格的影像产品"""
|
||||
self.logger.info("开始合并所有影像产品")
|
||||
merger = MergeTif(self.config.output_dir)
|
||||
merger.merge_all_tifs(grid_points, produce_dem)
|
||||
merger.merge_all_tifs(grid_points, mode)
|
||||
|
||||
def merge_ply(self, grid_points: Dict[tuple, pd.DataFrame]):
|
||||
"""合并所有网格的PLY点云"""
|
||||
@ -282,18 +282,19 @@ class ImagePreprocessor:
|
||||
f"将只合并成功处理的 {len(successful_grid_points)} 个网格"
|
||||
)
|
||||
|
||||
if self.config.mode == "快拼模式":
|
||||
self.merge_tif(successful_grid_points, self.config.produce_dem)
|
||||
elif self.config.mode == "三维模式":
|
||||
self.merge_tif(successful_grid_points, self.config.produce_dem)
|
||||
# self.merge_ply(successful_grid_points)
|
||||
# self.merge_obj(successful_grid_points, translations)
|
||||
self.convert_obj(successful_grid_points)
|
||||
else:
|
||||
self.merge_tif(successful_grid_points, self.config.produce_dem)
|
||||
# if self.config.mode == "快拼模式":
|
||||
self.merge_tif(successful_grid_points, self.config.mode,
|
||||
self.config.produce_dem)
|
||||
if self.config.mode == "三维模式":
|
||||
# self.merge_tif(successful_grid_points, self.config.produce_dem)
|
||||
# self.merge_ply(successful_grid_points)
|
||||
# self.merge_obj(successful_grid_points, translations)
|
||||
self.convert_obj(successful_grid_points)
|
||||
# else:
|
||||
# self.merge_tif(successful_grid_points, self.config.produce_dem)
|
||||
# # self.merge_ply(successful_grid_points)
|
||||
# # self.merge_obj(successful_grid_points, translations)
|
||||
# self.convert_obj(successful_grid_points)
|
||||
|
||||
def process(self):
|
||||
"""执行完整的预处理流程"""
|
||||
|
@ -17,7 +17,7 @@ from utils.visualizer import FilterVisualizer
|
||||
from post_pro.merge_tif import MergeTif
|
||||
from post_pro.merge_obj import MergeObj
|
||||
from post_pro.merge_laz import MergePly
|
||||
from post_pro.conv_obj import ConvertOBJ
|
||||
from post_pro.conv_obj2 import ConvertOBJ
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -44,6 +44,7 @@ class PreprocessConfig:
|
||||
grid_size: float = 500
|
||||
# 几个pipline过程是否开启
|
||||
mode: str = "快拼模式"
|
||||
accuracy: str = "medium"
|
||||
produce_dem: bool = False
|
||||
|
||||
|
||||
@ -223,6 +224,8 @@ class ImagePreprocessor:
|
||||
self.gps_points
|
||||
)
|
||||
grid_divider.visualize_grids(self.gps_points, grids)
|
||||
if len(grids) >= 20:
|
||||
self.logger.warning("网格数量已超过20, 需要人工调整分区")
|
||||
|
||||
return grid_points, translations
|
||||
|
||||
@ -302,6 +305,9 @@ class ImagePreprocessor:
|
||||
# self.copy_images(grid_points)
|
||||
# self.logger.info("预处理任务完成")
|
||||
|
||||
# successful_grid_points = self.odm_monitor.process_all_grids(
|
||||
# grid_points, self.config.produce_dem, self.config.accuracy)
|
||||
|
||||
# successful_grid_points = self.odm_monitor.process_all_grids(
|
||||
# grid_points, self.config.produce_dem)
|
||||
successful_grid_points = grid_points
|
||||
|
262
post_pro/conv_obj2.py
Normal file
262
post_pro/conv_obj2.py
Normal file
@ -0,0 +1,262 @@
|
||||
import os
|
||||
import subprocess
|
||||
import json
|
||||
import shutil
|
||||
import logging
|
||||
from pyproj import Transformer
|
||||
import cv2
|
||||
|
||||
|
||||
class ConvertOBJ:
|
||||
def __init__(self, output_dir: str):
|
||||
self.output_dir = output_dir
|
||||
# 用于存储所有grid的UTM范围
|
||||
self.ref_east = float('inf')
|
||||
self.ref_north = float('inf')
|
||||
# 初始化UTM到WGS84的转换器
|
||||
self.transformer = Transformer.from_crs(
|
||||
"EPSG:32649", "EPSG:4326", always_xy=True)
|
||||
self.logger = logging.getLogger('UAV_Preprocess.ConvertOBJ')
|
||||
|
||||
def convert_grid_obj(self, grid_points):
|
||||
"""转换每个网格的OBJ文件为OSGB格式"""
|
||||
os.makedirs(os.path.join(self.output_dir,
|
||||
"osgb", "Data"), exist_ok=True)
|
||||
|
||||
# 以第一个grid的UTM坐标作为参照系
|
||||
first_grid_id = list(grid_points.keys())[0]
|
||||
first_grid_dir = os.path.join(
|
||||
self.output_dir,
|
||||
f"grid_{first_grid_id[0]}_{first_grid_id[1]}",
|
||||
"project"
|
||||
)
|
||||
log_file = os.path.join(
|
||||
first_grid_dir, "odm_orthophoto", "odm_orthophoto_log.txt")
|
||||
self.ref_east, self.ref_north = self.read_utm_offset(log_file)
|
||||
|
||||
for grid_id in grid_points.keys():
|
||||
try:
|
||||
self._convert_single_grid(grid_id, grid_points)
|
||||
except Exception as e:
|
||||
self.logger.error(f"网格 {grid_id} 转换失败: {str(e)}")
|
||||
|
||||
self._create_merged_metadata()
|
||||
|
||||
def _convert_single_grid(self, grid_id, grid_points):
|
||||
"""转换单个网格的OBJ文件"""
|
||||
# 构建相关路径
|
||||
grid_name = f"grid_{grid_id[0]}_{grid_id[1]}"
|
||||
project_dir = os.path.join(self.output_dir, grid_name, "project")
|
||||
texturing_dir = os.path.join(project_dir, "odm_texturing")
|
||||
texturing_dst_dir = os.path.join(project_dir, "odm_texturing_dst")
|
||||
split_obj_dir = os.path.join(texturing_dst_dir, "split_obj")
|
||||
opensfm_dir = os.path.join(project_dir, "opensfm")
|
||||
log_file = os.path.join(
|
||||
project_dir, "odm_orthophoto", "odm_orthophoto_log.txt")
|
||||
os.makedirs(texturing_dst_dir, exist_ok=True)
|
||||
|
||||
# 修改obj文件z坐标的值
|
||||
min_25d_z = self.get_min_z_from_obj(os.path.join(
|
||||
project_dir, 'odm_texturing_25d', 'odm_textured_model_geo.obj'))
|
||||
self.modify_z_in_obj(texturing_dir, min_25d_z)
|
||||
|
||||
# 在新文件夹下,利用UTM偏移量,修改obj文件顶点坐标,纹理文件下采样
|
||||
utm_offset = self.read_utm_offset(log_file)
|
||||
modified_obj = self.modify_obj_coordinates(
|
||||
texturing_dir, texturing_dst_dir, utm_offset)
|
||||
self.downsample_texture(texturing_dir, texturing_dst_dir)
|
||||
|
||||
# 将obj文件进行切片
|
||||
self.logger.info(f"开始切片网格 {grid_id} 的OBJ文件")
|
||||
os.makedirs(split_obj_dir)
|
||||
cmd = (
|
||||
f"D:\software\Obj2Tiles\Obj2Tiles.exe --stage Splitting --lods 1 --divisions 3 "
|
||||
f"{modified_obj} {split_obj_dir}"
|
||||
)
|
||||
subprocess.run(cmd, check=True)
|
||||
|
||||
# 执行格式转换,Linux下osgconv有问题,记得注释掉
|
||||
self.logger.info(f"开始转换网格 {grid_id} 的OBJ文件")
|
||||
# 先获取split_obj_dir下的所有obj文件
|
||||
obj_lod_dir = os.path.join(split_obj_dir, "LOD-0")
|
||||
obj_files = [f for f in os.listdir(
|
||||
obj_lod_dir) if f.endswith('.obj')]
|
||||
for obj_file in obj_files:
|
||||
obj_path = os.path.join(obj_lod_dir, obj_file)
|
||||
osgb_file = os.path.splitext(obj_file)[0] + '.osgb'
|
||||
osgb_path = os.path.join(split_obj_dir, osgb_file)
|
||||
# 执行 osgconv 命令
|
||||
subprocess.run(['osgconv', obj_path, osgb_path], check=True)
|
||||
|
||||
# 创建OSGB目录结构,复制文件
|
||||
osgb_base_dir = os.path.join(self.output_dir, "osgb")
|
||||
data_dir = os.path.join(osgb_base_dir, "Data")
|
||||
for obj_file in obj_files:
|
||||
obj_file_name = os.path.splitext(obj_file)[0]
|
||||
tile_dirs = os.path.join(data_dir, f"{obj_file_name}")
|
||||
os.makedirs(tile_dirs, exist_ok=True)
|
||||
shutil.copy2(os.path.join(split_obj_dir, obj_file_name+".osgb"), tile_dirs)
|
||||
|
||||
def _create_merged_metadata(self):
|
||||
"""创建合并后的metadata.xml文件"""
|
||||
# 转换为WGS84经纬度
|
||||
center_lon, center_lat = self.transformer.transform(
|
||||
self.ref_east, self.ref_north)
|
||||
metadata_content = f"""<?xml version="1.0" encoding="utf-8"?>
|
||||
<ModelMetadata version="1">
|
||||
<SRS>EPSG:4326</SRS>
|
||||
<SRSOrigin>{center_lon},{center_lat},0</SRSOrigin>
|
||||
<Texture>
|
||||
<ColorSource>Visible</ColorSource>
|
||||
</Texture>
|
||||
</ModelMetadata>"""
|
||||
|
||||
metadata_file = os.path.join(self.output_dir, "osgb", "metadata.xml")
|
||||
with open(metadata_file, 'w', encoding='utf-8') as f:
|
||||
f.write(metadata_content)
|
||||
|
||||
def read_utm_offset(self, log_file: str) -> tuple:
|
||||
"""读取UTM偏移量"""
|
||||
try:
|
||||
east_offset = None
|
||||
north_offset = None
|
||||
|
||||
with open(log_file, 'r') as f:
|
||||
lines = f.readlines()
|
||||
for i, line in enumerate(lines):
|
||||
if 'utm_north_offset' in line and i + 1 < len(lines):
|
||||
north_offset = float(lines[i + 1].strip())
|
||||
elif 'utm_east_offset' in line and i + 1 < len(lines):
|
||||
east_offset = float(lines[i + 1].strip())
|
||||
|
||||
if east_offset is None or north_offset is None:
|
||||
raise ValueError("未找到UTM偏移量")
|
||||
|
||||
return east_offset, north_offset
|
||||
except Exception as e:
|
||||
self.logger.error(f"读取UTM偏移量时发生错误: {str(e)}")
|
||||
raise
|
||||
|
||||
def modify_obj_coordinates(self, texturing_dir: str, texturing_dst_dir: str, utm_offset: tuple) -> str:
|
||||
"""修改obj文件中的顶点坐标,使用相对坐标系"""
|
||||
obj_file = os.path.join(
|
||||
texturing_dir, "odm_textured_model_modified.obj")
|
||||
obj_dst_file = os.path.join(
|
||||
texturing_dst_dir, "odm_textured_model_geo_utm.obj")
|
||||
if not os.path.exists(obj_file):
|
||||
raise FileNotFoundError(f"找不到OBJ文件: {obj_file}")
|
||||
shutil.copy2(os.path.join(texturing_dir, "odm_textured_model_geo.mtl"),
|
||||
os.path.join(texturing_dst_dir, "odm_textured_model_geo.mtl"))
|
||||
east_offset, north_offset = utm_offset
|
||||
self.logger.info(
|
||||
f"UTM坐标偏移:{east_offset - self.ref_east}, {north_offset - self.ref_north}")
|
||||
|
||||
try:
|
||||
with open(obj_file, 'r') as f_in, open(obj_dst_file, 'w') as f_out:
|
||||
for line in f_in:
|
||||
if line.startswith('v '):
|
||||
# 处理顶点坐标行
|
||||
parts = line.strip().split()
|
||||
# 使用相对于整体最小UTM坐标的偏移
|
||||
x = float(parts[1]) + (east_offset - self.ref_east)
|
||||
y = float(parts[2]) + (north_offset - self.ref_north)
|
||||
z = float(parts[3])
|
||||
f_out.write(f'v {x:.6f} {z:.6f} {-y:.6f}\n')
|
||||
elif line.startswith('vn '): # 处理法线向量
|
||||
parts = line.split()
|
||||
nx = float(parts[1])
|
||||
ny = float(parts[2])
|
||||
nz = float(parts[3])
|
||||
# 同步反转法线的 Y 轴
|
||||
new_line = f"vn {nx} {nz} {-ny}\n"
|
||||
f_out.write(new_line)
|
||||
else:
|
||||
# 其他行直接写入
|
||||
f_out.write(line)
|
||||
|
||||
return obj_dst_file
|
||||
except Exception as e:
|
||||
self.logger.error(f"修改obj坐标时发生错误: {str(e)}")
|
||||
raise
|
||||
|
||||
def downsample_texture(self, src_dir: str, dst_dir: str):
|
||||
"""复制并重命名纹理文件,对大于100MB的文件进行多次下采样,直到文件小于100MB
|
||||
Args:
|
||||
src_dir: 源纹理目录
|
||||
dst_dir: 目标纹理目录
|
||||
"""
|
||||
for file in os.listdir(src_dir):
|
||||
if file.lower().endswith(('.png')):
|
||||
src_path = os.path.join(src_dir, file)
|
||||
dst_path = os.path.join(dst_dir, file)
|
||||
|
||||
# 检查文件大小(以字节为单位)
|
||||
file_size = os.path.getsize(src_path)
|
||||
if file_size <= 100 * 1024 * 1024: # 如果文件小于等于100MB,直接复制
|
||||
shutil.copy2(src_path, dst_path)
|
||||
else:
|
||||
# 文件大于100MB,进行下采样
|
||||
img = cv2.imread(src_path, cv2.IMREAD_UNCHANGED)
|
||||
if_first_ds = True
|
||||
while file_size > 100 * 1024 * 1024: # 大于100MB
|
||||
self.logger.info(f"纹理文件 {file} 大于100MB,进行下采样")
|
||||
|
||||
if if_first_ds:
|
||||
# 计算新的尺寸(长宽各变为1/4)
|
||||
new_size = (img.shape[1] // 4,
|
||||
img.shape[0] // 4) # 逐步减小尺寸
|
||||
# 使用双三次插值进行下采样
|
||||
resized_img = cv2.resize(
|
||||
img, new_size, interpolation=cv2.INTER_CUBIC)
|
||||
if_first_ds = False
|
||||
else:
|
||||
# 计算新的尺寸(长宽各变为1/2)
|
||||
new_size = (img.shape[1] // 2,
|
||||
img.shape[0] // 2) # 逐步减小尺寸
|
||||
# 使用双三次插值进行下采样
|
||||
resized_img = cv2.resize(
|
||||
img, new_size, interpolation=cv2.INTER_CUBIC)
|
||||
|
||||
# 更新文件路径为下采样后的路径
|
||||
cv2.imwrite(dst_path, resized_img, [
|
||||
cv2.IMWRITE_PNG_COMPRESSION, 9])
|
||||
|
||||
# 更新文件大小和图像
|
||||
file_size = os.path.getsize(dst_path)
|
||||
img = cv2.imread(dst_path, cv2.IMREAD_UNCHANGED)
|
||||
self.logger.info(
|
||||
f"下采样后文件大小: {file_size / (1024 * 1024):.2f} MB")
|
||||
|
||||
def get_min_z_from_obj(self, file_path):
|
||||
min_z = float('inf') # 初始值设为无穷大
|
||||
with open(file_path, 'r') as obj_file:
|
||||
for line in obj_file:
|
||||
# 检查每一行是否是顶点定义(以 'v ' 开头)
|
||||
if line.startswith('v '):
|
||||
# 获取顶点坐标
|
||||
parts = line.split()
|
||||
# 将z值转换为浮动数字
|
||||
z = float(parts[3])
|
||||
# 更新最小z值
|
||||
if z < min_z:
|
||||
min_z = z
|
||||
return min_z
|
||||
|
||||
def modify_z_in_obj(self, texturing_dir, min_25d_z):
|
||||
obj_file = os.path.join(texturing_dir, 'odm_textured_model_geo.obj')
|
||||
output_file = os.path.join(
|
||||
texturing_dir, 'odm_textured_model_modified.obj')
|
||||
with open(obj_file, 'r') as f_in, open(output_file, 'w') as f_out:
|
||||
for line in f_in:
|
||||
if line.startswith('v '): # 顶点坐标行
|
||||
parts = line.strip().split()
|
||||
x = float(parts[1])
|
||||
y = float(parts[2])
|
||||
z = float(parts[3])
|
||||
|
||||
if z < min_25d_z:
|
||||
z = min_25d_z
|
||||
|
||||
f_out.write(f"v {x} {y} {z}\n")
|
||||
else:
|
||||
f_out.write(line)
|
@ -192,7 +192,7 @@ class MergeTif:
|
||||
f"{product_name}合并过程中发生错误: {str(e)}", exc_info=True)
|
||||
raise
|
||||
|
||||
def merge_all_tifs(self, grid_points: Dict[tuple, pd.DataFrame], produce_dem: bool):
|
||||
def merge_all_tifs(self, grid_points: Dict[tuple, pd.DataFrame], mode: str):
|
||||
"""合并所有产品(正射影像、DSM和DTM)"""
|
||||
try:
|
||||
products = [
|
||||
@ -204,7 +204,7 @@ class MergeTif:
|
||||
},
|
||||
|
||||
]
|
||||
if produce_dem:
|
||||
if mode == '三维模式':
|
||||
products.append(
|
||||
{
|
||||
'name': 'DSM',
|
||||
|
@ -182,7 +182,7 @@ class ODMProcessMonitor:
|
||||
|
||||
if self.mode == "快拼模式":
|
||||
docker_command += (
|
||||
# f"--fast-orthophoto "
|
||||
f"--fast-orthophoto "
|
||||
f"--skip-3dmodel "
|
||||
)
|
||||
# elif self.mode == "三维模式":
|
||||
|
Loading…
Reference in New Issue
Block a user