2016-10-17 50 views
0

我正在使用透明顏色去除背景顏色的腳本。ImageMagic php中的模糊洪水填充 - 製作透明圖像

劇本是這樣的,並給出了確定的結果,區分紫紅色。

 $val = 65535/40; 
     //divide by fuzz dilution, 1 is none 
     $val = floatval($val/0.9); 
     //create white border 
     $image->borderImage ("rgb(255,255,255)" , 1 , 1); 
     //make all white fill fuchsia 
     $image->floodFillPaintImage ("rgb(255, 0, 255)" ,$val*3, "rgb(255,255,255)", 0 , 0, false); 
     //make fuchsia transparent 
     $image->paintTransparentImage("rgb(255,0,255)", 0.0, 0.5); 
     //remove border 1px that was added above 
     $image->shaveImage (1 , 1); 

但是,它留下了圖像周圍顏色的痕跡。這裏有一個例子,當我試圖去除白色背景的電話周圍的邊框 - 你可以清楚地看到邊緣的白色痕跡。

enter image description here

的問題是 - 做一個洪水填補像素0,0時,背景色被着色錯了,我需要一個「模糊」的油漆桶填充功能。 Imagemagic爲floodFillPaintImage提供了「模糊」算法,但「模糊」部分的參數僅用作像素的選擇,而不是模糊着色。

例如,我有一個100%的白色 - 算法正確選擇完美的白色背景,並填充一個新的完美紫紅色圖像。當你設置一個「模糊」參數,算法正確選擇 80%的白色像素(例如),但它再次以100%紫紅色着色。這就是出現丑角的問題。

ImageMagic支持像「真」模糊氾濫填充和「真實」模糊paintTransparentImage?或者有人對如何解決這個問題有更好的想法?

回答

3

我有一點點去了。我不能說我對此100%滿意,但我已經解釋了我正在做的事情,並且做了很小的步驟,所以你可以玩弄每個步驟並擺弄數字。爲了簡單起見,我只是在命令行上做了它。如果你能達到你想要的水平,它可以全部簡化並加速。

#!/bin/bash 

# Get size of original 
sz=$(convert -format "%wx%h" phone.png info:) 

# Floodfill background area with transparency 
convert phone.png -fuzz 5% -fill none -draw 'color 0,0 floodfill' ObjectOnTransparent.png 

# Extract alpha channel 
convert ObjectOnTransparent.png -alpha extract Alpha.png 

# Extract edges of alpha channel - experiment with thickness 
convert Alpha.png -edge 1 AlphaEdges.png 

# Get difference from background for all pixels 
convert phone.png \(+clone -fill white -colorize 100% \) -compose difference -composite Diff.png 

# Multiply edges with difference, so only edge pixels will have a chance of getting through to final mask 
convert AlphaEdges.png Diff.png -compose multiply -composite EdgexDiff.png 

# Extend Alpha by differences at edges 
convert Alpha.png EdgexDiff.png -compose add -composite ReEdgedAlpha.png 

# Apply new alpha to original image 
convert phone.png \(ReEdgedAlpha.png -colorspace gray \) -compose copyopacity -composite RemaskedPhone.png 

# Splat RemaskedPhone over red background 
convert -size $sz xc:red RemaskedPhone.png -composite Result.png 

ObjectOnTransparent.png

enter image description here

Alpha.png

enter image description here

AlphaEdges.png

enter image description here

Diff.png

enter image description here

EdgexDiff.png

enter image description here

ReEdged Α。PNG

​​

Result.png

enter image description here

+0

非常感謝你的幫助,它確實幫助我得到的問題開始。然而,EdgexDiff.png有時會產生邊框,這些邊框對於某些圖像來說太薄。這絕對比我原始的方法好,但仍然不是很好 – user151496