diff --git a/scripts/server/extract_images.py b/scripts/server/extract_images.py new file mode 100644 index 000000000..680fdb381 --- /dev/null +++ b/scripts/server/extract_images.py @@ -0,0 +1,45 @@ +import os +import cv2 +import numpy as np +import sys +from google.protobuf import message +import mapper_pb2 + +def save_images_from_feature_msgs(input_folder, output_folder): + # 确保输出文件夹存在 + os.makedirs(output_folder, exist_ok=True) + + # 遍历输入文件夹中的所有protobuf二进制文件 + for filename in os.listdir(input_folder): + if filename.endswith('.bin'): + file_path = os.path.join(input_folder, filename) + with open(file_path, 'rb') as f: + feature_msg = mapper_pb2.FeatureMsg() + feature_msg.ParseFromString(f.read()) + + # 从FeatureMsg中提取ImageMsg + image_msg = feature_msg.image + + # 将bytes数据转换为numpy array + image_data = np.frombuffer(image_msg.data, dtype=np.uint8) + + # 重新调整形状 + image_array = image_data.reshape((image_msg.height, image_msg.width, image_msg.channels)) + + # 将数据转换为适合OpenCV的格式 + if image_msg.channels == 1: # Grayscale + image_array = cv2.cvtColor(image_array, cv2.COLOR_GRAY2BGR) + elif image_msg.channels == 3: # RGB + image_array = cv2.cvtColor(image_array, cv2.COLOR_RGB2BGR) + + # 保存为JPEG文件 + output_filename = os.path.join(output_folder, image_msg.name.decode('utf-8')) + cv2.imwrite(output_filename, image_array) + print(f"Saved: {output_filename}") + +if __name__ == "__main__": + # 从命令行参数获取输入和输出路径 + protobuf_dir = sys.argv[1] + image_path = sys.argv[2] + + save_images_from_feature_msgs(protobuf_dir, image_path) diff --git a/scripts/server/sfm.sh b/scripts/server/sfm.sh index ce82fef62..d151b1a46 100755 --- a/scripts/server/sfm.sh +++ b/scripts/server/sfm.sh @@ -1,10 +1,11 @@ #!/bin/sh - PROTOBUF_PATH="$1" PROJECT="${PROTOBUF_PATH}/proj" +IMAGE="${PROJECT}/images" mkdir -p ${PROJECT} +mkdir -p ${IMAGE} log_time() { date "+%Y-%m-%d %H:%M:%S:%3N" @@ -15,6 +16,9 @@ protoc --proto_path=/root/colmaptzt/colmap_detailed/scripts/server --python_out= echo "$(log_time) convert protobuf to database..." python3 /root/colmaptzt/colmap_detailed/scripts/server/database.py ${PROTOBUF_PATH} ${PROJECT}/database.db +echo "$(log_time) convert protobuf to images..." +python3 /root/colmaptzt/colmap_detailed/scripts/server/extract_images.py ${PROTOBUF_PATH} ${IMAGE} + echo "$(log_time) feature matcher..." /root/colmap_detailed/build/src/colmap/exe/colmap sequential_matcher \ --SequentialMatching.overlap 10 \ @@ -25,7 +29,7 @@ echo "$(log_time) feature exhaustive_matcher done." echo "$(log_time) glomap mapper..." /root/glomap/build/glomap/glomap mapper \ --database_path ${PROJECT}/database.db \ - --image_path ${PROJECT}/images \ + --image_path ${IMAGE} \ --output_path ${PROJECT}/sparse echo "$(log_time) glomap mapper done."