繁体   English   中英

在 RGB 图像上绘制多类语义分割透明覆盖

[英]Plotting multi-class semantic segmentation transparent overlays over RGB image

我有语义分割掩码的结果(值在 0-1 之间,需要 otsu 阈值来确定什么是积极的),我想直接在 RGB 图像上使用 plot,每个预测 class 在 RGB 图像上具有不同的随机颜色。

我使用了以下 plot 一个单一颜色的单一蒙版。 是否有 package 或简单的策略来为多类做到这一点?

fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=(5, 5))
  ax.imshow(image, cmap='gray')
  ax.axis('off')
  mask = (fused_mosaic[..., channel]*255).astype('uint8')
  ret3,th3 = cv2.threshold(mask,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  fig, ax = image_show(full_im)
  ax.imshow(mask>ret3, alpha=0.3)

我正在寻找这样的东西,只是没有盒子和标签的简单。 我尝试使用detectron2(在示例中生成此注释的package,但它们需要一些我无法弄清楚的奇怪元数据object)。 在此处输入图像描述

谢谢

Scikit-image有一个内置的label2rgb() function 根据 label 通道着色:

#!/usr/bin/env python3

from skimage import io
from skimage import color
from skimage import segmentation
import matplotlib.pyplot as plt

# URL for tiger image from Berkeley Segmentation Data Set BSDS
url=('http://www.eecs.berkeley.edu/Research/Projects/CS/vision/bsds/BSDS300/html/images/plain/normal/color/108073.jpg')

# Load tiger image from URL
tiger = io.imread(url)

# Segment image with SLIC - Simple Linear Iterative Clustering
seg = segmentation.slic(tiger, n_segments=30, compactness=40.0, enforce_connectivity=True, sigma=3)

# Generate automatic colouring from classification labels
io.imshow(color.label2rgb(seg,tiger))
plt.show()

在此处输入图像描述

With our conversation above, you have a 2D NumPy array of integer IDs where each element in this array determines the class ID of said pixel thus giving you a semantic segmentation output.

我建议您分三个阶段执行此操作。

  1. 创建一个大小为N x 4的 RGB 颜色 map,其中N是分割中 output 类的总数。 因此,每个 class i都被分配了一个 RGBA 颜色像素值,您将使用它来为 output 着色。

  2. 展平输入 integer NumPy 数组,使其成为一维 NumPy 数组,我们可以使用它来索引 (1)

  3. 最后在 (1) 中索引到 RGB 颜色 map。 这将创建一个(R x C) x 4 2D NumPy 数组,其中包含 output 彩色图像,将语义标签映射到颜色。 当然,我们需要将其恢复为原始输入尺寸,因此重新调整此数组,使其变为R x C x 4供您显示。 最后,因为我们现在有一个图像的 alpha 通道,所以您可以将其显示在原始图像的顶部。


步骤 #1 - 生成颜色 map

matplotlib很好的工具来为你生成这种颜色 map。 您可以从此使用cm模块。 首先,确定您想使用哪种颜色 map 用于您的目的。 它们的完整列表可以在这里找到: https://matplotlib.org/3.1.1/tutorials/colors/colormaps.html 我将 go 与 Viridis 一起使用,因为这是matplotlib当前使用的默认设置。

假设您系统中的类总数为N ,首先生成颜色 map,然后创建一个从 0 到 1 的线性间隔数组,其中包含N个元素,以从头到尾均匀地创建颜色 map。 另请注意,这将生成N x 4色 map,最后一列是 alpha 通道。 这对以后非常重要。 Specifically, this method will colour any pixel with label 0 to belong to the lower end of the colour map and because this is a semantic segmentation output, label 0 should correspond to the background so we should set the alpha channel for this label to be 0要透明。 我们可以将颜色的 rest 设置为您想要的 alpha,即代码中的 0.3。

from matplotlib import cm
import numpy as np
N = ... # You define this here
colours = cm.get_cmap('viridis', N)  # Change the string from 'viridis' to whatever you want from the above link
cmap = colours(np.linspace(0, 1, N))  # Obtain RGB colour map
cmap[0,-1] = 0  # Set alpha for label 0 to be 0
cmap[1:,-1] = 0.3  # Set the other alphas for the labels to be 0.3

步骤 #2 - 进行语义分割 output 并找到合适的颜色

这是直截了当的。 假设fused_mosaic是我们之前讨论过的 2D integer 数组,将这个数组展平并索引您的颜色 map:

output = cmap[fused_mosaic.flatten()]

步骤 #3 - 重塑为所需的 output

这又是直截了当的:

R, C = fused_mosaic.shape[:2]
output = output.reshape((R, C, -1))

output现在将包含语义分割 map 中每个 object 的 RGBA 渲染图像。 然后,您最终可以使用它并将其显示在图像顶部。 使用您的代码,这将是:

fig, ax = image_show(full_im)  # Don't know what this does but it's from your code
ax.imshow(output)

要将所有内容联系在一起,这就是我最终要做的:

## Step #1
from matplotlib import cm
import numpy as np
N = ... # You define this here
colours = cm.get_cmap('viridis', N)  # Change the string from 'viridis' to whatever you want from the above link
cmap = colours(np.linspace(0, 1, N))  # Obtain RGB colour map
cmap[0,-1] = 0  # Set alpha for label 0 to be 0
cmap[1:,-1] = 0.3  # Set the other alphas for the labels to be 0.3

## Step #2
output = cmap[fused_mosaic.flatten()]

## Step #3
R, C = fused_mosaic.shape[:2]
output = output.reshape((R, C, -1))

## Overlay
fig, ax = image_show(full_im)  # Don't know what this does but it's from your code
ax.imshow(output)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM