使用PixelLib来实现图像分割

小白学视觉

共 7749字,需浏览 16分钟

 ·

2022-02-17 19:35

点击上方小白学视觉”,选择加"星标"或“置顶

重磅干货,第一时间送达

年来,计算机视觉快速发展。目前流行的计算机视觉技术如图像分类、目标检测等已被广泛应用于解决许多计算机视觉问题。在图像分类中,对整个图像进行分类。在目标检测中,则是通过检测图像中单个目标的位置来扩展图像分类。
图像分割
一些计算机视觉问题需要让计算机对图像内容有更深入的理解。分类和目标检测可能不适合解决这些问题,我们非常需要一种有效的技术来解这类的计算机视觉问题,图像分割技术应运而生。
每个图像都由一组像素值组成。图像分割是在像素级对图像进行分类的任务。机器能够根据分配给图像中每个像素值的类将图像分割成不同的段,从而更有效地分析图像。
在图像分割中采用的独特技术使其适用于解决某些计算机视觉问题。这些问题需要有关图像中出现的对象详细信息,无法通过对整个图像进行分类或为图像中存在的对象提供边界框来提供详细信息。图像分割的一些主要应用包括:
  • 帮助无人驾驶汽车视觉系统有效的了解道路场景。
  • 医学图像分割:为执行诊断测试提供身体部位的分割。
  • 卫星图像分析。
图像分割有两种主要类型:
语义分割:使用相同类别的对象使用相同的颜色映射进行分割。
实例分割:它不同于语义分割,它会对同一对象的不同实例用不同的颜色映射来进行分割。
下面三幅图片有助于你理解语义分割和实例分割。
原图:
语义分割:
实例分割:
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 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实例分割。
实现实例分割的代码:
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
下载1:OpenCV-Contrib扩展模块中文版教程
在「小白学视觉」公众号后台回复:扩展模块中文教程即可下载全网第一份OpenCV扩展模块教程中文版,涵盖扩展模块安装、SFM算法、立体视觉、目标跟踪、生物视觉、超分辨率处理等二十多章内容。

下载2:Python视觉实战项目52讲
小白学视觉公众号后台回复:Python视觉实战项目即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。

下载3:OpenCV实战项目20讲
小白学视觉公众号后台回复:OpenCV实战项目20讲即可下载含有20个基于OpenCV实现20个实战项目,实现OpenCV学习进阶。

交流群


欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~


浏览 28
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报