%%%% Cloud
%%%% 利用perlin noise生成云彩
clc;
clear all;
close all;
addpath('E:PhotoShop AlgortihmImage ProcessingPS Algorithm');
Image=imread('4.jpg');
Image=double(Image);
[row,col,layer]=size(Image);
baseNoise=rand(row,col);
persistance = 0.9;
totalAmplitude = 0.0;
octaveCount=10;
Cloud_texture=zeros(row,col);
for i=0:octaveCount
amplitude=persistance.^(octaveCount-i);
% amplitude=persistance.^i;
totalAmplitude=totalAmplitude+amplitude;
Cloud_texture=Cloud_texture+Generate_smoothnoise(baseNoise, i)*amplitude;
end
Cloud_texture=Cloud_texture/totalAmplitude;
imshow(Cloud_texture);
function SmoothNoise=Generate_smoothnoise(baseNoise, octave)
SmoothNoise=baseNoise;
[row, col]=size(baseNoise);
samplePeriod=2.^octave;
sampleFrequency=1/samplePeriod;
for i=1:row
sample_i0=floor((i/samplePeriod))*samplePeriod;
sample_i1=mod(sample_i0+samplePeriod,row);
vertical_blend = (i - sample_i0) * sampleFrequency;
for j=1:col
sample_j0 = floor(j / samplePeriod) * samplePeriod;
sample_j1 = mod(sample_j0 + samplePeriod,col);
horizontal_blend = (j - sample_j0) * sampleFrequency;
if(sample_i0<1)
sample_i0=1;
end
if(sample_i1<1)
sample_i1=1;
end
if(sample_j1<1)
sample_j1=1;
end
if(sample_j0<1)
sample_j0=1;
end
% blend the top two corners
top = Cosine_Interpolate(baseNoise(sample_i0,sample_j0),...
baseNoise(sample_i0,sample_j1), horizontal_blend);
% blend the bottom two corners
bottom = Cosine_Interpolate(baseNoise(sample_i1,sample_j0),...
baseNoise(sample_i1,sample_j1), horizontal_blend);
% final blend
SmoothNoise(i,j) = Cosine_Interpolate(top, bottom, vertical_blend);
end
end
function y=Cosine_Interpolate(x1,x2,alpha)
ft = alpha * 3.1415927;
f = (1 - cos(ft)) * .5;
y=x1*(1-f)+x2*f;
原图
效果图