假设你得到了这张图片
并给出了仅以编程方式为其内部涂上适当颜色的指令,但该程序不仅必须处理此形状和其他图元,而且还必须处理任何轮廓形状,无论它可能有多复杂以及是否有阴影.
这是我要解决的问题,但这是我卡住的地方,教计算机看黑线和其中的颜色似乎应该很简单。但搜索大多会找到特征脸风格识别算法,在我看来,这似乎是过度拟合,并且至少比这个问题的基本形式所需的复杂性要大得多。
我想将其定义为一个监督学习分类器问题,其目的是为我的模型提供一个完整的图像,它会输出较小的 numpy
数组,这些数组由分类为 的像素组成对象
或背景
。但为了做到这一点,我需要为其提供训练数据,这对我来说似乎需要手动标记训练集中的每个像素,这显然违背了程序的目的。
现在你已经了解了背景,下面是我的问题,给定这张图片,是否有一种有效的方法来获得两个不同的数组,每个数组由不包含任何纯黑色的所有相邻像素组成(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
这将填充图像中的所有闭合形状,考虑到图像边缘切割的形状不是闭合的
关于python - 仅着色形状的内部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36585243/