Python深度学习之使用Albumentations对图像做增强

作者:AI浩 时间:2023-11-24 06:18:23 

一、导入所需的库


import random

import cv2
from matplotlib import pyplot as plt

import albumentations as A

二、定义可视化函数显示图像上的边界框和类标签

可视化函数参考https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/vis.py


BOX_COLOR = (255, 0, 0) # Red
TEXT_COLOR = (255, 255, 255) # White

def visualize_bbox(img, bbox, class_name, color=BOX_COLOR, thickness=2):
   """Visualizes a single bounding box on the image"""
   x_min, y_min, w, h = bbox
   x_min, x_max, y_min, y_max = int(x_min), int(x_min + w), int(y_min), int(y_min + h)

cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness)

((text_width, text_height), _) = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.35, 1)    
   cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), BOX_COLOR, -1)
   cv2.putText(
       img,
       text=class_name,
       org=(x_min, y_min - int(0.3 * text_height)),
       fontFace=cv2.FONT_HERSHEY_SIMPLEX,
       fontScale=0.35,
       color=TEXT_COLOR,
       lineType=cv2.LINE_AA,
   )
   return img

def visualize(image, bboxes, category_ids, category_id_to_name):
   img = image.copy()
   for bbox, category_id in zip(bboxes, category_ids):
       class_name = category_id_to_name[category_id]
       img = visualize_bbox(img, bbox, class_name)
   plt.figure(figsize=(12, 12))
   plt.axis('off')
   plt.imshow(img)

三、获取图像和标注

在此示例中,我们将使用来自COCO数据集的图像,该图像具有两个关联的边界框。 该映像位于http://cocodataset.org/#explore?id=386298

从磁盘加载图像


image = cv2.imread('images/000000386298.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

用坐标和类标签定义两个边界框

这些边界框的坐标使用coco格式声明。 每个边界框使用四个值[x_min, y_min, width, height]进行描述。 有关边界框坐标的不同格式的详细说明,请参阅有关边界框的文档文章-https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/。


bboxes = [[5.66, 138.95, 147.09, 164.88], [366.7, 80.84, 132.8, 181.84]]
category_ids = [17, 18]

# We will use the mapping from category_id to the class name
# to visualize the class label for the bounding box on the image
category_id_to_name = {17: 'cat', 18: 'dog'}

展示图像的边框


visualize(image, bboxes, category_ids, category_id_to_name)

Python深度学习之使用Albumentations对图像做增强

四、使用RandomSizedBBoxSafeCrop保留原始图像中的所有边界框

RandomSizedBBoxSafeCrop crops a random part of the image. It ensures that the cropped part will contain all bounding boxes from the original image. Then the transform rescales the crop to height and width specified by the respective parameters. The erosion_rate parameter controls how much area of the original bounding box could be lost after cropping. erosion_rate = 0.2 means that the augmented bounding box's area could be up to 20% smaller than the area of the original bounding box.

RandomSizedBBoxSafeCrop裁剪图像的随机部分。 它确保裁剪的部分将包含原始图像的所有边界框。 然后,变换会将作物重新缩放为相应参数指定的高度和宽度。 erosion_rate参数控制裁剪后可能丢失原始边界框的面积。 frosting_rate = 0.2表示扩充后的边界框的面积可能比原始边界框的面积小20%。

五、定义增强管道


transform = A.Compose(
   [A.RandomSizedBBoxSafeCrop(width=448, height=336, erosion_rate=0.2)],
   bbox_params=A.BboxParams(format='coco', label_fields=['category_ids']),
)

六、输入用于增强的图像和边框

我们固定随机种子是为了可视化目的,因此增强将始终产生相同的结果。 在真实的计算机视觉管道中,您不应该在对图像应用转换之前固定随机种子,因为在这种情况下,管道将始终输出相同的图像。 图像增强的目的是每次使用不同的变换。


random.seed(7)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
   transformed['image'],
   transformed['bboxes'],
   transformed['category_ids'],
   category_id_to_name,
)

Python深度学习之使用Albumentations对图像做增强

七、其他不同随机种子的示例


random.seed(3)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
   transformed['image'],
   transformed['bboxes'],
   transformed['category_ids'],
   category_id_to_name,
)

Python深度学习之使用Albumentations对图像做增强


random.seed(444)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
   transformed['image'],
   transformed['bboxes'],
   transformed['category_ids'],
   category_id_to_name,
)

Python深度学习之使用Albumentations对图像做增强

来源:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/117077423

标签:Python,Albumentations,图像增强
0
投稿

猜你喜欢

  • go for range遍历二维数组的示例

    2024-04-25 15:27:01
  • 详解Python爬虫的基本写法

    2022-12-26 07:49:45
  • django+mysql的使用示例

    2022-10-24 20:34:15
  • pycharm通过anaconda安装pyqt5的教程

    2023-09-06 03:31:37
  • Oracle中instr函数使用方法

    2023-07-23 19:14:06
  • sqlserver 存储过程动态参数调用实现代码

    2024-01-25 05:11:07
  • 在thinkphp5.0路径中实现去除index.php的方式

    2024-05-11 09:54:34
  • python 字符串转列表 list 出现\\ufeff的解决方法

    2023-11-24 18:47:06
  • 在python带权重的列表中随机取值的方法

    2022-05-09 01:44:25
  • 详解Node.js读写中文内容文件操作

    2024-05-03 15:55:07
  • Python多进程协作模拟实现流程

    2021-07-01 17:52:38
  • 多个python文件调用logging模块报错误

    2021-03-30 23:30:24
  • 语言编程花絮内建构建顺序示例详解

    2023-11-04 09:42:12
  • 简单的XML操作:XML文件创建

    2008-04-25 10:31:00
  • python接口自动化如何封装获取常量的类

    2023-04-17 07:44:22
  • python中reload重载实例用法

    2021-05-12 07:02:09
  • 详解微信小程序文件下载--视频和图片

    2023-08-10 16:38:50
  • XML教程—编写结构完整的XML文档

    2008-10-11 13:43:00
  • python实现合并多个list及合并多个django QuerySet的方法示例

    2023-11-25 06:43:13
  • python使用xpath获取页面元素的使用

    2021-04-30 10:32:09
  • asp之家 网络编程 m.aspxhome.com