目录
1.图像拼接
2.合成视频
2.1 cv2.videowriter_fourcc功能介绍
2.2读取图片合成视频
1.图像拼接
实现目标:将自动驾驶车6个摄像头采集到的图片,按照正确顺序拼接显示,nuScenes数据集测试如下:
图像存放文件夹目录如下:
每个目录下有相同名称,不同视角采集到的图像 。
先显示图片,代码如下:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import os
"""
将六张图片在同一窗口下显示,关闭后打开后一帧的六张拼接图片
"""
def show_images(images, titles=None, num_cols=None, scale=3, normalize=False):
""" 一个窗口中绘制多张图像:
Args:
images: 可以为一张图像(不要放在列表中),也可以为一个图像列表
titles: 图像对应标题、
num_cols: 每行最多显示多少张图像
scale: 用于调整图窗大小
normalize: 显示灰度图时是否进行灰度归一化
"""
# 加了下面2行后可以显示中文标题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
"""
# 单张图片显示
if not isinstance(images, list):
if not isinstance(scale, tuple):
scale = (scale, scale * 1.5)
plt.figure(figsize=(scale[1], scale[0]))
img = images
if len(img.shape) == 3:
# opencv库中函数生成的图像为BGR通道,需要转换一下
B, G, R = cv.split(img)
img = cv.merge([R, G, B])
plt.imshow(img)
elif len(img.shape) == 2:
# pyplot显示灰度需要加一个参数
if normalize:
plt.imshow(img, cmap='gray')
else:
plt.imshow(img, cmap='gray', vmin=0, vmax=255)
else:
raise TypeError("Invalid shape " +
str(img.shape) + " of image data")
if titles is not None:
plt.title(titles, y=-0.15)
plt.axis('off')
plt.show()
return
"""
# 多张图片显示
if not isinstance(scale, tuple):
scale = (scale, scale)
num_imgs = len(images)
if num_cols is None:
num_cols = int(np.ceil((np.sqrt(num_imgs))))
num_rows = (num_imgs - 1) // num_cols + 1
idx = list(range(num_imgs))
_, figs = plt.subplots(num_rows, num_cols,
figsize=(scale[1] * num_cols, scale[0] * num_rows))
for f, i, img in zip(figs.flat, idx, images):
if len(img.shape) == 3:
# opencv库中函数生成的图像为BGR通道,需要转换一下
B, G, R = cv.split(img)
img = cv.merge([R, G, B])
f.imshow(img)
elif len(img.shape) == 2:
# pyplot显示灰度需要加一个参数
if normalize:
f.imshow(img, cmap='gray')
else:
f.imshow(img, cmap='gray', vmin=0, vmax=255)
else:
raise TypeError("Invalid shape " +
str(img.shape) + " of image data")
if titles is not None:
f.set_title(titles[i], y=-0.15)
f.axes.get_xaxis().set_visible(False)
f.axes.get_yaxis().set_visible(False)
# 将不显示图像的fig移除,不然会显示多余的窗口
if len(figs.shape) == 1:
figs = figs.reshape(-1, figs.shape[0])
for i in range(num_rows * num_cols - num_imgs):
figs[num_rows - 1, num_imgs % num_cols + i].remove()
plt.show()
if __name__ == '__main__':
"""
src1= cv.imread("vis_result/camera-2")
src2 = cv.imread("vis_result/camera-0")
src3 = cv.imread("vis_result/camera-1")
src4 = cv.imread("vis_result/camera-4")
src5 = cv.imread("vis_result/camera-3")
src6 = cv.imread("vis_result/camera-5")
"""
dir0 = "D:/pythonproject/bevshow/vis_result"
dir1 = r"D:/pythonproject/bevshow/vis_result/camera-0/"
path = os.listdir(dir1)
for name in path:
src1 = cv.imread(os.path.join(dir0, "camera-2", name))
src2 = cv.imread(os.path.join(dir0, "camera-0", name))
src3 = cv.imread(os.path.join(dir0, "camera-1", name))
src4 = cv.imread(os.path.join(dir0, "camera-5", name))
src5 = cv.imread(os.path.join(dir0, "camera-3", name))
src6 = cv.imread(os.path.join(dir0, "camera-4", name))
title1 = "front_left"
title2 = "front"
title3 = "front_right"
title4 = "back_right"
title5 = "back"
title6 = "back_left"
show_images([src1, src2, src3, src4, src5, src6], [title1, title2, title3, title4, title5, title6], num_cols=3)
# show_images(src, title)
实现效果为将6个文件夹中对应帧图片(同名图片)在一个窗口中显示,并显示标题:
拼接图像并保存,实现参考这篇:
使用Python批量拼接图片
代码如下:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import os
from PIL import Image
"""
输入六张图片文件夹,输出逐帧拼接的图片并保存在指定目录下
"""
# 保存路径
image_dir = 'D:/pythonproject/bevshow/result/'
# 6个文件夹根目录
dir0 = "D:/pythonproject/bevshow/vis_result"
# 其中一个文件夹,读取图片名用
dir1 = r"D:/pythonproject/bevshow/vis_result/camera-0/"
if __name__ == '__main__':
"""
src1= cv.imread("vis_result/camera-2")
src2 = cv.imread("vis_result/camera-0")
src3 = cv.imread("vis_result/camera-1")
src4 = cv.imread("vis_result/camera-4")
src5 = cv.imread("vis_result/camera-3")
src6 = cv.imread("vis_result/camera-5")
"""
path = os.listdir(dir1)
for name in path:
images = []
images0 = Image.open(os.path.join(dir0, "camera-2", name))
# print(os.path.join(dir0, "camera-2", name))
# D:/pythonproject/bevshow/vis_result\camera-2\1533151603547590-3e8750f331d7499e9b5123e9eb70f2e2.png
images1 = Image.open(os.path.join(dir0, "camera-0", name))
images2 = Image.open(os.path.join(dir0, "camera-1", name))
images3 = Image.open(os.path.join(dir0, "camera-4", name))
images4 = Image.open(os.path.join(dir0, "camera-3", name))
images5 = Image.open(os.path.join(dir0, "camera-5", name))
# 如果大小不同可以resize一下
"""
images0 = images0.resize((1600, 900))
images1 = images1.resize((1600, 900))
images2 = images2.resize((1600, 900))
images3 = images3.resize((1600, 900))
images4 = images4.resize((1600, 900))
images5 = images5.resize((1600, 900))
"""
# 拼接图片,按照自己想要的顺序,从左到右,从上到下
images.append(images0)
images.append(images1)
images.append(images2)
images.append(images3)
images.append(images4)
images.append(images5)
new_image = Image.new('RGB', (4800, 1800), 'white')
# 将四个图像粘贴到新图像的正确位置
new_image.paste(images[0], (0, 0))
new_image.paste(images[1], (1600, 0))
new_image.paste(images[2], (3200, 0))
new_image.paste(images[3], (0, 900))
new_image.paste(images[4], (1600, 900))
new_image.paste(images[5], (3200, 900))
# 将最终图像保存到磁盘上
new_image.save(image_dir + name)
print(new_image)
# show_images([src1, src2, src3, src4, src5, src6], [title1, title2, title3, title4, title5, title6], num_cols=3)
# show_images(src, title)
将拼接好的图像会保存在提前建立的result文件夹中
2.合成视频
读取result文件夹中拼接好的图像,按照一定帧数合成为视频
参考:python 将图片合成视频
2.1 cv2.videowriter_fourcc功能介绍
cv2.videowriter_fourcc是OpenCV中的一个函数,用于创建一个视频编码器对象。它接受四个字符作为参数,用于指定视频编码器的类型。这些字符通常是四个大写字母,例如MJPG、XVID、DIVX等。使用cv2.videowriter_fourcc可以将视频数据编码为指定类型的视频文件,以便在其他设备上播放或编辑。
参数列表
cv2.VideoWriter_fourcc('M', 'P', '4', 'V')
MPEG-4编码 .mp4 可指定结果视频的大小
cv2.VideoWriter_fourcc('X','2','6','4')
MPEG-4编码 .mp4 可指定结果视频的大小
cv2.VideoWriter_fourcc('I', '4', '2', '0')
该参数是YUV编码类型,文件名后缀为.avi 广泛兼容,但会产生大文件
cv2.VideoWriter_fourcc('P', 'I', 'M', 'I')
该参数是MPEG-1编码类型,文件名后缀为.avi
cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
该参数是MPEG-4编码类型,文件名后缀为.avi,可指定结果视频的大小
cv2.VideoWriter_fourcc('T', 'H', 'E', 'O')
该参数是Ogg Vorbis,文件名后缀为.ogv
cv2.VideoWriter_fourcc('F', 'L', 'V', '1')
该参数是Flash视频,文件名后缀为.flv
报错1:
[ WARN:0@0.033] global loadsave.cpp:248 cv::findDecoder imread_('D:/pythonproject/bevshow/result1533151603547590-3e8750f331d7499e9b5123e9eb70f2e2.png'): can't open/read file: check file path/integrity
Traceback (most recent call last):
File "D:\pythonproject\bevshow\test03.py", line 15, in <module>
img = cv2.resize(img, (1280, 720)) # 将图片转换为1280*720
cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4062: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
解决方案:查看图片是否为空,若为空则是路径问题,修改后即可
2.2读取图片合成视频
此处读取的图片格式为png,参考这一篇:【Python入门教程】基于OpenCV视频分解成图片+图片组合成视频(视频抽帧组帧)
代码如下:
# -*- coding: utf-8 -*-
"""
图片组帧成视频
"""
import os
import cv2
from PIL import Image
def Image_Frame(images_path, out_path, fps):
"""
:param images_path: 输入需要组帧的图片文件夹路径
:param out_path: 输出视频路径
:param fps: 视频帧率
:return: None
"""
print("-------------------------图片组帧-------------------------")
images_lists = os.listdir(images_path) # images_lists.sort()
image_size = Image.open(os.path.join(images_path, images_lists[0])).size
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
video_writer = cv2.VideoWriter(out_path, fourcc, fps, image_size)
for image_list in images_lists:
image_path = os.path.join(images_path, image_list)
frame = cv2.imread(image_path)
video_writer.write(frame)
print("正在添加:", image_list)
video_writer.release()
print("-------------------------组帧完成-------------------------")
if __name__ == "__main__":
Image_path = r'D:\pythonproject\bevshow\vis_result\camera-0'
Out_path = r'D:/pythonproject/bevshow/testdir/1.mp4'
FPS = int(input("请输入帧率:"))
Image_Frame(Image_path, Out_path, FPS)
在运行时一切都正常,但就是文件夹里没有合成的视频,我也不清楚什么原因,后来问了作者,发现是out_path要到文件名,不能只到文件夹,修改后可以生成而且速度比较快。
换了里另一个代码试了一下:python学习之合并多张图片转成mp4视频代码实现
报错2:
ValueError: append_data requires ndarray as first arg
解决:
frame = image.resize((1600, 912))
frame = np.array(frame)
运行成功代码为:
import os
import imageio
import numpy as np
from PIL import Image
"""
读取拼接好的图片,将其生成指定帧数视频并保存在指定目录下
"""
# 设置生成的视频文件名和路径
filename = 'output.mp4'#文件名
filepath = os.path.join("D:/pythonproject/bevshow/testdir", filename)#存储路径
path = "D:/pythonproject/bevshow/result"#图片读取路径
# 读取所有 PNG 图片
images = []
for file_name in sorted(os.listdir(path)):
if file_name.endswith('.png'):
img = Image.open(os.path.join(path, file_name))
images.append(img)
# 将图片转换为视频
fps = 30 # 每秒钟30帧
with imageio.get_writer(filepath, fps=fps) as video:
for image in images:
frame = image.resize((1600, 912))
frame = np.array(frame)
video.append_data(frame)
在指定目录下生成了MP4格式视频:
可以调整帧数以及尺寸大小,调尺寸可能会报错,但是可以在报错里找到给出的合适尺寸,照着修改即可 ,视频效果如下:文章来源:https://www.toymoban.com/news/detail-799519.html
自动驾驶采集多视角图像处理生成视频文章来源地址https://www.toymoban.com/news/detail-799519.html
3.完整代码
import os
import imageio
import numpy as np
from PIL import Image
"""
将两个步骤整合,输入6个文件夹,直接输出视频
"""
"""
输入六张图片文件夹,输出逐帧拼接的图片并保存在指定目录下
"""
# 保存路径
image_dir = 'D:/pythonproject/bevshow/result/'
# 6个文件夹根目录
dir0 = "D:/pythonproject/bevshow/vis_result"
# 其中一个文件夹,读取图片名用
dir1 = r"D:/pythonproject/bevshow/vis_result/camera-0/"
filename = 'output1.mp4' # 保存视频名称
filepath = os.path.join("D:/pythonproject/bevshow/testdir", filename) # 保存视频路径
if __name__ == '__main__':
path = os.listdir(dir1)
img = []
for name in path:
images = []
images0 = Image.open(os.path.join(dir0, "camera-2", name)) # 左前
images1 = Image.open(os.path.join(dir0, "camera-0", name)) # 前
images2 = Image.open(os.path.join(dir0, "camera-1", name)) # 右前
images3 = Image.open(os.path.join(dir0, "camera-5", name)) # 右后
images4 = Image.open(os.path.join(dir0, "camera-3", name)) # 后
images5 = Image.open(os.path.join(dir0, "camera-4", name)) # 左后
# 拼接图片,按照自己想要的顺序,从左到右,从上到下
images.append(images0)
images.append(images1)
images.append(images2)
images.append(images3)
images.append(images4)
images.append(images5)
new_image = Image.new('RGB', (4800, 1800), 'white')
# 将四个图像粘贴到新图像的正确位置
new_image.paste(images[0], (0, 0))
new_image.paste(images[1], (1600, 0))
new_image.paste(images[2], (3200, 0))
new_image.paste(images[3], (0, 900))
new_image.paste(images[4], (1600, 900))
new_image.paste(images[5], (3200, 900))
img.append(new_image)
# 将图片转换为视频
fps = 10 # 每秒钟30帧
with imageio.get_writer(filepath, fps=fps) as video:
for image in img:
frame = image.resize((1872, 1088))
frame = np.array(frame)
video.append_data(frame)
到了这里,关于自动驾驶采集多视角图像处理(python实现不同文件夹下同名图片批量拼接并生成视频)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!