1
ImageMagick(または同様のもの、動作するもの)を使用して、互いに隣接するピクセルの色が似ている画像の領域を見つける方法はありますか?事前に類似した色の領域を見つけるのにImagemagickを使う
おかげで、他の画像プロセッサの
ImageMagick(または同様のもの、動作するもの)を使用して、互いに隣接するピクセルの色が似ている画像の領域を見つける方法はありますか?事前に類似した色の領域を見つけるのにImagemagickを使う
おかげで、他の画像プロセッサの
の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()
私は質問の文脈でどのようなhere
からtardis.jpgを得ましたか。確かに、画像処理アプリケーション(Gimp、PSなど)を使用して、マウスでピクセルを選択し、同様の色の連続した領域に選択範囲を拡張することができます。しかし、アプリケーションのコンテキストでは、「イメージの領域を見つける」関数の後に、呼び出し元に何が返されると思いますか? – Dave