Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

python - 获取像素最近的邻居颜色(Getting pixel nearest neighbor color)

I have an image with edges looks like this:

(我有一个带有边缘的图像,看起来像这样:)

在此处输入图片说明

And I'm using this two methods to fix the edges:

(我正在使用这两种方法来修复边缘:)

def get_colors(pil_image, limit=4):
    pil_image = np.array(pil_image)
    image_colors = {}
    for row in pil_image:
        for pxl in row:
            pxl = tuple(pxl)
            if not image_colors.get(pxl):
                image_colors[pxl] = 1
            else:
                image_colors[pxl] += 1
    sorted_image_colors = sorted(image_colors, key=image_colors.get, reverse=True)
    return sorted_image_colors[:limit]


def remove_anti_aliasing(colors, img_ndarr):
    colors = np.array(colors).astype(np.int32)
    img_ndarr = img_ndarr.astype(np.int32)
    distances = np.argmin(np.array([np.sqrt(np.sum((color - img_ndarr) ** 2, axis=2)) for color in colors]), axis=0)
    return colors[distances].astype(np.uint8)

And the result looks like this:

(结果看起来像这样:)

在此处输入图片说明

The edges still looks like the first but now the color is limited to 4.

(边缘仍然看起来像第一个,但现在颜色限制为4。)

I want to achieve something like this where there's only 2 colors on the edges:

(我想要实现这样的功能,其中边缘只有两种颜色:)

在此处输入图片说明

How can I do that?

(我怎样才能做到这一点?)

  ask by conquistador translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...