需求
将6个面合并成一张全景图
实现代码
import uuid
import os
import py360convert
import cv2
def image_transpose():
# 给图片唯一标识
uuid0 = str(uuid.uuid1())
# 获取图片信息,六个面依次为:前、后、左、右、上、下
front_image_path = "img/front.jpg"
backward_image_path = "img/back.jpg"
left_image_path = "img/left.jpg"
right_image_path = "img/right.jpg"
top_image_path = "img/top.jpg"
bottom_image_path = "img/bottom.jpg"
quality_para = 3 # 可以根据需要调整
target_size_w = 3000 # 可以根据需要调整
# 获取图片宽度
target_size_w = int(target_size_w)
target_size_h = int(target_size_w // 2)
# 控制图片清晰度
quality_map = {
0: 40,
1: 60,
2: 90,
3: 100
}
quality_save = quality_map.get(quality_para, 70)
output_dir = './static/trans' + uuid0
os.makedirs(output_dir, exist_ok=True)
out_path = os.path.join(output_dir, 'panorama.jpg')
# 合成全景图
cube_dice0 = cv2.imread(front_image_path)
cube_dice1 = cv2.imread(right_image_path)
cube_dice2 = cv2.imread(backward_image_path)
cube_dice3 = cv2.imread(left_image_path)
cube_dice4 = cv2.imread(top_image_path)
cube_dice5 = cv2.imread(bottom_image_path)
# 检查图像是否成功读取
if cube_dice0 is None or cube_dice1 is None or cube_dice2 is None or cube_dice3 is None or cube_dice4 is None or cube_dice5 is None:
print("Error: One or more images could not be read. Please check the image paths.")
return
cube_dice1 = cv2.flip(cube_dice1, 1)
cube_dice2 = cv2.flip(cube_dice2, 1)
cube_dice4 = cv2.flip(cube_dice4, 0)
res = py360convert.c2e(
[cube_dice0, cube_dice1, cube_dice2, cube_dice3, cube_dice4, cube_dice5],
target_size_h, target_size_w, cube_format='list'
)
cv2.imwrite(out_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), quality_save])
print("Panorama image saved at: " + out_path)
# 调用函数
image_transpose()
全景图(Panorama)通常是通过将多个图像拼接在一起形成一个宽视角的图像。在这段代码中,使用了六个面图(前、后、左、右、上、下)来生成一个 360 度的全景图。具体步骤如下:
-
生成唯一标识符:
uuid0 = str(uuid.uuid1())
生成一个唯一的标识符,用于创建输出目录,确保每次运行代码时不会覆盖之前的结果。
-
定义图像路径和参数:
front_image_path = "img/front.jpg" backward_image_path = "img/back.jpg" left_image_path = "img/left.jpg" right_image_path = "img/right.jpg" top_image_path = "img/top.jpg" bottom_image_path = "img/bottom.jpg" quality_para = 3 # 可以根据需要调整 target_size_w = 3000 # 可以根据需要调整
定义六个面图的路径和一些参数,如图片质量和目标宽度。
-
计算目标图像的高度:
target_size_w = int(target_size_w) target_size_h = int(target_size_w // 2)
目标图像的高度是宽度的一半,这是因为全景图的宽高比通常是 2:1。
-
设置图片质量:
quality_map = { 0: 40, 1: 60, 2: 90, 3: 100 } quality_save = quality_map.get(quality_para, 70)
根据
quality_para
设置图片的保存质量。如果quality_para
不在quality_map
中,则使用默认值 70。 -
创建输出目录:
output_dir = './static/trans' + uuid0 os.makedirs(output_dir, exist_ok=True) out_path = os.path.join(output_dir, 'panorama.jpg')
创建一个唯一的输出目录来保存生成的全景图。
-
读取六个面图:
cube_dice0 = cv2.imread(front_image_path) cube_dice1 = cv2.imread(right_image_path) cube_dice2 = cv2.imread(backward_image_path) cube_dice3 = cv2.imread(left_image_path) cube_dice4 = cv2.imread(top_image_path) cube_dice5 = cv2.imread(bottom_image_path)
使用 OpenCV 读取六个面图。
-
检查图像是否成功读取:
if cube_dice0 is None or cube_dice1 is None or cube_dice2 is None or cube_dice3 is None or cube_dice4 is None or cube_dice5 is None: print("Error: One or more images could not be read. Please check the image paths.") return
确保所有图像都成功读取,如果有任何图像读取失败,打印错误信息并退出。
-
翻转图像:
cube_dice1 = cv2.flip(cube_dice1, 1) cube_dice2 = cv2.flip(cube_dice2, 1) cube_dice4 = cv2.flip(cube_dice4, 0)
对某些面图进行翻转,以确保它们在拼接时方向正确。
-
合成全景图:
res = py360convert.c2e( [cube_dice0, cube_dice1, cube_dice2, cube_dice3, cube_dice4, cube_dice5], target_size_h, target_size_w, cube_format='list' )
使用
py360convert
库的c2e
函数将六个面图转换为一张全景图。c2e
函数的参数包括六个面图、目标图像的高度和宽度,以及面图的格式(这里是列表格式)。 -
保存全景图:
cv2.imwrite(out_path, res, [int(cv2.IMWRITE_JPEG_QUALITY), quality_save])
使用 OpenCV 的
imwrite
函数将生成的全景图保存到指定路径,并设置图片质量。 -
打印输出路径:
print("Panorama image saved at: " + out_path)
打印全景图的保存路径。
总结
这段代码的核心思路是:
- 读取六个面图。
- 对某些面图进行必要的翻转。
- 使用
py360convert
库将六个面图转换为一张全景图。 - 将生成的全景图保存到指定路径。
通过这些步骤,你可以将六个面图合并成一张完整的 360 度全景图。