Python 如何使用PIL确定具有共享值的像素区域

Python 如何使用PIL确定具有共享值的像素区域,python,numpy,scipy,python-imaging-library,cluster-analysis,Python,Numpy,Scipy,Python Imaging Library,Cluster Analysis,我需要将图像分割为RGB值通过特定测试的像素区域。 我可以扫描图像并检查每个像素的值,但是将它们聚集到区域中,然后获取这些区域的坐标(x、y、宽度、高度)的部分会让我完全陷入黑暗:) 这是我目前掌握的代码 from PIL import Image def detectRedRegions(PILImage): image = PILImage.load() width, height = PILImage.size reds = [] h =

我需要将图像分割为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
从PIL导入图像
def检测区域(PILImage):
image=PILImage.load()
宽度,高度=PILImage.size
红色=[]
h=0
当h<高度:
w=0
当w<宽度时:
px=图像[w,h]
如果是红色(px):
reds.append([w,h])
#这就是我无知的地方
w+=1
h+=1
我读了很多关于集群的书,但是我不能完全理解这个主题。任何适合我需要的代码示例都会很好(并且希望能给我启发)

谢谢!

[编辑]

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

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()
它的灵感来自于

[/编辑]

人们可能会使用洪水填充,但我喜欢这样:

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()
从集合导入defaultdict
从PIL导入图像,ImageDraw
def连接的_部件(边缘):
"""
给定由边(即节点对)表示的图,生成其
作为节点集连接的组件。
时间复杂度与边数成线性关系。
"""
邻居=默认DICT(设置)
对于边中的a、b:
邻居[a]。添加(b)
邻居[b]。添加(a)
seen=set()
def组件(节点,邻居=邻居,看见=看见,看见=看见。添加):
不可见=设置([节点])
next_unseen=unseen.pop
虽然看不见:
node=next_unseen()
请参阅(节点)
看不见|=邻居[节点]-看到
屈服点
返回(为邻居中的节点设置(组件(节点)),如果节点不在视图中)
def匹配_像素(图像,测试):
"""
生成像素满足测试要求的所有像素坐标。
"""
宽度,高度=image.size
像素=image.load()
对于x范围内的x(宽度):
对于X范围内的y(高度):
如果测试(像素[x,y]):
产量x,y
def make_边(坐标):
"""
生成所有相邻像素坐标对。
"""
坐标=设置(坐标)
对于坐标中的x,y:
如果(x-1,y-1)在坐标中:
收益率(x,y),(x-1,y-1)
如果(x,y-1)在坐标系中:
收益率(x,y),(x,y-1)
如果(x+1,y-1)在坐标中:
收益率(x,y),(x+1,y-1)
如果(x-1,y)在坐标中:
收益率(x,y),(x-1,y)
收益率(x,y),(x,y)
def边界框(坐标):
"""
返回所有坐标的边界框。
"""
xs,ys=zip(*坐标)
返回最小值(xs)、最小值(ys)、最大值(xs)、最大值(ys)
def不相交区域(图像,测试):
"""
返回所有非连续区域的边界框
谁在测试。
"""
对于每个in-connected_组件(生成_边(匹配_像素(图像,测试)):
屈服边界框(每个)
def是否足够黑(像素):
r、 g,b=像素
返回r<10,g<10,b<10
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
image=image.open('some_image.jpg'))
draw=ImageDraw.draw(图像)
对于不相交区域中的rect(图像,黑色是否足够):
矩形(矩形,轮廓=(255,0,0))
image.show()

这里,两个都满足
的相邻像素对是足够黑的()
被解释为图形中的边。此外,每个像素都被视为其自己的邻居。由于这种重新解释,我们可以对图形使用连接组件算法,这非常容易实现。结果是所有区域的边界框序列,这些区域的像素满足
是否足够黑()

在图像处理中,您需要的是区域标记或连接组件检测。 包中提供了一个实现。 因此,如果您安装了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)

你到底想做什么?一般来说,具有特定颜色的区域不会是正方形,而是任意形状(尽管以某种方式连接),因此用简单的元组(如x、y、宽度、高度)来查找和定义区域将是一个挑战。我知道区域不会是正方形:)我所想的(很抱歉没有弄清楚这一点)是获取区域周围的边界框(应该足够精确,以满足我的需要)
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)