【发布时间】:2021-05-17 02:55:33
【问题描述】:
我有一个跑步者的以下 NumPy 数组,你可以在这里下载: https://drive.google.com/file/d/1SfIEqGsBV_vA7iP4UjLdklLJlLdDzozL/view?usp=sharing
要显示它,请使用以下代码:
import numpy as np
import matplotlib.pyplot as plt
# load data
data = np.load('running_man.npy')
# plot data
plt.imshow(data)
如您所见,图像中有很多噪点(雀斑)。我想摆脱它并检索跑步者的干净图像。知道怎么做吗?
这是我目前所做的:
from skimage import measure
# Find contours at a constant value of 1
contours = measure.find_contours(data, 1, fully_connected='high')
# Select the largest contiguous contour
contour = sorted(contours, key=lambda x: len(x))[-1]
# Create an empty image to store the masked array
r_mask = np.zeros_like(data, dtype='bool')
# Create a contour image by using the contour coordinates rounded to their nearest integer value
r_mask[np.round(contour[:, 0]).astype('int'), np.round(contour[:, 1]).astype('int')] = 1
# Fill in the hole created by the contour boundary
r_mask = ndimage.binary_fill_holes(r_mask)
# Invert the mask since one wants pixels outside of the region
r_mask = ~r_mask
plt.imshow(r_mask)
...但是你可以看到轮廓非常粗糙! 行之有效的是将图像上传到在线 jpg 到 SVG 转换器 -> 这使得线条超级流畅。 ...但我希望能够在 python 中做到这一点。
想法: 我正在寻找可以保持尖角的东西,也许可以检测沿边缘的渐变并仅保持渐变高于某个阈值的点...
【问题讨论】:
-
这个问题有什么变化吗?
-
@IanChu 是的,我将链接更改为数据。它现在应该可以工作了! (不得不删除旧问题并重新发布,由于某种原因它不起作用)
标签: python-3.x numpy image-processing