gpt4 book ai didi

ruby-on-rails - imagemagick 检测透明区域的坐标

转载 作者:行者123 更新时间:2023-12-02 08:31:35 26 4
gpt4 key购买 nike

我有一个带有透明区域的 PNG 图像,正方形/矩形区域包含透明度。我想知道是否有某种方法可以知道图像中这些透明区域的顶部、左侧、宽度、高度。

感谢您的帮助

最佳答案

更新的答案

在这几年里,我遇到了一个更简单的解决方案,所以我想我会更新它,以便其他看到它的人都能从最新最好的产品中受益。

同样开始,通过将 alpha 层提取到它自己的图像并将其反转:

convert start.png -alpha extract -negate intermediate.png

enter image description here

现在对其执行“连通分量分析”:

convert start.png -alpha extract -negate           \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-connected-components 8 -auto-level result.png

Objects (id: bounding-box centroid area mean-color):
0: 256x256+0+0 128.7,130.4 62740 srgb(0,0,0)
3: 146x8+103+65 175.5,68.5 1168 srgb(255,255,255)
2: 9x93+29+42 33.0,88.0 837 srgb(255,255,255)
1: 113x7+4+21 60.0,24.0 791 srgb(255,255,255)

你会看到有一个标题行和4行输出,每行最后都有一个颜色,第一行是黑色,对应整个形状,最后三行是白色,对应三个透明区域。它基本上是您想要的最后三行中每一行的第二个字段。因此,146x8+103+65 表示一个宽 146 像素、高 103 像素的框,在左上角右侧偏移 103 像素,从左上角向下偏移 65 像素。

如果我用红色画那些,你可以看到它识别的是什么:

convert result.png -stroke red -fill none -strokewidth 1 \
-draw "rectangle 103,65 249,73" \
-draw "rectangle 29,42 38,135" \
-draw "rectangle 4,21 117,28" result.png

enter image description here


原始答案

以下内容可能会帮助您找到答案,但我并没有从头到尾开发它——人们经常提出问题,然后再也没有登录,这涉及到相当多的努力...

让我们从这个输入图像开始——白色区域是透明的:

enter image description here

您可以像这样使用 ImageMagick 从图像中提取 alpha channel :

convert input.png -alpha extract -negate alpha.png

这给出了这个,其中白色区域是透明的

enter image description here

好的,一种方法是找到白色区域的边界框,您可以使用 trim 执行此操作,它会为您提供包围白色区域的边界框:

convert input.png -alpha extract -format "%@" info:
245x114+4+21

因此边界框宽 245 像素,高 114 像素,从左上角的偏移量 +4+21 开始。我可以在图像上绘制以显示它:

enter image description here

所以,这是一个开始。

此外,您还可以让 ImageMagick 以文本格式枚举像素,因此您可以运行此命令

convert input.png -alpha extract -negate txt: | more
# ImageMagick pixel enumeration: 256,256,255,gray
0,0: (0,0,0) #000000 gray(0)
1,0: (0,0,0) #000000 gray(0)
2,0: (0,0,0) #000000 gray(0)

它告诉您图像是 256x256 并且前 3 个像素都是黑色的。如果你想要白色的(即透明的),你可以这样做:

convert input.png -alpha extract -negate txt: | grep FFFFFF | more
4,21: (255,255,255) #FFFFFF gray(255)
5,21: (255,255,255) #FFFFFF gray(255)
6,21: (255,255,255) #FFFFFF gray(255)
7,21: (255,255,255) #FFFFFF gray(255)

这告诉您像素 4,21 是透明区域的左上角 - 我很高兴它与上述边界框方法的输出匹配:-)

因此,您可以轻松获得所有透明像素的列表。可以开发这种方法,或者在 Ruby (RMagick) 中编写类似的代码来查找连续的黑色区域——但这目前超出了这个答案的范围——因为我不是 Ruby 程序员:-)

好的,今天下午我学习了一些 Ruby,请不要笑,这是我的第一个 Ruby 程序。它可能非常丑陋,更像 Perl 或 C(我的首选语言),但它可以工作并找到矩形透明区域。

#!/usr/bin/ruby

require 'RMagick'
include Magick
infile=ARGV[0]
img = ImageList.new(infile)
w=img.columns
h=img.rows
#Extract alpha channel into pixel array
px=img.export_pixels(0,0,w,h,"A")

for row in 0..h-1
for col in 0..w-1
thispx=px[w*row+col]
if thispx<32768 then
a=row
b=col
# Find extent (c) of rectangle towards right
for r in col..w-1
thispx=px[w*row+r]
if thispx<32768
c=r
else
break
end
end
# Find extent (d) of rectangle towards bottom
for s in row..h-1
thispx=px[w*s+col]
if thispx<32768
d=s
else
break
end
end
# Blank this rectangle as we have located it
for r in row..d
for s in col..c
px[w*r+s]=65535
end
end

# Tell caller about this rectangle
printf "%d,%d %d,%d\n",a,b,d,c
end
end
end

像这样运行它:

bounds.rb input.png

关于ruby-on-rails - imagemagick 检测透明区域的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26244191/

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