Loop script, changes colours and save each image as jpeg

5 visualizzazioni (ultimi 30 giorni)
Hello,
I would really be grateful of some help please!
Im playing the script attached - but am trying to do the following.
1) Loop the script such that I can it say 50 times
2) Alter the background colour randomly per iteration of the loop
3) Alter the tree colour randomnly per iteration of the loop
4) Save each image generated as a jpeg.
Thank you!
function Tree(r)
r=4
figure('Position',[50,50,700,700]); %image size
drawBranch([0,0],90,r,r);
axis equal
axis off
fig = gcf;
fig.Color = [1,0.843,0.5]; %Sets background colour
function drawBranch(pt,angle,remainingIterations,r)
width=5*(remainingIterations/r);
len1=5*remainingIterations;
ang1=angle+15;
ang2=angle+7;
ang3=angle-7;
ang4=angle-15;
y1=len1*sind(ang1)+pt(2);
x1=len1*cosd(ang1)+pt(1);
y2=len1*sind(ang2)+pt(2);
x2=len1*cosd(ang2)+pt(1);
y3=len1*sind(ang3)+pt(2);
x3=len1*cosd(ang3)+pt(1);
y4=len1*sind(ang4)+pt(2);
x4=len1*cosd(ang4)+pt(1);
ang22=ang2-5;
ang33=ang3+5;
y22=len1/2*sind(ang22)+y2;
x22=len1/2*cosd(ang22)+x2;
y33=len1/2*sind(ang33)+y3;
x33=len1/2*cosd(ang33)+x3;
c=[0 1-(remainingIterations/r) 1-0.5*(remainingIterations/r)];
p1=plot([pt(1),x1],[pt(2),y1],'LineWidth',width,'Color',c);
hold on
p2=plot([pt(1),x2],[pt(2),y2],'LineWidth',width,'Color',c);
p3=plot([pt(1),x3],[pt(2),y3],'LineWidth',width,'Color',c);
p4=plot([pt(1),x4],[pt(2),y4],'LineWidth',width,'Color',c);
p5=plot([x2,x22],[y2,y22],'LineWidth',width,'Color',c);
p6=plot([x3,x33],[y3,y33],'LineWidth',width,'Color',c);
p1.Color(4)=0.8;
p2.Color(4)=0.8;
p3.Color(4)=0.8;
p4.Color(4)=0.8;
p5.Color(4)=0.8;
p6.Color(4)=0.8;
if remainingIterations-1>0
drawBranch([x1,y1],ang1,remainingIterations-1,r);
drawBranch([x22,y22],ang22,remainingIterations-1,r);
drawBranch([x33,y33],ang33,remainingIterations-1,r);
drawBranch([x4,y4],ang4,remainingIterations-1,r);
end
end
set(gcf, 'InvertHardcopy', 'off');
saveas(fig,'Tree%d.png');
end

Risposte (4)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 3 Nov 2021
Is this what you want to achieve:
r=1:50;
for ii=1:numel(r)
Tree(r(ii))
end
function Tree(r)
figure('Position',[50,50,700,700]); %image size
drawBranch([0,0],90,r,r);
axis equal
axis off
fig = gcf;
fig.Color = [1,0.843,0.5]; %Sets background colour
function drawBranch(pt,angle,remainingIterations,r)
width=5*(remainingIterations/r);
len1=5*remainingIterations;
ang1=angle+15;
ang2=angle+7;
ang3=angle-7;
ang4=angle-15;
y1=len1*sind(ang1)+pt(2);
x1=len1*cosd(ang1)+pt(1);
y2=len1*sind(ang2)+pt(2);
x2=len1*cosd(ang2)+pt(1);
y3=len1*sind(ang3)+pt(2);
x3=len1*cosd(ang3)+pt(1);
y4=len1*sind(ang4)+pt(2);
x4=len1*cosd(ang4)+pt(1);
ang22=ang2-5;
ang33=ang3+5;
y22=len1/2*sind(ang22)+y2;
x22=len1/2*cosd(ang22)+x2;
y33=len1/2*sind(ang33)+y3;
x33=len1/2*cosd(ang33)+x3;
c=[0 1-(remainingIterations/r) 1-0.5*(remainingIterations/r)];
p1=plot([pt(1),x1],[pt(2),y1],'LineWidth',width,'Color',c);
hold on
p2=plot([pt(1),x2],[pt(2),y2],'LineWidth',width,'Color',c);
p3=plot([pt(1),x3],[pt(2),y3],'LineWidth',width,'Color',c);
p4=plot([pt(1),x4],[pt(2),y4],'LineWidth',width,'Color',c);
p5=plot([x2,x22],[y2,y22],'LineWidth',width,'Color',c);
p6=plot([x3,x33],[y3,y33],'LineWidth',width,'Color',c);
p1.Color(4)=0.8;
p2.Color(4)=0.8;
p3.Color(4)=0.8;
p4.Color(4)=0.8;
p5.Color(4)=0.8;
p6.Color(4)=0.8;
if remainingIterations-1>0
drawBranch([x1,y1],ang1,remainingIterations-1,r);
drawBranch([x22,y22],ang22,remainingIterations-1,r);
drawBranch([x33,y33],ang33,remainingIterations-1,r);
drawBranch([x4,y4],ang4,remainingIterations-1,r);
end
end
set(gcf, 'InvertHardcopy', 'off');
saveas(fig,'Tree%d.png');
end

