基于Matlab绘制超绚丽的烟花的过程详解
时间:2023-01-27 10:13:18|栏目:C代码|点击: 次
1.使用效果
2.随机点生成
我们要构造一个黑色背景,对其添加高斯噪声,然后依据阈值删掉部分噪声,生成一张随机点图片:
% 构造黑色背景并生成白色杂点 blackPic=uint8(zeros(800,800)); distPic=imnoise(blackPic,'gaussian',0, 0.11); distPic(distPic<254)=0;
3.图像膨胀
我们发现之前构造的点太小了,我们要对其进行形态学膨胀处理:
% 杂点膨胀 se=strel('square',3); distPic=imdilate(distPic,se);
4.特效「风」模拟
就是建立一个循环,不断地将点往右侧复制,并将颜色变暗,代码中第二个参数为拖拽长度,第三个参数为暗化速度:
% 构造风特效 windPic=wind(distPic,180,0.99); % 风特效构造函数 function resultPic=wind(oriPic,len,ratio) oriPic=double(oriPic); for i=1:len tempPic=[zeros(size(oriPic,1),1),oriPic(:,1:(end-1))].*ratio; oriPic(oriPic<tempPic)=tempPic(oriPic<tempPic); end resultPic=uint8(oriPic); end
5.级坐标变换
% 极坐标变换 polarPic=polarTransf(windPic(:,end:-1:1)'); % 级坐标变换构造函数 function resultPic=polarTransf(oriPic) oriPic=double(oriPic); [m,n]=size(oriPic); [t,r]=meshgrid(linspace(-pi,pi,n),1:m); M=2*m; N=2*n; [NN,MM]=meshgrid((1:N)-n-0.5,(1:M)-m-0.5); T=atan2(NN,MM); R=sqrt(MM.^2+NN.^2); resultPic=interp2(t,r,oriPic,T,R,'linear',0); resultPic=uint8(resultPic); end
6.图像模糊及再映射
我们发现烟花图片中心区域方格化严重:
我们很容易想到图像模糊,但是模糊后亮度又不够,因此我们再做一次映射将图片整体亮度提高:
模糊及映射:
% 模糊以减少像素化 polarPic=imgaussfilt(polarPic,1.5); polarPic=uint8(double(polarPic)./double(max(max(polarPic))).*260);
7.图像上色
生成一个圆形的渐变图片,并与白色烟花图片进行一次正交叠底:
% 构造圆形渐变图 matSize=[1600,1600]; point=[800,800]; colorList=[195 53 93 211 102 141 231 179 192 229 182 172 227 178 137 238 191 147 236 195 113]; % 正片叠底 colorMat=cColorMat(matSize,point,colorList); colorMatR=colorMat(:,:,1); colorMatG=colorMat(:,:,2); colorMatB=colorMat(:,:,3); fwPicR=double(colorMatR).*double(polarPic)./255; fwPicG=double(colorMatG).*double(polarPic)./255; fwPicB=double(colorMatB).*double(polarPic)./255; fwPic(:,:,1)=fwPicR; fwPic(:,:,2)=fwPicG; fwPic(:,:,3)=fwPicB; fwPic=uint8(fwPic); imshow(fwPic) %========================================================================== % 图像颜色映射函数 function colorMat=cColorMat(matSize,point,colorList) [xMesh,yMesh]=meshgrid(1:matSize(2),1:matSize(1)); zMesh=sqrt((xMesh-point(2)).^2+(yMesh-point(1)).^2); zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh))); colorFunc=colorFuncFactory(colorList); colorMesh=colorFunc(zMesh); colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1)); colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1)); colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1)); colorMat=uint8(colorMat); end function colorFunc=colorFuncFactory(colorList) x=(0:size(colorList,1)-1)./(size(colorList,1)-1); y1=colorList(:,1);y2=colorList(:,2);y3=colorList(:,3); colorFunc=@(X)[interp1(x,y1,X,'linear')',... interp1(x,y2,X,'linear')',... interp1(x,y3,X,'linear')']; end
其他几个颜色:
colorList2=[25 59 157 24 71 219 38 124 237 93 215 255 168 244 255 243 254 250 246 252 240]; colorList3=[239 250 210 229 164 122 232 150 138 255 164 204 192 58 111 158 10 26 224 168 121];
8.完整代码
function drawFireWorks % 构造黑色背景并生成白色杂点 blackPic=uint8(zeros(800,800)); distPic=imnoise(blackPic,'gaussian',0, 0.11);distPic(distPic<254)=0; % 杂点膨胀 se=strel('square',3); distPic=imdilate(distPic,se); % 构造风特效 windPic=wind(distPic,180,0.99); % 极坐标变换 polarPic=polarTransf(windPic(:,end:-1:1)'); % 模糊以减少像素化 polarPic=imgaussfilt(polarPic,1.5); polarPic=uint8(double(polarPic)./double(max(max(polarPic))).*260); %----------------------------------------------------------------- % 构造圆形渐变图 matSize=[1600,1600]; point=[800,800]; colorList=[195 53 93 211 102 141 231 179 192 229 182 172 227 178 137 238 191 147 236 195 113]; % 正片叠底 colorMat=cColorMat(matSize,point,colorList); colorMatR=colorMat(:,:,1); colorMatG=colorMat(:,:,2); colorMatB=colorMat(:,:,3); fwPicR=double(colorMatR).*double(polarPic)./255; fwPicG=double(colorMatG).*double(polarPic)./255; fwPicB=double(colorMatB).*double(polarPic)./255; fwPic(:,:,1)=fwPicR; fwPic(:,:,2)=fwPicG; fwPic(:,:,3)=fwPicB; fwPic=uint8(fwPic); imshow(fwPic) %========================================================================== % 风特效构造函数 function resultPic=wind(oriPic,len,ratio) oriPic=double(oriPic); for i=1:len tempPic=[zeros(size(oriPic,1),1),oriPic(:,1:(end-1))].*ratio; oriPic(oriPic<tempPic)=tempPic(oriPic<tempPic); end resultPic=uint8(oriPic); end % 极坐标变换构造函数 function resultPic=polarTransf(oriPic) oriPic=double(oriPic); [m,n]=size(oriPic); [t,r]=meshgrid(linspace(-pi,pi,n),1:m); M=2*m; N=2*n; [NN,MM]=meshgrid((1:N)-n-0.5,(1:M)-m-0.5); T=atan2(NN,MM); R=sqrt(MM.^2+NN.^2); resultPic=interp2(t,r,oriPic,T,R,'linear',0); resultPic=uint8(resultPic); end %========================================================================== % 图像颜色映射函数 function colorMat=cColorMat(matSize,point,colorList) [xMesh,yMesh]=meshgrid(1:matSize(2),1:matSize(1)); zMesh=sqrt((xMesh-point(2)).^2+(yMesh-point(1)).^2); zMesh=(zMesh-min(min(zMesh)))./(max(max(zMesh))-min(min(zMesh))); colorFunc=colorFuncFactory(colorList); colorMesh=colorFunc(zMesh); colorMat(:,:,1)=colorMesh(end:-1:1,1:matSize(1)); colorMat(:,:,2)=colorMesh(end:-1:1,matSize(1)+1:2*matSize(1)); colorMat(:,:,3)=colorMesh(end:-1:1,2*matSize(1)+1:3*matSize(1)); colorMat=uint8(colorMat); end function colorFunc=colorFuncFactory(colorList) x=(0:size(colorList,1)-1)./(size(colorList,1)-1); y1=colorList(:,1);y2=colorList(:,2);y3=colorList(:,3); colorFunc=@(X)[interp1(x,y1,X,'linear')',... interp1(x,y2,X,'linear')',... interp1(x,y3,X,'linear')']; end end
上一篇:一篇文章带你入门C语言:数组
栏 目:C代码
下一篇:C++实现LeetCode(187.求重复的DNA序列)
本文地址:http://www.codeinn.net/misctech/224545.html