yolov5训练集图像拼接代码
时间: 2023-11-16 18:00:19 浏览: 31
以下是yolov5训练集图像拼接的代码:
```python
import os
import cv2
import numpy as np
def mosaic(imgs, size):
"""
拼接图片
:param imgs: 图片列表
:param size: 拼接的行列数
:return: 拼接后的图片
"""
h, w = imgs[0].shape[:2]
vis = np.zeros((h * size[1], w * size[0], 3), np.uint8)
for i in range(size[1]):
for j in range(size[0]):
img = imgs[i * size[0] + j]
vis[i * h:(i + 1) * h, j * w:(j + 1) * w, :] = img
return vis
def get_image_list(path):
"""
获取图片列表
:param path: 图片路径
:return: 图片列表
"""
img_list = []
for file_name in os.listdir(path):
if file_name.endswith('.jpg') or file_name.endswith('.png'):
img_path = os.path.join(path, file_name)
img = cv2.imread(img_path)
img_list.append(img)
return img_list
# 示例代码
img_path = 'path/to/image/folder'
img_list = get_image_list(img_path)
mosaic_img = mosaic(img_list, (4, 4))
cv2.imwrite('path/to/save/mosaic/image', mosaic_img)
```