使用 Imagemagick 查找相似颜色的区域

发布于 2024-12-12 10:39:04 字数 80 浏览 2 评论 0原文

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

提前致谢,

Is there any way to use ImageMagick (or something similar, anything that will work!) to find areas of an image where the pixels beside each other are a similar color?

Thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-12-19 10:39:04

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()

我从 此处

Photoshop, Gimp and Plenty of other image processors will do this for you. Programmatically, here is some code in python that accomplishes this:

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()

I got the tardis.jpg from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文