Ben Foster
Ben Foster il 3 Nov 2021
hmm - doesnt run :)
in the original script, r=number of branches of the fractal.
I've swapped your r for k, but still no joy in it running....(also swapped 50 for 10, just to make it quicker if it runs)
k=1:10;
for ii=1:numel(k)
Tree(r(ii))
end
function Tree(k)
  1 Commento
Sulaymon Eshkabilov
Sulaymon Eshkabilov il 4 Nov 2021
Now, you have made an error here. Use this syntax:
r=1:10; % To simulate between 1:10
for ii=1:numel(r)
Tree(r(ii)) % Input argument names MUST match with the function
end
function Tree(r) % Don't make any changes from this point and on
...
...
end

Accedi per commentare.


Ben Foster
Ben Foster il 4 Nov 2021
if I make no changes, this is the error:
Error: File: treem.m Line: 5 Column: 1
Function definitions are not permitted in this context.

Ben Foster
Ben Foster il 4 Nov 2021
Ok, so I am slowly getting there....
I have edited the script as below, to randomly generate the colours of the Fractal Tree and the Background.
If I run this, so it loops 10 times. I get 10 figures drawn - good!
The issue I seem to have, is I am using 2015a version, which I think is the cause of the errors above. However, I can work around this by executingt the m file, via the command line using:
for K = 1 : 10; Tree; end
This runs the script 10 times, and generates 10 matlab figures.
My next challenge is, saving each of the figures generated onto my D drive as JPEGs and PNGs - I need to generate lots and lots, so saving each ones isnt going to be practical!
Help much appreciated, I have tried lots of combos: Saveas, Imwrite etc.
Thank you!
function Tree(r)
r=4
figure('Position',[50,50,700,700]); %image size
drawBranch([0,0],90,r,r);
axis equal
axis off
fig = gcf;
%fig.Color = [1,0.843,0]; %Sets background colour
fig.Color = rand (1,3); %Random background colour
function drawBranch(pt,angle,remainingIterations,r)
width=5*(remainingIterations/r);
len1=5*remainingIterations;
ang1=angle+15;
ang2=angle+7;
ang3=angle-7;
ang4=angle-15;
y1=len1*sind(ang1)+pt(2);
x1=len1*cosd(ang1)+pt(1);
y2=len1*sind(ang2)+pt(2);
x2=len1*cosd(ang2)+pt(1);
y3=len1*sind(ang3)+pt(2);
x3=len1*cosd(ang3)+pt(1);
y4=len1*sind(ang4)+pt(2);
x4=len1*cosd(ang4)+pt(1);
ang22=ang2-5;
ang33=ang3+5;
y22=len1/2*sind(ang22)+y2;
x22=len1/2*cosd(ang22)+x2;
y33=len1/2*sind(ang33)+y3;
x33=len1/2*cosd(ang33)+x3;
%c=[0 1-(remainingIterations/r) 1-0.5*(remainingIterations/r)]; %change first number to alter colours
c=rand (1,3); %Random colours
p1=plot([pt(1),x1],[pt(2),y1],'LineWidth',width,'Color',c);
hold on
p2=plot([pt(1),x2],[pt(2),y2],'LineWidth',width,'Color',c);
p3=plot([pt(1),x3],[pt(2),y3],'LineWidth',width,'Color',c);
p4=plot([pt(1),x4],[pt(2),y4],'LineWidth',width,'Color',c);
p5=plot([x2,x22],[y2,y22],'LineWidth',width,'Color',c);
p6=plot([x3,x33],[y3,y33],'LineWidth',width,'Color',c);
p1.Color(4)=0.8;
p2.Color(4)=0.8;
p3.Color(4)=0.8;
p4.Color(4)=0.8;
p5.Color(4)=0.8;
p6.Color(4)=0.8;
if remainingIterations-1>0
drawBranch([x1,y1],ang1,remainingIterations-1,r);
drawBranch([x22,y22],ang22,remainingIterations-1,r);
drawBranch([x33,y33],ang33,remainingIterations-1,r);
drawBranch([x4,y4],ang4,remainingIterations-1,r);
end
end
%Image Save Setup
%Need help defining this bit!
end

Categorie

Scopri di più su Fractals in Help Center e File Exchange

Prodotti


Release

R2015a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by