2011-03-28 63 views
1

PImage toAverageBlur(PImage sourceImg)平均模糊代碼錯誤

{

PImage newImg = new PImage(sourceImg.width, sourceImg.height); 
float r0,r1,r2,r3,r4,r5,r6,r7,r8; 
float b0,b1,b2,b3,b4,b5,b6,b7,b8; 
float g0,g1,g2,g3,g4,g5,g6,g7,g8; 
float total; 

newImg.loadPixels(); 
for(int i = 1; i < sourceImg.width-1; i++) 
{ 
    for(int j = 1; j < sourceImg.height-1; j++) 
    { 
    int pixelPosition = i*sourceImg.width + j; 



    r1 = red(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]); 
    b1 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]); 
    g1 = green(sourceImg.pixels[((i)*sourceImg.width)+(j-1)]); 
    float sumOne = (r1+b1+g1)/9; 


    r2 = red(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]); 
    b2 = blue(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]); 
    g2 = green(sourceImg.pixels[((i)*sourceImg.width)+(j+1)]); 
    float sumTwo = (r2+b2+g2)/9; 


    r3 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]); 
    b3 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]); 
    g3 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j-1)]); 
    float sumThree = (r3+b3+g3)/9; 


    r4 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]); 
    b4 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]); 
    g4 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j)]); 
    float sumFour = (r4+b4+g4)/9; 


    r5 = red(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]); 
    b5 = blue(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]); 
    g5 = green(sourceImg.pixels[((i-1)*sourceImg.width)+(j+1)]); 
    float sumFive = (r5+b5+g5)/9; 


    r6 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]); 
    b6 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]); 
    g6 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j-1)]); 
    float sumSix = (r6+b6+g6)/9; 


    r7 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]); 
    b7 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]); 
    g7 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j)]); 
    float sumSeven = (r7+b7+g7)/9; 


    r8 = red(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]); 
    b8 = blue(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]); 
    g8 = green(sourceImg.pixels[((i+1)*sourceImg.width)+(j+1)]); 
    float sumEight = (r8+b8+g8)/9; 

    r0 = red(sourceImg.pixels[pixelPosition]); 
    b0 = blue(sourceImg.pixels[pixelPosition]); 
    g0 = green(sourceImg.pixels[pixelPosition]); 
    float sumZero = (r0+b0+g0)/9; 

    total = sumOne+sumTwo+sumThree+sumFour+sumFive+sumSix+sumSeven+sumEight+sumZero; 

    newImg.pixels[pixelPosition] = color(total); 


    } 
} 

newImg.updatePixels(); return newImg; }

+0

請告訴我真正的Bug? – Goz 2011-03-28 17:27:57

回答

1

我認爲這應該是int pixelPosition = j*a.width + i;int pixelPosition = i*a.height + j;

假設你的圖像是100x10,這意味着有1000個元素。如果您正在查看元素(50,5),那麼i = 50,j = 5,原始代碼爲pixelPosition = i*a.width + j;,這意味着pixelPosition = 50 * 100 + 5 = 5005,這是超出範圍的。如果你看看你的代碼,你總是使用(i +/- 1)* width +(j +/- 1),所以對於我低於高度但其他所有部分都不會出現的行,範圍。

下一個問題是平均紅色,綠色和藍色。您應該轉換爲灰色或其他顏色空間變量不合並顏色。

下一個問題是舍入錯誤。當RGB值很小時,每個和將被截斷。使用浮點類型,然後在最後一步中投射它。

下一個問題是在最後一行:

aBlur.pixels[pixelPosition] = a.pixels[pixelPosition] + color(total); 

aBlur似乎像它應該是輸入圖像的灰度模糊,但我不知道該色彩空間的設置,我們假設它是RGB。您複製原始像素並添加模糊的色彩版本?

假設原始像素是(150,150,150),並且獲得的平均值是170.如果函數顏色(170)使像素(170,170,170),那麼原始和平均值之和將是(320,320,320)。所以現在像素值遠不是原始的模糊版本。

我懷疑這條線應該是

aBlur.pixels[pixelPosition] = color(total);