【问题标题】:Use Imagemagick to find areas of similar colour使用 Imagemagick 查找颜色相似的区域
【发布时间】:2011-12-16 20:40:41
【问题描述】:

有没有办法使用 ImageMagick(或类似的东西,任何可行的东西!)来查找图像中相邻像素颜色相似的区域?

提前致谢,

【问题讨论】:

  • 问题的上下文是什么?您当然可以使用图像处理应用程序(Gimp、PS 等)用鼠标选择一个像素并将选择范围扩展到相似颜色的连续区域。但是在应用程序的上下文中,您希望在函数“查找图像区域”之后返回给调用者什么?

标签: python image colors imagemagick pixel


【解决方案1】:

Photoshop、Gimp 和 Plenty 的其他图像处理器将为您完成这项工作。以编程方式,以下是 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()

我从here得到了 tardis.jpg

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 2020-08-01
    • 2019-03-23
    • 2019-03-26
    • 1970-01-01
    • 2012-07-01
    • 2011-12-12
    • 1970-01-01
    • 2010-12-16
    相关资源
    最近更新 更多