前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用PixelLib来实现图像分割

使用PixelLib来实现图像分割

作者头像
小白学视觉
发布2022-04-06 09:19:02
5740
发布2022-04-06 09:19:02
举报
文章被收录于专栏:深度学习和计算机视觉

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

图像分割

一些计算机视觉问题需要让计算机对图像内容有更深入的理解。分类和目标检测可能不适合解决这些问题,我们非常需要一种有效的技术来解这类的计算机视觉问题,图像分割技术应运而生。

每个图像都由一组像素值组成。图像分割是在像素级对图像进行分类的任务。机器能够根据分配给图像中每个像素值的类将图像分割成不同的段,从而更有效地分析图像。

在图像分割中采用的独特技术使其适用于解决某些计算机视觉问题。这些问题需要有关图像中出现的对象详细信息,无法通过对整个图像进行分类或为图像中存在的对象提供边界框来提供详细信息。图像分割的一些主要应用包括:

  • 帮助无人驾驶汽车视觉系统有效的了解道路场景。
  • 医学图像分割:为执行诊断测试提供身体部位的分割。
  • 卫星图像分析。

图像分割有两种主要类型:

语义分割:使用相同类别的对象使用相同的颜色映射进行分割。

实例分割:它不同于语义分割,它会对同一对象的不同实例用不同的颜色映射来进行分割。

下面三幅图片有助于你理解语义分割和实例分割。

原图:

语义分割:

实例分割:

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+模型来实现语义分割的代码。

代码语言:javascript
复制
import pixellib
from 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")

我们来观察每一行代码:

代码语言:javascript
复制
import pixellib
from pixellib.semantic import semantic_segmentation 
segment_image = semantic_segmentation()

执行语义分割的类是从pixelLib导入的,我们创建了该类的一个实例。

代码语言:javascript
复制
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
代码语言:javascript
复制
segment_image.segmentAsPascalvoc(“path_to_image”, output_image_name = “path_to_output_image)

我们加载该函数对图像执行分割。这个函数有两个参数…

  • path_to_image:这个是要分割的图像路径。
  • output_image_name:这个是保存分割图像的路径。它将保存在当前工作目录中。

sample1.jpg:

代码语言:javascript
复制
import pixellib
from 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")

对图像中的对象进行分割并保存结果。如果需要,可以在图像上应用覆盖分割。

代码语言:javascript
复制
segment_image.segmentAsPascalvoc("sample1.jpg", output_image_name = "image_new.jpg", overlay = True)

我们添加了额外的参数overlay并将其设置为true,我们得到了一个对象上具有覆盖分割的图像。

通过修改下面的代码,可以检查执行分割所需的时间。

代码语言:javascript
复制
import pixellib
from pixellib.semantic import semantic_segmentation
import 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")
代码语言:javascript
复制
Inference Time: 7.38seconds

对图像进行语义分割需要7.38秒。

该模型是在pascal voc数据集上训练的,这个数据集有20个对象类别。

对象及其对应的颜色映射:

PixelLib也是可以返回分割输出的数组:

使用此代码获取分割输出的数组,

代码语言:javascript
复制
output, segmap = segment_image.segmentAsPascalvoc()

通过修改下面的语义分割代码,可以测试获取数组的代码并打印出输出的形状。

代码语言:javascript
复制
import pixellib
from pixellib.semantic import semantic_segmentation
import 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)

使用此代码获取输出和覆盖分割的数组,

代码语言:javascript
复制
segmap, segoverlay = segment_image.segmentAsPascalvoc(overlay = True)
代码语言:javascript
复制
import pixellibfrom pixellib.semantic import semantic_segmentationimport cv2segment_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实例分割。

实现实例分割的代码:

代码语言:javascript
复制
import pixellib
from 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")

观察每一行代码

代码语言:javascript
复制
import pixellib
from pixellib.instance import instance_segmentation
segment_image = instance_segmentation()

导入执行实例分割的类,我们创建了该类的一个实例。

代码语言:javascript
复制
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
代码语言:javascript
复制
segment_image.segmentImage("path_to_image", output_image_name = "output_image_path")

这是对图像执行实例分割的代码,它需要两个参数:

  • path_to_image:模型要预测的图像路径。
  • output_image_path:保存分割结果的路径。它将保存在当前工作目录中。

sample2.jpg:

代码语言:javascript
复制
import pixellib
from 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")

这是当前工作目录中保存的图像。

可以使用边界框实现分割。这可以通过修改代码来实现。

代码语言:javascript
复制
segment_image.segmentImage("path_to_image", output_image_name = "output_image_path", show_bboxes = True)

我们添加了一个额外的参数show_bboxes并将其设置为true,分割掩码由边界框生成。

通过修改下面的代码,可以检查执行分割所需的时间。

代码语言:javascript
复制
import pixellib
from pixellib.instance import instance_segmentation
import 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")
代码语言:javascript
复制
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数组
  • 分割掩码数组
  • 输出的数组

使用此代码

代码语言:javascript
复制
segmask, output = segment_image.segmentImage()

通过修改下面的实例分割代码,可以测试获取数组的代码并打印出输出的形状。

代码语言:javascript
复制
import pixellib
from pixellib.instance import instance_segmentation
import 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,获得带边界框的分割数组。

代码语言:javascript
复制
segmask, output = segment_image.segmentImage(show_bboxes = True)
代码语言:javascript
复制
import pixellib
from pixellib.instance import instance_segmentation
import 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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-02-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小白学视觉 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档