元胞自动机 森林感染模拟 interesting_demo 美赛模拟训练模拟纽约森林灰树感染,包含地图图像处理

% simulate forest infection with cellular automata
% [email protected]
% January 13 2018 
% close all;%close windows
[v R C]=detectree();
n = min(R,C);%matrix size+++++++++++++++++++++++++
iteration=300;%number of iteration++++++++++++++++++++++++
Pinfect = 5e-3;%ramdomly be affected++++++++++++++++++++++++ 
% Plight = 0; 

treeLife=100;%++++++++++++++++++++++++
Pgrowth = 2e-3;%++++++++++++++++++++++++
UL = [n 1:n-1]; 
DR = [2:n 1];
% veg=zeros(n,n)+2;
veg=zeros(n,n);
%Healing tree number
%Healtree=zeros(n);
%Healing Rate
PHealrate=0.001;
% randomlly{begin}=======
% probb=rand(n,n);
% veg(find(probb<0.5))=2;
v=v([1:n],[1:n]);
veg(find(v==0))=2;
% veg=v(1:n,1:n);
% v=veg;
% randomlly{end}=========
%valuable for recording
NumInfect=[];
NumNormal=[];
% veg((n./2-10):(n./2+10),(n./2-10):(n./2+10))=1;
B=zeros(n,n);

deathCounter=zeros(n,n)+treeLife;%trees' ability to face against the infect
ifStarCount=zeros(n,n);
% veg=zeros(n,n)+2;

% veg(n./2 , n./2)=1;
imh = image(cat(3,veg,veg,veg));

xx=1:n;
yy=1:n;
%=====
% imi = image(cat(3,deathCounter,deathCounter,deathCounter));
% veg = empty=0 burning=1 green=2
for i=1:iteration
    %nearby infected?
    sum =            (veg(UL,:)==1) + ...
        (veg(:,UL)==1)     +      (veg(:,DR)==1) + ...
                     (veg(DR,:)==1);
%==============================================judge if near by infected
%     veg = 2*(veg==2) - ...
%           ( (veg==2) & (sum>0 | (rand(n,n)<Plight)) ) + ...
%           2*((veg==0) & rand(n,n)<Pgrowth) ;
 ifStarCount(find(veg==1))=1;
veg = 2*(veg==2) - ...
         ( (veg==2) & (sum>0 | (rand(n,n)<Pinfect))  ) + ...
          2*((veg==0) & rand(n,n)<Pgrowth) ;
%=======================================HEALING START===========

if i>100      
 veg=((veg==1)&(rand(n)<PHealrate) | veg==2 ).*2;
end     
 veg;

%=======================================HEALING END===========

%=======================================DEATH COUNTER=========== 
%When tree is infected
ifStarCount(veg==1)=1;

deathCounter(find(ifStarCount))=deathCounter(find(ifStarCount))-1;

% deathCounter

ifStarCount(find(deathCounter==0))=0;
deathCounter(find(deathCounter==0))=treeLife;
veg(find(ifStarCount==1))=1;%=====started
%=========================remove the death tree=================
veg(find(deathCounter==0))=0;


% %=======================================show updated forest=========== 
    B(deathCounter==100)=0;
    B(deathCounter<100)=0.1;
    B(deathCounter<75)=0.4;
    B(deathCounter<25)=0.6;
    B(deathCounter==0)=0.7;
    set(imh, 'cdata', cat(3,(veg==1)-B,(veg==1)-B+(veg==2),zeros(n)));
    drawnow;
    %==========================set Graph information==================
    xlabel('latitude');
    ylabel('longitude');
    grid on;
    title('Tree infection graph')
    
%     %=======================================show infextion=========== 
%     set(imi, 'cdata', cat(3,(deathCounter<10),(10<deathCounter<19),(deathCounter~=treeLife)))
%     drawnow
%     %==========================set Graph information==================
%     xlabel('latitude');
%     ylabel('longitude');
%     grid on;
%     title('Tree infection graph')
% deathCounter(find(ifStarCount==0))=0;
NumInfect=[NumInfect length(find(ifStarCount==1))];
NumNormal=[NumNormal length(find(veg==2))]
end
xxx=(1:iteration);
% plot(xxx,NumNormal./(n.^2).*100,'g');
plot(xxx,NumInfect./(n.^2).*100,'r','LineWidth',2);
hold on;
plot(xxx,NumNormal./(n.^2).*100,'g');
grid on;
title('Proportion different type of trees','LineWidth',2);
xlabel('Year');
ylabel('Proportion/%');
% deathCounter(find(ifStarCount==0))=0;
% imagesc(xx,yy,deathCounter);

function [block R C] = detectblock()
im=imread('sample.jpg');
[R C]= size(im);
block=zeros(size(im))+255;
block(find((im<110)&(92<im)))=0;
% tree(find(60<im))=0;

imtool(block);
end

 
 

function[tree R C]=detectree();

im=imread('sample.jpg');

[R C]= size(im);

tree=zeros(size(im))+255;

tree(find((im<80)&(60<im)))=0;

% tree(find(60<im))=0;

 

imtool(tree);

end




猜你喜欢

转载自blog.csdn.net/weixin_39257042/article/details/80265260