仅着色形状的内部

Coloring only the inside of a shape

假设你得到了这张图片

并获得了以编程方式仅在其内部涂上适当颜色的指令,但该程序不仅必须处理此形状和其他图元,而且还必须处理任何轮廓形状,无论它可能有多复杂和阴影或不。

这是我要解决的问题,但这就是我卡住的地方,教计算机看黑线和其中的颜色似乎应该很简单。但是搜索主要是找到了特征脸风格识别算法,在我看来这似乎是过度拟合并且比这个问题的至少基本形式所需的复杂性要大得多。

我想将其定义为监督学习分类器问题,其目的是为我的模型提供完整的图像,它会输出更小的 numpy 数组,这些数组由分类为 [=11= 的像素组成] 或 background。但为了做到这一点,我需要为其提供训练数据,这对我来说似乎需要手动标记我训练集中的每个像素,这显然违背了程序的目的。

既然你已经了解了背景,我的问题是,给定这张图片,是否有一种有效的方法来获得两个不同的数组,每个数组由所有相邻像素组成,不包含任何纯黑色 (RGB(0,0 ,0)) 像素?

这将使一组所有像素都在圆圈内部,另一个所有像素都在圆圈外部

您可以使用 scipy.ndimage.measurements.label 为您完成所有繁重的工作:

import scipy.ndimage
import scipy.misc

data = scipy.misc.imread(...)
assert data.ndim == 2, "Image must be monochromatic"

# finds and number all disjoint white regions of the image
is_white = data > 128
labels, n = scipy.ndimage.measurements.label(is_white)

# get a set of all the region ids which are on the edge - we should not fill these
on_border = set(labels[:,0]) | set(labels[:,-1]) | set(labels[0,:]) | set(labels[-1,:])

for label in range(1, n+1):  # label 0 is all the black pixels
    if label not in on_border:
        # turn every pixel with that label to black
        data[labels == label] = 0

这将填充图像中的所有闭合形状,考虑到图像边缘切割的形状不是闭合的