1

我使用以下方法分析图像的四个 1px 边框(边缘):

convert IMAGE.jpg +repage -crop x1\!+0+0 -format "%[fx:standard_deviation]\n" info:-
convert IMAGE.jpg +repage -crop x1\!+0+%[fx:h-1] -format "%[fx:standard_deviation]\n" info:-
convert IMAGE.jpg +repage -crop 1\!x+0+0 -format "%[fx:standard_deviation]\n" info:-
convert IMAGE.jpg +repage -crop 1\!x+%[fx:w-1]+0 -format "%[fx:standard_deviation]\n" info:-

这给了我所有边缘(顶部、底部、左侧、右侧)的标准偏差,所以如果它们中的大多数是 0,我知道可能有一个主要的背景/边框颜色。

这很好,但优化也很差,因为 convert 必须读取文件 4 次。我怎样才能让它在一次执行中打印所有这些?

此外,有没有办法知道这些边缘的“平均”颜色?例如,如果图像是 100x100 并且顶部边框的 100x1 区域有 50 个黑色像素和 50 个白色像素,我会在该边缘的标准偏差旁边得到“128.128.128,255”。

谢谢!

4

1 回答 1

2

我有点着急,没有检查这个,但你可以将图像保存在 MPR - Magick Persistant Register中,它是一个命名的内存块,并像这样回忆它:

magick start.png -write MPR:orig -delete 0 -format "%[fx:standard_deviation]\n" \
   \( MPR:orig -crop x1\!+0+0          -write info: -delete 0 \) \
   \( MPR:orig -crop x1\!+0+%[fx:h-1]  -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+0+0          -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+%[fx:w-1]+0  -write info: -delete 0 \) null:

这也可以做你的平均计算:

magick start.png -write MPR:orig -delete 0 \
   \( MPR:orig -crop x1\!+0+0          -format "%[fx:standard_deviation], " -write info: -resize 1x1\! -format "%[mean]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop x1\!+0+%[fx:h-1]  -format "%[fx:standard_deviation], " -write info: -resize 1x1\! -format "%[mean]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+0+0          -format "%[fx:standard_deviation], " -write info: -resize 1x1\! -format "%[mean]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+%[fx:w-1]+0  -format "%[fx:standard_deviation], " -write info: -resize 1x1\! -format "%[mean]\n" -write info: -delete 0 \) null:

样本输出

0, 65535
0, 0
0.290373, 32767.5
0.290373, 32767.5

或者,最终答案:

magick start.png -write MPR:orig -delete 0 \
   \( MPR:orig -crop x1\!+0+0          -format "%[fx:standard_deviation], %[fx:mean.r], %[fx:mean.g],%[fx:mean.b]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop x1\!+0+%[fx:h-1]  -format "%[fx:standard_deviation], %[fx:mean.r], %[fx:mean.g],%[fx:mean.b]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+0+0          -format "%[fx:standard_deviation], %[fx:mean.r], %[fx:mean.g],%[fx:mean.b]\n" -write info: -delete 0 \) \
   \( MPR:orig -crop 1\!x+%[fx:w-1]+0  -format "%[fx:standard_deviation], %[fx:mean.r], %[fx:mean.g],%[fx:mean.b]\n" -write info: -delete 0 \) null:

样本输出

0, 1, 0,0
0, 0, 0,0
0.290373, 0.5, 0,0
0.290373, 0.5, 0,0
于 2019-02-21T19:25:21.697 回答