Используйте Imagemagick, чтобы найти области похожего цвета - PullRequest
1 голос
/ 28 октября 2011

Есть ли способ использовать ImageMagick (или что-то подобное, все, что будет работать!), Чтобы найти области изображения, где пиксели рядом друг с другом имеют одинаковый цвет?

Заранее спасибо,

1 Ответ

1 голос
/ 28 октября 2011

Photoshop, Gimp и множество других процессоров изображений сделают это за вас. Программно, вот код на python, который выполняет это:

from PIL import Image, ImageDraw

def inImage(im, px):
    x,y = px
    return x < im.size[0] and y < im.size[1] and x > 0 and y > 0

def meetsThreshold(im, px, st, threshold):
    color = im.getpixel(px)
    similar = im.getpixel(st)
    for cPortion, sPortion in zip(color,similar):
        if abs(cPortion - sPortion) > threshold:
            return False
    return True

def floodFill(im, fillaroundme, fillWith, meetsThresholdFunction):
    imflooded = im.copy()
    imflooded.putpixel(fillaroundme, fillwith)
    processed = []
    toProcess = [fillaroundme]
    while len(toProcess) > 0:
        edge = toProcess.pop()
        processed.append(edge)
        x, y = edge
        for checkMe in ((x+1, y), (x-1, y), (x, y+1), (x, y-1)):
            if inImage(im, checkMe) and meetsThresholdFunction(im, edge, checkMe):
                imflooded.putpixel(checkMe, fillWith)            
                if checkMe not in toProcess and checkMe not in processed:
                    toProcess.append(checkMe)
        processed.append(edge)
    return imflooded


im = Image.open(r"tardis.jpg")
filled = floodFill(im, (120, 220), (255, 0, 0), lambda im, px, st: meetsThreshold(im, px, st, 10))

filled.show()

Я получил tardis.jpg от здесь

...