imagemagick:根据r / g / b条件有选择地填充像素?

问题描述 投票:1回答:2

有没有办法根据使用像素的R或G或B值的算术表达式有选择地替换颜色?

示例:假设我有一个RGB图像“foobar.png”,我想将红色通道<100的所有像素更改为白色。

在伪代码中:

for (all pixels in image) { if (pixel.red < 100) then pixel = 0xffffff; }

有没有办法用ImageMagick解决这个问题?

filter colors imagemagick rgb pixel
2个回答
2
投票

你可以使用FX expressions

说我创建一个测试图像......

convert -size 400x400 gradient:red-blue input.png

input

用红色值<100替换任何像素(假设最大值是8位量子点255),可以表示为..

convert input.png -fx 'r < (100/255) ? #FFFFFF : u' output.png

output

更新

FX很强大,但很慢。它也会画出粗糙的边缘。另一种方法是分离RED通道,将其转换为掩码,并在其他通道上进行复合。这可以使用-evaluate-sequance MAX完成,或者设置alpha通道并在白色背景上进行构图。

创建示例输入图像。

convert -size 400x400 xc:white \
    -sparse-color shepards '0 0 red 400 0 blue 400 400 green 0 400 yellow ' \
    input.png

input

convert -size 400x400 xc:white \
    \( input.png \
        \( +clone  -separate -delete 1,2 \
           -negate -level 39% -negate \
        \) \
        -compose CopyOpacity -composite \
    \) -compose Atop -composite  output.png

output


3
投票

这与emcconville的出色解决方案类似但略有不同。这是Unix语法。

#1 compute the 100 out of 255 threshold in percent
#2 read the input
#3 clone the input and make it completely white
#4 clone the input and separate the red channel, threshold and negate so that the white part represents values less than 100 out of 255
#5 use the threshold image as a mask in a composite to select between the original and the white images
#6 write the output

thresh=`convert xc: -format "%[fx:100*100/255]" info:`
convert image.png \
\( -clone 0 -fill white -colorize 100 \) \
\( -clone 0 -channel r -separate +channel -threshold $thresh% -negate \) \
-compose over -composite \
result.png

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.