2014-10-07 94 views
2

我有一個透明區域,包含透明度的正方形/矩形區域的PNG圖像。 我想知道是否有某種方式可以知道圖像中這些透明區域的頂部,左側,寬度和高度。imagemagick檢測透明區域的座標

感謝所有幫助

回答

5

更新回答

在隨後的幾年,我所遇到的一個簡單的解決方案,所以我想我會更新它使任何人看到它可以得到的好處最新和最偉大的。

開始一樣的,通過提取阿爾法層到其自己的圖像,並倒轉:

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