天天看點

圖像處理------噪聲之美 - 随機噪聲産生

數學原理:

首先看兩張圖檔,大小均為256 * 256個像素, 第一張是純藍色

圖一:

圖像處理------噪聲之美 - 随機噪聲産生

第二張是加有随機噪聲的藍色

 圖二:

圖像處理------噪聲之美 - 随機噪聲産生

産生随機噪聲的算法簡單的不能再簡單了

假設rgb的r與g顔色分量均為零, 則 blue = 255 * math.random() 随機數的取值範圍在

[0, 1]之間, 程式的核心代碼如下:

for(int row=0; row<256; row++) {

                            for(int col=0; col<256; col++) {

b = (int)(255.0d * math.random());

                                     rgbdata[index]= ((clamp(a) & 0xff) << 24) |

                                                                           ((clamp(r)& 0xff) << 16)  |

                                                                           ((clamp(g)& 0xff) << 8)   |

                                                                           ((clamp(b)& 0xff));

                                     index++;

                            }

}

上面顯然不是我想要的結果,我想要的是下面兩種:

 圖三:

圖像處理------噪聲之美 - 随機噪聲産生

圖四:

圖像處理------噪聲之美 - 随機噪聲産生

對的,隻要我們對上面的算法稍加改進,就可以實作這樣漂亮的噪聲效果

實作第二張圖效果的算法缺點在于,它每次都産生一個新的随機數,假設[0,1] = 255,接着第

二點随機可以能為[0, 2] = 0 第三個點可能随機值為[0, 3] = 125, 毫無規律可言,而我希望是

假設第一點随機[0, 1] = 255則間隔n個點以後再産生下個随機顔色值[0,n+1] =125, 在下一

個點則為[0, 2n +1] = 209…..于是問題産生了, 我們怎麼計算[1, n]的之間的每個像素點的值

哇,這個問題不正是關于圖像放縮的插值問題嘛,一個最簡單的選擇是雙線性插值算法,

有了算法選擇,下面的問題就是我們怎麼計算點值的問題,面臨兩個選擇,一個值照搬雙線

性插值中的計算方法,但是有點不自然,我們想要的是噪聲,顯然線性的計算結果不是最好

的最好的選擇,cos(x)如何,在[0, pi]内是遞減,在[pi,2pi]内是遞增,而且值的範圍在[-1, 1]

之間,而我們的随機數值要在[0, 1]之間于是綜合上述考慮我們有cos(pi + (x-x0/x1-x0)* pi) + 1, 現

在計算出來的值是[0, 1]區間之内 根據插值公式最終有:

y= (y1-y0) * cos(pi + (x-x0/x1-x0) * pi) + 1 + y0

其中[x, y]代表要計算的點,周圍四個采樣點為:[x-n, y-n], [x+n,y-n], [x-n, y+n], [x+n, y+n ]

運用雙線性插值原理即可計算出[1, n]個每個像素點的值。

關鍵代碼實作及解釋:

擷取四個采樣點,及其值,然後使用類似雙線性算法計算出[x,y]的随機數值進而計算出像素值

的程式代碼如下:

// bi-line interpolation algorithm here!!!  

    double getcolor(int x, int y, int m, int colortype)  

    {  

       int x0 = x - (x % m);  

       int x1 = x0 + m;  

       int y0 = y - (y % m);  

       int y1 = y0 + m;  

        double x0y0 = noise(x0,y0, colortype);  

        double x1y0 = noise(x1,y0, colortype);  

        double x0y1 = noise(x0,y1, colortype);  

        double x1y1 = noise(x1,y1, colortype);  

        double xx0 =interpolate(x0, x0y0, x1, x1y0, x);  

        double xx1 = interpolate(x0,x0y1, x1, x1y1, x);  

        double n =interpolate(y0, xx0, y1, xx1, y);  

        return n;  

    }  

根據兩個點計算插入值的公式代碼如下:

return (1.0 + math.cos(math.pi +  (math.pi / (x1-x0)) * (x-x0))) / 2.0   

* (xx1-xx0) + xx0;  

