imagemagick检测透明区域的坐标

问题描述:

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

感谢所有帮助

更新回答

在随后的几年,我所遇到的一个简单的解决方案,所以我想我会更新它使任何人看到它可以得到的好处最新和最伟大的。

开始一样的,通过提取阿尔法层到其自己的图像,并倒转:

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行输出,每个在e nd,第一行是黑色的,对应于整个形状,最后三行是白色的,对应于三个透明区域。它基本上是你想要的最后三行中每一行的第二个字段。所以,146x8+103+65意味着一个框宽146px,高103px,偏左103px,右上角65px,左上角偏下65px。

如果我画的那些,红色,你可以看到它已经确定:

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


原来的答案

以下可以帮助你到一个答案,但我还没有开发完成 - 人们经常提出问题,然后从不登录aga在这方面还有很多努力......

让我们先从这个输入图像 - 其中白色区域是透明的:

enter image description here

您可以用ImageMagick的从图片中提取alpha通道是这样的:

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

这给这里,白色区域是透明的

enter image description here

好了,一个办法是找白色区域的边界框,你可以用trim做到这一点,它会给你包围白色区域的边框:

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

所以边框宽度为245px,高114px,从左上角偏移+ 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) 

它告诉你该图像是256×256,而且前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 
+0

谢谢!这是一个非常好的帮助:) – user2139779 2014-10-09 20:44:56