【问题标题】:Replace color of region by height imagemagick用高度imagemagick替换区域的颜色
【发布时间】:2019-03-26 23:29:45
【问题描述】:

使用命令“convert”我知道如何替换具有坐标和大小的区域的颜色,但是有没有办法在图像中替换所有区域,例如高度为 40 像素?谢谢

这将是一个输入图像示例,其中有 4 个高度为 40 像素的绿色矩形。

Input.png

如果可能的话,这将是这 4 个绿色矩形被替换为黑色的输出,知道它们的高度但不知道它们的坐标。

Out.png

【问题讨论】:

  • 请问有样图吗?
  • @MarkSetchell 嗨,马克。我添加了输入和输出图像的示例。谢谢
  • 你的形象真的有代表性吗?我的意思是你的颜色真的是纯色的,计算机生成的块吗?你真的使用 PNG 文件而不是讨厌的 JPEG 文件吗?

标签: image-processing imagemagick image-manipulation


【解决方案1】:

我会尽力帮助马克完成任务。我将 ImageMagick 和一些 unix 代码与 Imagemagick 的 Connected Components Labeling (-connected-components) 一起使用,如下所示。

这是图像中所有颜色的简单连接组件结果:

convert in-1.png \
-define connected-components:verbose=true \
-connected-components 4 \
null:

Objects (id: bounding-box centroid area mean-color):
  0: 256x256+0+0 133.6,134.1 50820 srgb(255,255,255)
  1: 86x40+23+30 65.5,49.5 3440 srgb(0,127,70)
  6: 60x40+42+126 71.5,145.5 2400 srgb(0,127,70)
  4: 86x27+22+80 64.5,93.0 2322 srgb(0,38,255)
  5: 86x27+121+121 163.5,134.0 2322 srgb(0,127,70)
  2: 37x40+127+59 145.0,78.5 1480 srgb(0,127,70)
  3: 36x40+177+59 194.5,78.5 1440 srgb(0,127,70)
  7: 41x32+89+186 109.0,201.5 1312 srgb(255,106,0)


请注意,没有一个绿色,即 srgb(0,127,70) 的高度高于 40。全部为 40,一个为 27。因此,为了演示,让所有大于 30 的框。

对于上面的代码,我先选中所有绿色对象,去掉前导空格,提取边界框,也就是字段2,然后把x改成+。

然后我遍历每个边界框并提取 ht、左上角 xx 和 yy 值。我根据 htval=30 测试 ht,如果通过,我用黑色填充绿色。

htval=30
convert in-1.png in-1_result.png
bboxArr=(`convert in-1.png \
-define connected-components:verbose=true \
-connected-components 4 \
null: | grep "srgb(0,127,70)" | sed 's/^[ ]*//' | cut -d\  -f2 | tr "x" "+"`)
num=${#bboxArr[*]}
for ((i=0; i<num; i++)); do
ht=`echo ${bboxArr[$i]} | cut -d+ -f2`
xx=`echo ${bboxArr[$i]} | cut -d+ -f3`
yy=`echo ${bboxArr[$i]} | cut -d+ -f4`
if [ $ht -gt $htval ]; then
convert in-1_result.png -fill black -draw "color $xx,$yy floodfill" -alpha off in-1_result.png
fi
done


注意上面的行

null: | grep "srgb(0,127,70)" | sed 's/^[ ]*//' | cut -d\  -f2 | tr "x" "+"`)


可以替换为

null: | awk '/srgb\(0,127,70\)/ && sub(/x/, "+") {print $2}'

补充:

这是一种更紧凑的方法,使用 awk 进行所有过滤并将输出保存为颜色 x,y 填充。然后只需要一个绘图命令来进行处理。

convert in-1.png in-1_result.png
floodfill_arr=(`convert in-1.png \
-define connected-components:verbose=true \
-connected-components 4 \
null: | awk '/srgb\(0,127,70\)/ && sub(/[x]/, "+") && split($2, arr, "+") {if (arr[4]>30) {print " color " arr[3] "," arr[4] " floodfill"}}'`)
echo "${floofill_arr[*]}"

color 42,126 floodfill color 121,121 floodfill color 127,59 floodfill color 177,59 floodfill

convert in-1_result.png -fill black -draw "${floodfill_arr[*]}" -alpha off in-1_result.png


awk 首先找到所有绿色的行,然后用+ 替换任何x,然后使用字段分隔符+ 将字段$2 拆分为数组(arr)部分,然后测试第4 个arr 字段(ht)是否为大于 30,如果是,则为每个通过测试的边界框打印 -draw 命令。

【讨论】:

  • 优秀。谢谢您的帮助。它似乎工作正常。但是当我尝试使用 1920x5235 和 1.4MB 的原始图像时,当我发送 convert in-1.png \ -define connected-components:verbose=true \ -connected-components 4 \ null: 时收到错误 convert: too many objects @ error/vision.c/Connected ComponentsImage/437
  • 如何解决这个问题?谢谢
  • 如果您的图像是 JPEG,那么颜色不是恒定的,并且您会得到太多区域,因为有很多稍微不同的颜色。发布你的图片,我会看看。可能需要进行一些预处理以使相似的颜色变为相同的颜色。或者可能必须通过添加-define connected-components:area-threshold=XXX 来过滤掉连接组件处理中的小区域
  • 我做了一些类似的颜色。并且去除了几个“噪音”,不再出现物体过多的错误。非常感谢您的帮助和解决方案
【解决方案2】:

您可以使用 ImageMagick“连接组件分析: 查找图像中的所有 blob,如下所示:

convert blobs.png -define connected-components:verbose=true  -connected-components 4 null:

样本输出

Objects (id: bounding-box centroid area mean-color):
  0: 256x256+0+0 133.6,134.1 50820 srgb(255,255,255)
  1: 86x40+23+30 65.5,49.5 3440 srgb(0,127,70)
  6: 60x40+42+126 71.5,145.5 2400 srgb(0,127,70)
  4: 86x27+22+80 64.5,93.0 2322 srgb(0,38,255)
  5: 86x27+121+121 163.5,134.0 2322 srgb(0,127,70)
  2: 37x40+127+59 145.0,78.5 1480 srgb(0,127,70)
  3: 36x40+177+59 194.5,78.5 1440 srgb(0,127,70)
  7: 41x32+89+186 109.0,201.5 1312 srgb(255,106,0)

有一个标题行告诉您所有字段是什么,然后找到每个 blob 一行,忽略第一个非标题行,因为它是白色背景 - 请参阅最后一个字段是 srgb(255,255,255)

查看倒数第二行,它宽 36 像素,高 40 像素,平均颜色为srgb(0,127,70)。我猜这是你想要填充或改变颜色的斑点。

如果您想查找 40 像素高的 blob,请将其传递给 grep,如下所示:

convert blobs.png \
   -define connected-components:verbose=true \
   -connected-components 4 null: | grep "x40+"

你会得到所有 40 高的 blob:

  1: 86x40+23+30 65.5,49.5 3440 srgb(0,127,70)
  6: 60x40+42+126 71.5,145.5 2400 srgb(0,127,70)
  2: 37x40+127+59 145.0,78.5 1480 srgb(0,127,70)
  3: 36x40+177+59 194.5,78.5 1440 srgb(0,127,70)

如果你不知道如何完成它,我会在明天添加更多内容 - 这里已经很晚了。

【讨论】:

  • 感谢您的帮助和时间以及有关如何解决此问题的初步想法。
猜你喜欢
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2012-09-26
  • 2017-03-30
  • 1970-01-01
相关资源
最近更新 更多