仅对形状的内部着色 [英] Coloring only the inside of a shape

查看:88
本文介绍了仅对形状的内部着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们说给你这张图片

并给出了仅以编程方式对其内部进行适当颜色着色的指令,但是该程序不仅必须在此形状和其他图元上工作,而且还必须在任何轮廓形状上工作,无论它可能是复杂的还是带有阴影的。

and are given the instruction to programmatically color only the inside of it the appropriate color, but the program would have to not only work on this shape and other primitives but on any outlined shape, however complex it may be and shaded or not.

这是我要解决的问题,但这是我遇到的问题,教电脑看黑线似乎很简单,并且里面的颜色。但是搜索主要是使用特征脸样式识别算法,在我看来,这种算法过于合适,而且复杂度远远超过了解决该问题的基本形式所需要的复杂度。

This is the problem I am trying to solve, but here's where I'm stuck, it seems like it should be simple to teach a computer to see black lines, and color inside them. But searching mostly turns up eigenface style recognition algorithms, which seems to me to be over fitting and far greater complexity than is needed for at least the basic form of this problem.

我想将其构造为监督学习分类器问题,其目的是为模型提供完整的图像,它将输出较小的 numpy 数组,该数组由分类为对象背景。但是要做到这一点,我需要给它训练数据,在我看来,我需要手工标记训练集中的每个像素,这显然违反了程序的目的。

I would like to frame this as a supervised learning classifier problem, the purpose of which is to feed my model a complete image and it will output smaller numpy arrays consisting of pixels classsified as object or background. But in order to do that I would need to give it training data, which to me seems like I would need to hand label every pixel in my training set, which obviously defeats the purpose of the program.

现在您已经有了背景,这是我的问题,给定此图像,是否有一种有效的方法来获得两个不同的数组,每个数组均由不包含任何纯黑色(RGB(0 ,0,0))像素?

Now that you have the background, here's my question, given this image, is there an efficient way to get two distinct arrays, each consisting of all adjacent pixels that do not contain any solid black (RGB(0,0,0)) pixels?

其中一个将所有像素设置在圆的内部,而另一个将所有像素设置在圆的外部

Which would make one set all pixels on the inside of the circle, and the other, all pixels on the outside of the circle

推荐答案

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

You can use scipy.ndimage.measurements.label to do all the heavy lifting for you:

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

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

This will fill all closed shapes within the image, considering a shape cut by the edge of the image not to be closed

这篇关于仅对形状的内部着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