【Matlab】随机游走产生图像效果

随机游走类似布朗运动,就是随机的向各个方向走吧。产生的图像实在漂亮,所以还是贴出分享。

clear all;
close all;
clc;

n=100000;        
x= 0;
y= 0;            
pixel=zeros(n,2); 
neighbour=[-1 -1;-1 0;-1 1;0 -1;0 1;1 -1;1 0;1 1];  
for i=1:n
    r=floor(1+8*rand());    
    y=y+neighbour(r,1);     
    x=x+neighbour(r,2);     
    pix(i,:)=[y x];         
end

miny=min(pixel(:,1));         
minx=min(pixel(:,2));         

pix(:,1)=pixel(:,1)-miny+1;   
pix(:,2)=pixel(:,2)-minx+1;

maxy=max(pixel(:,1));         
maxx=max(pixel(:,2));

img=zeros(maxy,maxx);       
for i=1:n                  
    img(pixel(i,1),pix(i,2))=1;
end
imshow(img)

输出结果:


关于Image Engineering & Computer Vision的更多讨论与交流,敬请关注本博和新浪微博songzi_tea.

禁止转载,谢谢!!!


原文地址:https://www.cnblogs.com/fuhaots2009/p/3481752.html