Libvips: さまざまな色を1つに置き換えることはできますか?

作成日 2018年02月19日  ·  5コメント  ·  ソース: libvips/libvips

こんにちは! 画像から目的の色以外のすべてを削除するImageMagickのコマンドがあります。

convert input.jpg -fill white -fuzz 20% +opaque "#E7482C" output.jpg

libvipsで同じ結果を達成することは可能ですか?

question

全てのコメント5件

こんにちは@ nattfodd 、Rubyでは(たとえば)次のようなことができます:

require 'vips'

image = Vips::Image.new_from_file ARGV[0], access: :sequential

# the colour we search for as an RGB triple 
match = [40, 100, 90]

# find  euclidean distance between each RGB pixels in the input and our match
# colour
distance = Vips::Image.sum(((image - match) ** 2).bandsplit) ** 0.5

# swap pixels more than 50 away for white
image = (distance > 50).ifthenelse([255, 255, 255], image)

image.write_to_file ARGV[1]

これはIMコマンドよりも高速ではありませんが、少なくともメモリの使用量は少なくなります。

または、dE1976を使用することもできます。

require 'vips'

image = Vips::Image.new_from_file ARGV[0], access: :sequential

# the colour we search for as a CIELAB coordinate 
match = [40, -20, 0]

# calculate dE 1976 colour difference
distance = image.dE76(image.new_from_image(match))

# swap pixels more than 50 away for white
image = (distance > 50).ifthenelse([255, 255, 255], image)

image.write_to_file ARGV[1]

繰り返しますが、IMより速くはありませんが、CIELABで作業すると、視覚的に一貫した結果が得られるはずです。

... .dE()はdistance(image、constant)をサポートせず、distance(image、image)のみをサポートするため、奇妙なimage.new_from_image(match)が必要です。 したがって、一定の画像をimageのサイズにし、2つの画像の差を計算します。

どうもありがとう、 @ jcupitt ! 2番目の方法は私が必要なことをするようです!

OK、それがお役に立ててうれしいです!

もちろん、上記のバージョン1ではsqrtをスキップできます。

require 'vips'

image = Vips::Image.new_from_file ARGV[0], access: :sequential

# the colour we search for as an RGB triple 
match = [40, 100, 90]

# find  euclidean distance between each RGB pixels in the input and our match
# colour
distance = Vips::Image.sum(((image - match) ** 2).bandsplit) 

# swap pixels more than 50 away for white
image = (distance > 50 ** 2).ifthenelse([255, 255, 255], image)

image.write_to_file ARGV[1]

これにより、IMよりもわずかに高速になります。

このページは役に立ちましたか?
0 / 5 - 0 評価