打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用PixelLib来实现图像分割

年来,计算机视觉快速发展。目前流行的计算机视觉技术如图像分类、目标检测等已被广泛应用于解决许多计算机视觉问题。在图像分类中,对整个图像进行分类。在目标检测中,则是通过检测图像中单个目标的位置来扩展图像分类。

图像分割

一些计算机视觉问题需要让计算机对图像内容有更深入的理解。分类和目标检测可能不适合解决这些问题,我们非常需要一种有效的技术来解这类的计算机视觉问题,图像分割技术应运而生。
每个图像都由一组像素值组成。图像分割是在像素级对图像进行分类的任务。机器能够根据分配给图像中每个像素值的类将图像分割成不同的段,从而更有效地分析图像。
在图像分割中采用的独特技术使其适用于解决某些计算机视觉问题。这些问题需要有关图像中出现的对象详细信息,无法通过对整个图像进行分类或为图像中存在的对象提供边界框来提供详细信息。图像分割的一些主要应用包括:
  • 帮助无人驾驶汽车视觉系统有效的了解道路场景。
  • 医学图像分割:为执行诊断测试提供身体部位的分割。
  • 卫星图像分析。
图像分割有两种主要类型:
语义分割:使用相同类别的对象使用相同的颜色映射进行分割。
实例分割:它不同于语义分割,它会对同一对象的不同实例用不同的颜色映射来进行分割。
下面三幅图片有助于你理解语义分割和实例分割。
原图:
语义分割:
实例分割:
PixelLib:是为了在现实生活中更容易实现图像分割而构建的库。PixelLib是一个灵活的库,可以集成到需要应用图像分割的软件解决方案中。
语义分割和实例分割可以用五行代码实现。
安装PixelLib及其依赖项:
安装最新版本的tensorflow(tensorflow 2.0),使用:
  • pip3 install tensorflow
使用以下命令安装opencv python:
  • pip3 install opencv-python
使用以下命令安装scikit映像:
  • pip3 install scikit-image
安装Pillow :
  • pip3 install pillow
安装Pixellib:
  • pip3 install pixellib
用PixelLib实现语义分割:
在pascal voc数据集上训练deeplabv3+模型来实现语义分割的代码。
import pixellibfrom pixellib.semantic import semantic_segmentation
segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5') segment_image.segmentAsPascalvoc('path_to_image', output_image_name = 'path_to_output_image')
我们来观察每一行代码:
import pixellibfrom pixellib.semantic import semantic_segmentation segment_image = semantic_segmentation()
执行语义分割的类是从pixelLib导入的,我们创建了该类的一个实例。
segment_image.load_pascalvoc_model(“deeplabv3_xception_tf_dim_ordering_tf_kernels.h5”)
在上面的代码中,我们加载了在pascal voc上训练的用于分割对象的xception 模型。模型可以从这里下载。
  • https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5
segment_image.segmentAsPascalvoc(“path_to_image”, output_image_name = “path_to_output_image)
我们加载该函数对图像执行分割。这个函数有两个参数…
  • path_to_image:这个是要分割的图像路径。
  • output_image_name:这个是保存分割图像的路径。它将保存在当前工作目录中。
sample1.jpg:
import pixellibfrom pixellib.semantic import semantic_segmentation
segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5') segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name = 'image_new.jpg')
对图像中的对象进行分割并保存结果。如果需要,可以在图像上应用覆盖分割。
segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name = 'image_new.jpg', overlay = True)
我们添加了额外的参数overlay并将其设置为true,我们得到了一个对象上具有覆盖分割的图像。
通过修改下面的代码,可以检查执行分割所需的时间。
import pixellibfrom pixellib.semantic import semantic_segmentationimport time
segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('pascal.h5')
start = time.time()segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name= 'image_new.jpg')
end = time.time()print(f'Inference Time: {end-start:.2f}seconds')
Inference Time: 7.38seconds
对图像进行语义分割需要7.38秒。
该模型是在pascal voc数据集上训练的,这个数据集有20个对象类别。
对象及其对应的颜色映射:
PixelLib也是可以返回分割输出的数组:
使用此代码获取分割输出的数组,
output, segmap = segment_image.segmentAsPascalvoc()
通过修改下面的语义分割代码,可以测试获取数组的代码并打印出输出的形状。
import pixellibfrom pixellib.semantic import semantic_segmentationimport cv2
segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('pascal.h5')output, segmap = segment_image.segmentAsPascalvoc('sample1.jpg')cv2.imwrite('img.jpg', output)print(output.shape)
使用此代码获取输出和覆盖分割的数组,
segmap, segoverlay = segment_image.segmentAsPascalvoc(overlay = True)
import pixellibfrom pixellib.semantic import semantic_segmentationimport cv2
segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('pascal.h5')segmap, segoverlay = segment_image.segmentAsPascalvoc('sample1.jpg', overlay= True)cv2.imwrite('img.jpg', segoverlay)print(segoverlay.shape)

使用PIXELLIB的实例分割:

基于Mask R-CNN框架的PixelLib实例分割。

实现实例分割的代码:

import pixellibfrom pixellib.instance import instance_segmentation
segment_image = instance_segmentation()segment_image.load_model('mask_rcnn_coco.h5') segment_image.segmentImage('path_to_image', output_image_name = 'output_image_path')

观察每一行代码

