yolov5 图像拼接
时间: 2023-09-12 17:11:59 浏览: 189
YOLOv5中采用了一种图像增强方法称为Mosaic,它是将9张图像拼接在一起进行处理。这样做的好处是图像的大小增大了,标签也更多了。训练一张这样的拼接图像等同于训练了9张小图。这样的图像拼接操作可以提高模型的准确性和鲁棒性。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【YOLOv5】--图像增强部分](https://blog.csdn.net/weixin_47665864/article/details/128891916)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
相关问题
yolov5图像拼接代码
以下是YOLOv5中实现图像拼接的代码,使用了Mosaic方法:
```python
def mosaic(self, img, labels):
# 获取随机的3张图片
img4, labels4 = self.get_random_data()
img3, labels3 = self.get_random_data()
img2, labels2 = self.get_random_data()
# 将4张图片拼接在一起
new_image = np.zeros((self.img_size * 2, self.img_size * 2, 3), dtype=np.uint8)
new_image[0:self.img_size, 0:self.img_size] = img
new_image[self.img_size:self.img_size * 2, 0:self.img_size] = img2
new_image[0:self.img_size, self.img_size:self.img_size * 2] = img3
new_image[self.img_size:self.img_size * 2, self.img_size:self.img_size * 2] = img4
# 调整标签
new_labels = []
for label in labels:
x1, y1, x2, y2, cls_id = label
if x1 > self.img_size:
x1 += self.img_size
x2 += self.img_size
if y1 > self.img_size:
y1 += self.img_size
y2 += self.img_size
new_labels.append([x1, y1, x2, y2, cls_id])
for label in labels2:
x1, y1, x2, y2, cls_id = label
x1 += self.img_size
x2 += self.img_size
new_labels.append([x1, y1, x2, y2, cls_id])
for label in labels3:
x1, y1, x2, y2, cls_id = label
y1 += self.img_size
y2 += self.img_size
new_labels.append([x1, y1, x2, y2, cls_id])
for label in labels4:
x1, y1, x2, y2, cls_id = label
x1 += self.img_size
y1 += self.img_size
x2 += self.img_size
y2 += self.img_size
new_labels.append([x1, y1, x2, y2, cls_id])
return new_image, new_labels
```
yolov5训练集图像拼接代码
以下是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)
```
阅读全文