對一張圖像實作随機噪聲值得出像素值計算的代碼如下:

              for(int row=0; row<256; row++) {  

    for(int col=0; col<256; col++) {  

        // set random color value for each pixel  

        r = (int)(255.0d * getcolor(row, col, intervalpixels, 1));  

        g = (int)(255.0d * getcolor(row, col, intervalpixels, 2));  

        b = (int)(255.0d * getcolor(row, col, intervalpixels, 4));  

        rgbdata[index] = ((clamp(a) & 0xff) << 24) |  

                        ((clamp(r) & 0xff) << 16)  |  

                        ((clamp(g) & 0xff) << 8)   |  

                        ((clamp(b) & 0xff));  

        index++;  

}  

完全源代碼如下:

import java.awt.borderlayout;  

import java.awt.dimension;  

import java.awt.graphics;  

import java.awt.graphics2d;  

import java.awt.renderinghints;  

import java.awt.image.bufferedimage;  

import java.util.random;  

import javax.swing.jcomponent;  

import javax.swing.jframe;  

public class randomnoiseimage extends jcomponent {  

    /** 

     *  

     */  

    private static final long serialversionuid = -2236160343614397287l;  

    private bufferedimage image = null;  

    private double[] blue_random;  

    private double[] red_random;  

    private double[] green_random;  

    private int intervalpixels = 40; // default  

    public randomnoiseimage() {  

        super();  

        this.setopaque(false);  

    protected void paintcomponent(graphics g) {  

        graphics2d g2 = (graphics2d)g;  

        g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);  

        g2.drawimage(getimage(), 5, 5, image.getwidth(), image.getheight(), null);  

    private bufferedimage getimage() {  

        if(image == null) {  

            image = new bufferedimage(256, 256, bufferedimage.type_int_argb);  

            int[] rgbdata = new int[256*256];  

            generatenoiseimage(rgbdata);  

            setrgb(image, 0, 0, 256, 256, rgbdata);  

        }  

        return image;  

    private void generatenoiseimage(int[] rgbdata) {  

        int index = 0;  

        int a = 255;  

        int r = 0;  

        int g = 0;  

        int b = 0;  

        int sum = 256 * 256;  

        blue_random = new double[sum];  

        red_random = new double[sum];  

        green_random = new double[sum];  

        random random = new random();  

        for(int i=0; i< sum; i++) {  

            blue_random[i] = random.nextdouble();  

            red_random[i] = random.nextdouble();  

            green_random[i] = random.nextdouble();  

        for(int row=0; row<256; row++) {  

            for(int col=0; col<256; col++) {  

                // set random color value for each pixel  

                r = (int)(255.0d * getcolor(row, col, intervalpixels, 1));  

                g = (int)(255.0d * getcolor(row, col, intervalpixels, 2));  

                b = (int)(255.0d * getcolor(row, col, intervalpixels, 4));  

                rgbdata[index] = ((clamp(a) & 0xff) << 24) |  

                                ((clamp(r) & 0xff) << 16)  |  

                                ((clamp(g) & 0xff) << 8)   |  

                                ((clamp(b) & 0xff));  

                index++;  

            }  

    private int clamp(int rgb) {  

        if(rgb > 255)  

            return 255;  

        if(rgb < 0)  

            return 0;  

        return rgb;  

    // bi-line interpolation algorithm here!!!  

        int x0 = x - (x % m);  

        int x1 = x0 + m;  

        int y0 = y - (y % m);  

        int y1 = y0 + m;  

        double x0y0 = noise(x0, y0, colortype);  

        double x1y0 = noise(x1, y0, colortype);  

        double x0y1 = noise(x0, y1, colortype);  

        double x1y1 = noise(x1, y1, colortype);  

        double xx0 = interpolate(x0, x0y0, x1, x1y0, x);  

        double xx1 = interpolate(x0, x0y1, x1, x1y1, x);  

        double n = interpolate(y0, xx0, y1, xx1, y);  

    // algorithm selection here !!!  

    private double interpolate(double x0, double xx0, double x1, double xx1, double x) {  

        return (1.0 + math.cos(math.pi +   

                  (math.pi / (x1-x0)) * (x-x0))) / 2.0 * (xx1-xx0) + xx0;  

    double noise(int x, int y, int colortype)  

        if(colortype == 1) {  

            if (x < 256 && y < 256)  

                return red_random[y * 256 + x];  

            else  

                return 0.0;  

        } else if(colortype == 2) {  

                return green_random[y * 256 + x];  

        } else {  

                return blue_random[y * 256 + x];  

    public void setrgb( bufferedimage image, int x, int y, int width, int height, int[] pixels ) {  

        int type = image.gettype();  

        if ( type == bufferedimage.type_int_argb || type == bufferedimage.type_int_rgb )  

            image.getraster().setdataelements( x, y, width, height, pixels );  

        else  

            image.setrgb( x, y, width, height, pixels, 0, width );  

    public static void main(string[] args) {  

        jframe frame = new jframe("noise art panel");  

        frame.setdefaultcloseoperation(jframe.exit_on_close);  

        frame.getcontentpane().setlayout(new borderlayout());  

        // display the window.  

        frame.getcontentpane().add(new randomnoiseimage(), borderlayout.center);  

        frame.setpreferredsize(new dimension(280,305));  

        frame.pack();  

        frame.setvisible(true);