import pixellibfrom pixellib.instance import instance_segmentationsegment_image = instance_segmentation()
导入执行实例分割的类,我们创建了该类的一个实例。
segment_image.load_model('mask_rcnn_coco.h5')
这是加载mask r-cnn模型执行实例分割的代码。从这里下载mask r-cnn模型。
  • https://github.com/ayoolaolafenwa/PixelLib/releases/download/1.2/mask_rcnn_coco.h5
segment_image.segmentImage('path_to_image', output_image_name = 'output_image_path')
这是对图像执行实例分割的代码,它需要两个参数:
  • path_to_image:模型要预测的图像路径。
  • output_image_path:保存分割结果的路径。它将保存在当前工作目录中。
sample2.jpg:

import pixellibfrom pixellib.instance import instance_segmentation
segment_image = instance_segmentation()segment_image.load_model('mask_rcnn_coco.h5') segment_image.segmentImage('sample2.jpg', output_image_name = 'image_new.jpg')
这是当前工作目录中保存的图像。
可以使用边界框实现分割。这可以通过修改代码来实现。
segment_image.segmentImage('path_to_image', output_image_name = 'output_image_path', show_bboxes = True)
我们添加了一个额外的参数show_bboxes并将其设置为true,分割掩码由边界框生成。
通过修改下面的代码,可以检查执行分割所需的时间。
import pixellibfrom pixellib.instance import instance_segmentationimport time
segment_image = instance_segmentation()segment_image.load_model('mask_rcnn_coco.h5')
start = time.time()segment_image.segmentImage('former.jpg', output_image_name= 'image_new.jpg')
end = time.time()print(f'Inference Time: {end-start:.2f}seconds')
Inference Time: 12.87seconds
在图像上运行实例分割需要12.87秒。
Mask Rúu CNN模型是在microsoftco数据集上训练的,该数据集有80个公共对象类别。该模型可以对这些对象类别进行实例分割。
Coco数据集中的对象类别列表:
[‘BG’, ‘person’, ‘bicycle’, ‘car’, ‘motorcycle’, ‘airplane’, ‘bus’, ‘train’, ‘truck’, ‘boat’, ‘traffic light’, ‘fire hydrant’, ‘stop sign’, ‘parking meter’, ‘bench’, ‘bird’, ‘cat’, ‘dog’, ‘horse’, ‘sheep’, ‘cow’, ‘elephant’, ‘bear’, ‘zebra’, ‘giraffe’, ‘backpack’, ‘umbrella’, ‘handbag’, ‘tie’, ‘suitcase’, ‘frisbee’, ‘skis’, ‘snowboard’, ‘sports ball’, ‘kite’, ‘baseball bat’, ‘baseball glove’, ‘skateboard’, ‘surfboard’, ‘tennis racket’, ‘bottle’, ‘wine glass’, ‘cup’, ‘fork’, ‘knife’, ‘spoon’, ‘bowl’, ‘banana’, ‘apple’, ‘sandwich’, ‘orange’, ‘broccoli’, ‘carrot’, ‘hot dog’, ‘pizza’, ‘donut’, ‘cake’, ‘chair’, ‘couch’, ‘potted plant’, ‘bed’, ‘dining table’, ‘toilet’, ‘tv’, ‘laptop’, ‘mouse’, ‘remote’, ‘keyboard’, ‘cell phone’, ‘microwave’, ‘oven’, ‘toaster’, ‘sink’, ‘refrigerator’, ‘book’, ‘clock’, ‘vase’, ‘scissors’, ‘teddy bear’, ‘hair drier’, ‘toothbrush’]
PixelLib的专业用途有很多,例如分割。
获取以下数组:
  • 检测到的对象数组
  • 对象对应类的id数组
  • 分割掩码数组
  • 输出的数组
使用此代码
segmask, output = segment_image.segmentImage()
通过修改下面的实例分割代码,可以测试获取数组的代码并打印出输出的形状。
import pixellibfrom pixellib.instance import instance_segmentationimport cv2
instance_seg = instance_segmentation()instance_seg.load_model('mask_rcnn_coco.h5')segmask, output = instance_seg.segmentImage('sample2.jpg')cv2.imwrite('img.jpg', output)print(output.shape)
通过包含参数show_bboxes,获得带边界框的分割数组。
segmask, output = segment_image.segmentImage(show_bboxes = True)
import pixellibfrom pixellib.instance import instance_segmentationimport cv2
instance_seg = instance_segmentation()instance_seg.load_model('mask_rcnn_coco.h5')segmask, output = instance_seg.segmentImage('sample2.jpg', show_bboxes= True)cv2.imwrite('img.jpg', output)print(output.shape)
安装PixelLib并用你的图像来测试它。
访问PixelLib的官方github存储库。
  • https://github.com/ayoolaolafenwa/PixelLib
访问PixelLib的官方文档
  • https://pixellib.readthedocs.io/en/latest/
参考链接:https://towardsdatascience.com/image-segmentation-with-six-lines-0f-code-acb870a462e8

☆ END ☆
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Py之pixellib:pixellib库的简介、安装、经典案例之详细攻略
【论文推荐】最新七篇图像分割相关论文—半监督学习、多源域适应、多器官分割、知识全卷积网络、Quick...
Instance Segmentation 和 Semantic Segmentation
RCNN学习笔记(8):Fully Convolutional Networks for Semantic Segmentation(全卷积网络FCN)
A 2021 guide to Semantic Segmentation
Projects
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服