衡量像素块的连续程度

Measure of how much contiguous is a block of pixels

我正在使用 Genetic Algorithms (GA) 解决 image processing 问题(更精确的图像分割)。在这种情况下,individual 表示一个像素块(即一组像素坐标)。我需要鼓励 contiguous 像素的个人。

鼓励连续的像素块:

我面临的问题是如何在一组像素坐标 (x, y) 上测量此特征(多少 contiguous)?

如下图所示,右边的个体(黑色像素集)显然比左边的个体 "contiguous"(因此更健康):


                       

我想我明白你在问什么,我的建议是计算你的像素之间共享 "walls" 的数量:

我认为从左到右,个体的连续性在下降。

计算墙的数量并不难编写代码,但我在这里实现它的方式可能会很慢。

import random

width = 5
height = 5
image = [[0 for x in range(width)] for y in range(height)]

num_pts_in_individual = 4

#I realize this may give replicate points
individual = [[int(random.uniform(0,height)),int(random.uniform(0,width))] for x in range(num_pts_in_individual)]

#Fill up the image
for point in individual:
    image[point[0]][point[1]] = 1

#Print out the image
for row in image:
    print row


def count_shared_walls(image):
    num_shared = 0
    height = len(image)
    width = len(image[0])
    for h in range(height):
        for w in range(width):
            if image[h][w] == 1:
                if h > 0 and image[h-1][w] == 1:
                    num_shared += 1
                if w > 0 and image[h][w-1] == 1:
                    num_shared += 1
                if h < height-1 and image[h+1][w] == 1:
                    num_shared += 1
                if w < width-1 and image[h][w+1] == 1:
                    num_shared += 1
    return num_shared

shared_walls = count_shared_walls(image)
print shared_walls

共享墙的不同图片和数量:

[0, 0, 0, 0, 0]
[0, 1, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 1, 1]
[0, 0, 0, 0, 0]
2


[1, 0, 0, 0, 0]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 1, 0, 0]
0

[0, 0, 0, 1, 1]
[0, 0, 0, 1, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
4

这方面的一个主要问题是,如果像素位置发生变化但不改变共享墙的数量,则不会影响分数。也许结合使用您描述的距离方法和共享墙方法是最好的。