如何使用PIL确定具有相同值的像素区域

4

我需要将一张图片分为若干个像素区域,这些区域的RGB值要通过某种测试。
扫描图片并检查每个像素的值对我来说已经没问题了,但是将它们聚类到区域中,然后获取这些区域的坐标(x、y、宽度、高度)却让我一筹莫展 :)
以下是我目前的代码:

from PIL import Image

def detectRedRegions(PILImage):
      image = PILImage.load()
      width, height = PILImage.size
      reds = []
      h = 0
      while h < height:
        w = 0
        while w < width:
          px = image[w, h]
          if is_red(px):
            reds.append([w, h])
            # Here's where I'm being clueless 
          w +=1
        h +=1

我阅读了很多关于集群的文章,但就是无法理解这个主题。如果您能提供适合我的需求的代码示例,那将是非常好的(并且希望能够启发我)。

谢谢!


1
你到底想做什么?一般来说,具有特定颜色的区域不会是一个正方形,而是一个任意形状(虽然以某种方式相连),因此要通过简单的元组(x、y、宽度、高度)来找到和定义一个区域将是一个挑战。 - Taro Sato
我知道区域不会是正方形的 :) 我想要的是(对于我的需求来说应该足够准确)获得围绕区域的边界框(对此我没有表达清楚,抱歉)。 - Yaniv Golan
2个回答

6

[编辑]

虽然下面的解决方案可行,但可以进一步改进。这是一个名称更好、性能更好的版本:

from itertools import product
from PIL import Image, ImageDraw


def closed_regions(image, test):
    """
    Return all closed regions in image who's pixels satisfy test.
    """
    pixel = image.load()
    xs, ys = map(xrange, image.size)
    neighbors = dict((xy, set([xy])) for xy in product(xs, ys) if test(pixel[xy]))
    for a, b in neighbors:
        for cd in (a + 1, b), (a, b + 1):
            if cd in neighbors:
                neighbors[a, b].add(cd)
                neighbors[cd].add((a, b))
    seen = set()
    def component(node, neighbors=neighbors, seen=seen, see=seen.add):
        todo = set([node])
        next_todo = todo.pop
        while todo:
            node = next_todo()
            see(node)
            todo |= neighbors[node] - seen
            yield node
    return (set(component(node)) for node in neighbors if node not in seen)


def boundingbox(coordinates):
    """
    Return the bounding box that contains all coordinates.
    """
    xs, ys = zip(*coordinates)
    return min(xs), min(ys), max(xs), max(ys)


def is_black_enough(pixel):
    r, g, b = pixel
    return r < 10 and g < 10 and b < 10


if __name__ == '__main__':

    image = Image.open('some_image.jpg')
    draw = ImageDraw.Draw(image)
    for rect in disjoint_areas(image, is_black_enough):
        draw.rectangle(boundingbox(region), outline=(255, 0, 0))
    image.show()

与下面的 disjoint_areas() 不同,closed_regions() 返回像素坐标集而不是它们的边界框。

此外,如果我们使用 flooding 而不是连通组件算法,我们可以使它变得更简单,速度大约快两倍:

from itertools import chain, product
from PIL import Image, ImageDraw


flatten = chain.from_iterable


def closed_regions(image, test):
    """
    Return all closed regions in image who's pixel satisfy test.
    """
    pixel = image.load()
    xs, ys = map(xrange, image.size)
    todo = set(xy for xy in product(xs, ys) if test(pixel[xy]))
    while todo:
        region = set()
        edge = set([todo.pop()])
        while edge:
            region |= edge
            todo -= edge
            edge = todo.intersection(
                flatten(((x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)) for x, y in edge))
        yield region

# rest like above

这个灵感来自Eric S. Raymond的泛洪算法版本

[/EDIT]

也许可以使用泛洪算法,但我喜欢这个方法:

from collections import defaultdict
from PIL import Image, ImageDraw


def connected_components(edges):
    """
    Given a graph represented by edges (i.e. pairs of nodes), generate its
    connected components as sets of nodes.

    Time complexity is linear with respect to the number of edges.
    """
    neighbors = defaultdict(set)
    for a, b in edges:
        neighbors[a].add(b)
        neighbors[b].add(a)
    seen = set()
    def component(node, neighbors=neighbors, seen=seen, see=seen.add):
        unseen = set([node])
        next_unseen = unseen.pop
        while unseen:
            node = next_unseen()
            see(node)
            unseen |= neighbors[node] - seen
            yield node
    return (set(component(node)) for node in neighbors if node not in seen)


def matching_pixels(image, test):
    """
    Generate all pixel coordinates where pixel satisfies test.
    """
    width, height = image.size
    pixels = image.load()
    for x in xrange(width):
        for y in xrange(height):
            if test(pixels[x, y]):
                yield x, y


def make_edges(coordinates):
    """
    Generate all pairs of neighboring pixel coordinates.
    """
    coordinates = set(coordinates)
    for x, y in coordinates:
        if (x - 1, y - 1) in coordinates:
            yield (x, y), (x - 1, y - 1)
        if (x, y - 1) in coordinates:
            yield (x, y), (x, y - 1)
        if (x + 1, y - 1) in coordinates:
            yield (x, y), (x + 1, y - 1)
        if (x - 1, y) in coordinates:
            yield (x, y), (x - 1, y)
        yield (x, y), (x, y)


def boundingbox(coordinates):
    """
    Return the bounding box of all coordinates.
    """
    xs, ys = zip(*coordinates)
    return min(xs), min(ys), max(xs), max(ys)


def disjoint_areas(image, test):
    """
    Return the bounding boxes of all non-consecutive areas
    who's pixels satisfy test.
    """
    for each in connected_components(make_edges(matching_pixels(image, test))):
        yield boundingbox(each)


def is_black_enough(pixel):
    r, g, b = pixel
    return r < 10 and g < 10 and b < 10


if __name__ == '__main__':

    image = Image.open('some_image.jpg')
    draw = ImageDraw.Draw(image)
    for rect in disjoint_areas(image, is_black_enough):
        draw.rectangle(rect, outline=(255, 0, 0))
    image.show()

这里,满足is_black_enough()条件的相邻像素对被解释为图形中的边缘。此外,每个像素被视为其自身的邻居。由于这种重新解释,我们可以使用图形的连通组件算法,这很容易实现。结果是所有像素满足is_black_enough()条件的区域的边界框序列。

5
你需要的是图像处理中的区域标记或连通组件检测。 scipy.ndimage 包中提供了一个实现。因此,只要安装了numpy + scipy,以下内容应该就可以使用。
import numpy as np
import scipy.ndimage as ndi
import Image

image = Image.load()
# convert to numpy array (no data copy done since both use buffer protocol)
image = np.asarray(image)
# generate a black and white image marking red pixels as 1
bw = is_red(image)
# labeling : each region is associated with an int
labels, n = ndi.label(bw)
# provide bounding box for each region in the form of tuples of slices
objects = ndi.find_objects(labels)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接