gpt4 book ai didi

imagemagick - 我可以使用 imagemagick 来检测边框颜色吗?

转载 作者:行者123 更新时间:2023-12-05 04:07:26 27 4
gpt4 key购买 nike

我必须处理大量图像,并且正在寻找检测边框颜色的方法。例如,拍摄这张图片:

enter image description here

是否可以通过某种方式确定图像具有白色背景?解决方法是提取 1px 的边框并检查提取的边框的颜色是否同样为白色。但我认为 ImageMagick 的 shave 功能不支持这个,对吧?

最佳答案

有很多技巧...

  • 您可以获得左上角(或其他)的像素值,或者
  • 所有 4 个角并取中值,或者
  • 从每一侧切出 1 像素宽的切片,然后选择方差最小的切片。

让我们制作一张 200x100 的测试图像:

convert xc:red xc:lime +append \( xc:blue xc:white +append \) -append -scale 200x100\! image.png

enter image description here

这里有一些获取角点像素的方法:

# Top left pixel
magick image.png -format "%[hex:u.p{0,0}]\n" info:
FF0000

# Top right pixel
magick image.png -crop "1x1+%[fx:w-1]+0" -format "%[hex:u.p{0,0}]\n" info:
00FF00

# Bottom left pixel
magick image.png -crop "1x1+0+%[fx:h-1]+0" -format "%[hex:u.p{0,0}]\n" info:
0000FF

# Bottom right pixel
magick image.png -crop "1x1+%[fx:w-1]+%[fx:h-1]" -format "%[hex:u.p{0,0}]\n" info:
FFFFFF

现在让我们看看我们可以使用更好的示例图像的顶部、底部、左侧和右侧边缘:

convert -size 256x256 gradient:red-yellow image.png

enter image description here

# Top row mean and standard deviation
magick image.png +repage -crop x1\!+0+0 -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.333333,0

标准偏差为零,因此该行中的所有像素都相同。

# Bottom row mean and standard deviation
magick image.png +repage -crop "x1\!+0+%[fx:h-1]" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.666667,0

标准偏差为零,因此该行中的所有像素都相同,但更亮(0.6666 对 0.3333),因为红色和绿色像素都亮起 - 变成黄色。

# Left edge mean and standard deviation
magick image.png +repage -crop "1\!x+0+0" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.5,0.0967909

边缘有一些偏差,因为颜色在变化。

# Right edge mean and standard deviation
magick image.png +repage -crop "1\!x+%[fx:w-1]+0" -format "%[fx:mean],%[fx:standard_deviation]\n" info:
0.5,0.0967909

关于imagemagick - 我可以使用 imagemagick 来检测边框颜色吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48681740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com