Hello,
I am working with a Matlab code where I want to run a loop. In each loop, I will do some processing and then show an image based on the processed data. When I go to the next loop, I want the previously stored image to be closed and generate a new image based on the latest data.
A sum up from my code is this :
for ww= 0.4:0.2:2.6
**********************image processing code*********************************
t1=1-ww*(dark_channel1/A); % part that connect ww with the image processing code
**********************image processing code*********************************
out = pic;
figure;
imshow (out)
imwrite(out,sprintf('%d.jpg',ww));
End;
What I expect is many different images depend on the "ww", but the output that I take looks like in each iteration each image sticks to the previous one.
I think that somehow I can’t separate the output (image), and after each iteration I don't reset my loop. As a result I display a new image but stack on the previous. It is like in each iteration the new image sticks to the previous.
Any suggestions??
Thank you in advance

 Réponse acceptée

ANKUR KUMAR
ANKUR KUMAR le 17 Mar 2021
for i=1:5
imwrite(randi(255,250,205)/255,sprintf('Image_%d.png',i))
end

10 commentaires

Tsitsos Dimitris
Tsitsos Dimitris le 17 Mar 2021
Dear Ankur,
Thank you for the answer.
But what do I achieve with this loop?
The problem is that I can't take the photos that I want but as I have already mentioned the output looks like in each iteration each image sticks to the previous one.
Should I replace something with your lines of code?
Thank you in advance
ANKUR KUMAR
ANKUR KUMAR le 17 Mar 2021
Modifié(e) : ANKUR KUMAR le 17 Mar 2021
Couldn't understand properly the question.
Could you please attach the complete code (the sample you provided contains 1 for loop and 2 end), and sufficient variables.
Tsitsos Dimitris
Tsitsos Dimitris le 17 Mar 2021
Modifié(e) : Rik le 18 Mar 2021
sure!
for ww= 0.4:0.2:2.6
for itr=1:5
under_50=0;
for i=1:h
for j=1:w
dark_I(i,j)=min(img(i,j,:));
end
end
for i=1:h
for j=1:w
if(dark_I(i,j)<50)
under_50=under_50+1;
end
end
end
total=size(img,1)*size(img,2)*size(img,3);
percent=under_50/total;
if(percent<m)
minV=255;
maxV=0;
countlabel=0;
img=double(img);
dark_I1=dark_I;
kh=floor(h/degree);
kw=floor(w/degree);
lh=mod(h,degree);
lw=mod(w,degree);
for op1=1:degree:(kh-1)*degree+1
for op2=1:degree:(kw-1)*degree+1
darkI1_1=double(dark_I1(op1:op1+degree-1,op2:op2+degree-1));
label=min(min(darkI1_1));
for oop1=1:degree
for oop2=1:degree
darkI1_1(oop1,oop2)=label;
end
end
countlabel=countlabel+label;
if label<minV
minV=label;
end
if label>maxV
maxV=label;
end
%splice
if(op2==1)
I_linkcol=darkI1_1;
else
I_linkcol=cat(2,I_linkcol,darkI1_1);
end
end
if(op1==1)
I_linkrow=I_linkcol;
else
I_linkrow=cat(1,I_linkrow,I_linkcol);
end
end
I_row=I_linkrow;
% splice
if(lh==0&&lw==0)
I_row=uint8(I_row);
end
if(lh==0&&lw~=0)
rightlink=double(dark_I(1:h,w-lw+1:w));
I_row=cat(2,I_row,rightlink);
I_row=uint8(I_row);
end
if(lh~=0&&lw==0)
downlink=double(dark_I(h-lh+1:h,1:w));
I_row=cat(1,I_row,downlink);
I_row=uint8(I_row);
end
if(lh~=0&&lw~=0)
rightlink=double(dark_I(1:h-lh,w-lw+1:w));
downlink=double(dark_I(h-lh+1:h,1:w));
I_row=cat(2,I_row,rightlink);
I_row=cat(1,I_row,downlink);
I_row=uint8(I_row);
end
% =======================================
dark_channel=double(I_row);
for opp1=1:degree:(kh-1)*degree+1
for opp2=1:degree:(kw-1)*degree+1
dark_channel1=double(dark_channel(opp1:opp1+degree-1,opp2:opp2+degree-1));
% numa=klabels(opp1,opp2);
% A=Anum(numa,1);
A=150;
t1=1-ww*(dark_channel1/A);
%splice
if(opp2==1)
Tlink=t1;
else
Tlink=cat(2,Tlink,t1);
end
end
if(opp1==1)
t=Tlink;
else
t=cat(1,t,Tlink);
end
end
%optimize t
for opp11=1:degree:(kh-1)*degree+1
for opp22=1:degree:(kw-1)*degree+1
tt=double(t(opp11:opp11+degree-1,opp22:opp22+degree-1));
dark_II=double(dark_I(opp11:opp11+degree-1,opp22:opp22+degree-1));
I_rowR=double(I_row(opp11:opp11+degree-1,opp22:opp22+degree-1));
T=tt-0.001*(dark_II-I_rowR).*tt;
if(opp22==1)
T1=T;
else
T1=cat(2,T1,T);
end
end
if(opp11==1)
ttt=T1;
else
ttt=cat(1,ttt,T1);
end
end
LastT=max(ttt,t0);
[h1,w1]=size(LastT);
new1=img(:,:,1);
new2=img(:,:,2);
new3=img(:,:,3);
I_1(:,:,1)=double(new1(1:h1,1:w1));
I_1(:,:,2)=double(new2(1:h1,1:w1));
I_1(:,:,3)=double(new3(1:h1,1:w1));
J1(:,:,1) = uint8((I_1(:,:,1) - (1-LastT)*A)./LastT);
J1(:,:,2) = uint8((I_1(:,:,2) - (1-LastT)*A)./LastT);
J1(:,:,3) = uint8((I_1(:,:,3) - (1-LastT)*A)./LastT);
if(lh==0&&lw==0)
J=J1;
end
if(lh==0&&lw~=0)
right=double(img(1:h,w-lw+1:w,:));
J=cat(2,J1,right);
J=uint8(J);
end
if(lh~=0&&lw==0)
down=double(img(h-lh+1:h,1:w,:));
J=cat(1,J1,down);
J=uint8(J);
end
if(lh~=0&&lw~=0)
right=double(img(1:h-lh,w-lw+1:w,:));
down=double(img(h-lh+1:h,1:w,:));
I_row=cat(2,J1,right);
J=cat(1,I_row,down);
J=uint8(J);
end
else
J=img;
end
pic=J;
img=pic;
end
out = pic;
figure;
imshow (out)
imwrite(out,sprintf('%d.jpg',ww));
end
Unrecognized function or variable 'h'.
I think that THE MAIN problem is that the loop does not reset in each iteration and the photos look like they are stick to the previous one.
For any questions please let me know!
I fixed the allignment of your code and removed all extra semicolons (you should also note that if is not a function, so all those parentheses are unnecessary).
After that I attempted to run your code. Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue.
On your duplicate question I wrote this:
Apart from the fact that you open a new figure every iteration, your name generation is also odd. ww is a decimal value, but you tell sprintf to treat it like an integer.
imwrite(out,sprintf('%.1f.jpg',ww));
I would suggest using imshow only in the first iteration, and after that replace the CData property of the image object. That will be much faster.
h_im=[];
for n=1:10
IM=uint8(randi(255,512,512));
if isempty(h_im)
h_im=imshow(IM);
else
h_im.CData=IM;
drawnow % force graphics update
end
end
Tsitsos Dimitris
Tsitsos Dimitris le 18 Mar 2021
Modifié(e) : Tsitsos Dimitris le 18 Mar 2021
I don't really get how I can add to my code your part. Sorry if I my question in silly but I am kinda new in matlab world.
Rik
Rik le 18 Mar 2021
Before we start optimizing, we should get your code to work in the first place.
Did you read the Wikipedia link with an explanation? The point is write code that will run and reproduce your issue. The code I posted is an example: you don't need any data, you can put this in Matlab and run it.
Your code is long and complex because it works with real data. You should try to reduce it to the smallest possible code that will reproduce your problem, similar to what you tried to do in your question.
Since this is moving away from the answer that was given by Ankur, you should probably post it in a comment on your question.
Tsitsos Dimitris
Tsitsos Dimitris le 18 Mar 2021
This is only a part of my code!
It is much bigger, I don't think that I can make it any smaller because it is a part of a whole image deahzing project.
At my matlab it works but as I have already mentioned to you, I get a really strange picture. It is like every generated picture sticks to the previous one.
I think that the problem is that I can't reset my loop. But I don't know how to do this.
Also I don't really get how your example works, and how it can help to my problem.
In any case thank you for your time.
Rik
Rik le 18 Mar 2021
It is unlikely we actually need your real data. Just like you did in your question, you need to try to keep only the code that is required to reproduce the problem. What you did incorrectly for your question is that it is missing variables, so we can't run the code. It also doesn't looks like it would cause the issue you describe.
What you mean by 'resetting' the loop is confusing to me, but all of that can be solved if you write a MWE. You need to write code that will produce your problem. It doesn't need to use real data, but it must be the smallest it can be. We don't need your full code. You can even use some of the example images implemented in Matlab if you prefer. You can also attach a mat file if you need to.
Sometimes the proces of making a MWE will already show you how to solve the problem on your own.
Tsitsos Dimitris
Tsitsos Dimitris le 18 Mar 2021
Modifié(e) : Tsitsos Dimitris le 18 Mar 2021
I undestand!
Give me some time and I will post my updated code here!
Thank you again
Regarding your email:
There must be a way to reproduce this problem. Try removing code line by line to see what happens when.
As an example:
for i=1:h
for j=1:w
dark_I(i,j)=min(img(i,j,:));
end
end
This sets some values to dark_I. Assuming img contains integers 0-255, you can replace this loop with this:
dark_I(1:h,1:w)=uint8(randi(255,h,w));
It is also possible to create a mat file containing dark_I wrapped in a cell with the values for the first 3 iterations. That should also be enough to replicate the result.
I don't do private consulting, there are other people you can hire for that. You need to find a way to reproduce your issue in fewer lines of code than you posted above. Unless there is a bug caused by a very specific condition (making it hard/impossible to reproduce on my end anyway), it is almost always possible to reduce the cause of such behavior down to code that fits on one screen.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by