【问题标题】:make mask border more distinguished in instance segmentation mask使蒙版边界在实例分割蒙版中更加明显
【发布时间】:2021-03-03 19:53:47
【问题描述】:

我有以下实例分割掩码:

我想在将单元格之间的边界转换为二进制掩码之前使其更加明显。

如果我拍摄图像并对其进行二值化,我会得到以下图像:

是否有解决此问题的内置函数,我找不到? 如果没有最好的方法是什么?

.npy 格式的原始图像位于https://filebin.net/eixoprrp0o7opz7k

【问题讨论】:

  • @PatrickArtner 我已经检查过了,但是当图像是二进制时它工作得很好。在每个实例的值都不同于 1 的情况下,我遇到了麻烦。
  • 可能使用原始图像的(二值化)副本,然后将其侵蚀并组合回原始图像(乘以)会扩大间隙。任何地方你乘以 0 得到 0,所有其他地方都与你乘以 1 相同?
  • 可以上传原图吗?我假设你想分开接触的物体?还是它们已经被标记了,而您只是想绘制轮廓?
  • @GaneshTata 我添加了一个下载图片的链接(没有轴)。

标签: python image-processing image-segmentation


【解决方案1】:

我喜欢opencv 图书馆。这是一种绘制轮廓的方法。

import numpy as np
import matplotlib.pyplot as plt
import cv2

img = np.load('test.npy').astype(np.int32) # the method only works with 32bit image
contour_thickness = 1

contours, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img , contours, -1, 0, contour_thickness)

plt.imshow(img)
plt.show()

【讨论】:

  • 一个非常优雅的解决方案,它完全符合我的实现。 (可能有效)
【解决方案2】:

所以我找不到直接的方法来实现我的目标,所以我自己实现了它。

import numpy as np
from scipy.ndimage.filters import gaussian_filter

def fix_patch(patch, val):
    patch_tmp = np.where(patch == val, patch, 0)
    blurred_patch = gaussian_filter(patch_tmp, sigma=0.7)
    patch_tmp = np.where((blurred_patch < int(0.9 * val)) & (blurred_patch > int(0.5 * val)), 0, 1)
    return patch * patch_tmp


def smart_matrix_indexing(r_min, r_max, c_min, c_max, mat):
    row_max, col_max = np.subtract(mat.shape, (1, 1))
    r_min = np.max([r_min - 3, 0])
    r_max = np.min([r_max + 3, row_max])
    c_min = np.max([c_min - 3, 0])
    c_max = np.min([c_max + 3, col_max])
    return r_min, r_max, c_min, c_max


def fix_segmentation_maps(mask):
    unique_values = np.unique(mask)
    unique_values = unique_values[np.where(unique_values > 0)]
    for val in unique_values:
        r, c = np.where(mask == val)
        r_min, r_max, c_min, c_max = smart_matrix_indexing(r.min(), r.max(), c.min(), c.max(), mask)
        patch = mask[r_min:r_max, c_min:c_max]
        mask[r_min:r_max, c_min:c_max] = fix_patch(patch, val)
    return mask

fixed_mask = fix_segmentation_maps(mask)

这将输出以下图像:

逻辑:

  1. 为每个实例元素找到一个窗口
  2. 从图片中裁剪
  3. 将非实例的所有内容归零
  4. 使用高斯滤波器平滑
  5. 保留边界元素
  6. 取反值 (1 -> 0, 0 ->1)。这将只清零我们希望删除的边界
  7. 将它乘以补丁并重新分配它

所以它能够以某种下降方式设置边界。 如果有更好的方法我很乐意听到。

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2019-10-13
    • 1970-01-01
    • 2020-11-06
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多