matlab exercise program (Berlin noise)

With respect to noise generation, we can use the RAND (256) 256 * This function generates a random noise level of 256, so that noise that we called white noise.

But too random white noise, and sometimes it does not reflect the real noise, such as hills, texture and other less "random" ups and downs.

Therefore, it was developed in Berlin noise, the noise in the terrain of graphics, such as clouds or flame generation method is often used.

Here are the next algorithmic process:

1. First, define the mesh size and the size of the image to be generated.

2. Each grid vertex direction generates a random vector, the vector is red in FIG.

3. traversing each pixel of the image, the pixel to calculate the pixel grid where the four vertices of the vector d, is to the right of Vector blue FIG.

4. Calculate the mesh vertices in a random direction vectors d 3 obtained vector dot product weights to obtain directions.

5. Use smoothing function dx, dy smooth proportionality coefficient sx, sy. To meet the smoothing function f (0) = 0, f (0.5) = 0.5, f Equation (1) = 1, it is preferable to meet the continuous second derivative.

6. The assignment of the current pixel weighting coefficients direction of the smoothing function is obtained.

matlab code is as follows:

Clear All; Close All; CLC; 

n- = 256 ;% noise image size 
CELLSIZE = 10 ;% mesh size, different sizes will produce noise at different scales 

G = RAND ( 2 , n-/ CELLSIZE + 2 , n-/ CELLSIZE + 2 ) - 0.5 ;% each mesh vertex random direction vectors 

IMG = zeros (n-);
 for I = . 1 : n-
     for J = . 1 : n- 

        INDI = I / CELLSIZE + . 1 ;         
        INDJ = J / CELLSIZE + . 1 ; 
        
        floori =  Floor ( indi);
        floorj = Floor (INDJ); 
        
        D00 = [INDI INDJ] - [floori floorj];% calculated current point to the four corners of the current grid point distance 
        D10 = [INDI INDJ] - [floori + . 1 floorj]; 
        D01 = [INDI INDJ ] - [floori floorj + . 1 ]; 
        D11 = [INDI INDJ] - [floori + . 1 floorj + . 1 ]; 
        
        S = SUM (G (:., floori, floorj) * D00 ' );% current grid four corner points of the direction vector right direction for the current point 
        T = SUM (G (:, + floori . 1 , floorj) * D10. ' ); 
        U = SUM (G (:, floori, floorj + . 1 .) * D01 ' );
        V = SUM (G (:, floori + . 1 , floorj + . 1 .) * D11 ' ); 
        
        DX = INDI - floori; 
        Dy = INDJ - floorj; 
        
        SY = . 6 * Dy ^. . 5 - 15 * Dy ^. . 4 + 10 * . Dy ^ . 3 ;% conform F ( 0 ) = 0 , F ( 0.5 ) = 0.5 , F ( 1 ) = equation 1, satisfies the second derivative continuous 
        SX = . 6 * DX ^. . 5 - 15 * DX ^. . 4 +10 * ^ DX. . 3 ;% for fluctuations in the grid described in 
        
        A = S + (TS) * SX; 
        B = U + (Vu) * SX; 

        IMG (I, J) = A + (BA) * SY ;
     End 
End 
imshow (IMG, [])

Grid 25 generated images:

Mesh 10 generates an image:

Finally, the different noise generated grid scale can be superimposed to obtain more kinds of noise.

reference:

https://blog.csdn.net/candycat1992/article/details/50346469

http://www.twinklingstar.cn/2015/2581/classical-perlin-noise/

Guess you like

Origin www.cnblogs.com/tiandsp/p/12222283.html