2009-12-15 76 views
1

我使用下面的代碼來創建圖像的動態反映:}簡單的Pixel Bender模糊?

{ 
input image4 src; 
output pixel4 dst; 

parameter float imageheight 
< 
    minValue: 0.0; 
    maxValue : 1000.0; 
    defaultValue :300.0; 

>; 

parameter float fadeheight 
< 
    minValue : 0.0; 
    maxValue: 1000.0; 
    defaultValue: 50.0; 
>; 

parameter float fadealpha 
< 
    minValue : 0.0; 
    maxValue : 1.0; 
    defaultValue : 0.5; 
>; 

void 
evaluatePixel() 
{ 
    float2 coord = outCoord(); 

    if (coord[ 1 ] < imageheight) { 
     dst = sampleNearest(src, coord); 
    } else { 

     float alpha = 1.0 - (coord[ 1 ] - imageheight)/fadeheight; 

     coord[ 1 ] = imageheight - (coord[ 1 ] - imageheight); 

     dst = sampleNearest(src, coord);    
     alpha *= fadealpha; 
     dst.a *= alpha; 
     dst.r *= alpha; 
     dst.g *= alpha; 
     dst.b *= alpha; 

     float2 pos = outCoord(); 
     pixel4 color = sampleNearest(src,pos); 

     color+=0.75*sampleNearest(src, pos+float2(0.5*2, 0))+0.25*sampleNearest(src, pos+float2(2, 0)); 
     color+=0.75*sampleNearest(src, pos-float2(0.5*2, 0))+0.25*sampleNearest(src, pos-float2(2, 0)); 
     color+=0.75*sampleNearest(src, pos+float2(0, 0.5*2))+0.25*sampleNearest(src, pos+float2(0, 2)); 
     color+=0.75*sampleNearest(src, pos-float2(0, 0.5*2))+0.25*sampleNearest(src, pos-float2(0, 2)); 

     dst = color/5.0; 
    }  
} 

它的工作很好,但我真的很想模糊反射的輸出稍微給它光滑的外觀。我有一個谷歌,但所有的結果似乎令人難以置信的複雜。在Pixel Bender中創建模糊效果(類似於Flash內置濾鏡)非常困難嗎?

在這種情況下,我無法應用Flash濾鏡,因此必須在Pixel Bender中完成。

+1

只有具有非常小的半徑的模糊才能在像素彎曲器中實現。對於更大的半徑,像素彎曲器的工作方式(在圖像的每個像素上執行相同的代碼)使其效率非常低,其他算法會更好地處理它。例如Flash中的原生Blur濾鏡。之後爲什麼不應用本地模糊(使用僅覆蓋反射區域的濾鏡矩陣)? – Quasimondo 2010-06-23 21:48:16

回答

2

實際上,這非常簡單,只需要創建一個尺寸相同的新圖像,然後將其中的每個像素設置爲該位置上的像素的平均值以及要模糊的圖像周圍的像素。輸出的像素越多,輸出越模糊。例如,您可以使用上方,下方,左側和右側的像素來模糊每個像素。或者你可以使用像素和所有8個人接觸...如果你想要的話,你也可以給以前的像素值比周圍的更重,爲不同的效果。最好的辦法是嘗試一些東西,直到獲得理想的效果。

順便說一下,第一種模糊被稱爲「平均」。後一種方法可以通過使用高斯分佈來對像素進行加權,如果你使用了許多像素 - 高斯模糊。