Skip to content

Commit

Permalink
add extract_images.py
Browse files Browse the repository at this point in the history
  • Loading branch information
“xiaobeifeng” committed Nov 4, 2024
1 parent 8ae2360 commit 7f7f0fe
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
45 changes: 45 additions & 0 deletions scripts/server/extract_images.py
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 6 additions & 2 deletions scripts/server/sfm.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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 \
Expand All @@ -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."

Expand Down

0 comments on commit 7f7f0fe

Please sign in to comment.