write multiple txt files

6 vues (au cours des 30 derniers jours)
Mahmoud Hassan
Mahmoud Hassan le 5 Jan 2019
Commenté : Rik le 5 Jan 2019
hello, i want to read 45 images. these images are binarized and their names is numbered from 1 to 45 and then count number of black points "zeros" in each image and set the result in txt file with the same name of the image but i have an error in the line of " I=imread( H '.tif' ).
i think that i have syntax error but i dont know what is it
for H=1:45
I=imread( H '.tif');
J = I;
FileName=[ H '.txt'];
file=fopen(FileName,'wt');
[rows,cols]=size(J);
fprintf('Rotated: %6.2f.\n');
for r=1:rows
ct=0 ;
for c=1:cols
pic=J(r,c);
if pic == 0
ct = ct+1 ;
else
%%gray(i,j)=0;
end
end
fprintf(file,'%2.0f, \n',ct);
fprintf('Rotated: %6.2f.\n',ct);
end
fclose(file);
end

Réponse acceptée

Rik
Rik le 5 Jan 2019
Modifié(e) : Rik le 5 Jan 2019
A few lines later you try something better when you try to form a file name. But the problem is a bit deeper: you are trying to implicitly convert a number to its equivalent char. You should do that explicitly.
The code below still has some issues that you need to address.
for H=1:45
I=imread(sprintf('%d.tif',H));%consider using a full path
J = I;
FileName=sprintf('%d.txt',H);
file=fopen(FileName,'wt');
[rows,cols]=size(J);
fprintf('Rotated: %6.2f.\n');%no input, and printed to the command line
for r=1:rows
ct=0 ;
for c=1:cols
pic=J(r,c);
if pic == 0
ct = ct+1 ;
else
%%gray(i,j)=0;
end
end
fprintf(file,'%2.0f, \n',ct);
fprintf('Rotated: %6.2f.\n',ct);%printed to the command line
end
fclose(file);
end
You could also use the sum function instead of this loop:
for H=1:45
I=imread(sprintf('%d.tif',H));%consider using a full path
J = I;
FileName=sprintf('%d.txt',H);
file=fopen(FileName,'wt');
[rows,cols]=size(J);
fprintf('Rotated: %6.2f.\n');%no input, and printed to the command line
sums_list=sum(J==0,2);
for r=1:rows
ct=sums_list(r);
fprintf(file,'%2.0f, \n',ct);
fprintf('Rotated: %6.2f.\n',ct);%printed to the command line
end
fclose(file);
end
  7 commentaires
Mahmoud Hassan
Mahmoud Hassan le 5 Jan 2019
Modifié(e) : Mahmoud Hassan le 5 Jan 2019
it worked bro ! really thank you so much ... and yeah i noticed it that wasnt the problem ... but i have one more question pls ... does your code would have different result for number of dark points in each row from my code ? i mean is my logic to calculate the dark points is wrong or less effective than your sums_list ?!
Rik
Rik le 5 Jan 2019
It should be the exact equivalent output, but my code should get there much faster.
With increasing size the benefit grows. This also has to do with the expected number of zero values in your image, see the block below.
The sum method takes 29.5 % of the time that the loop takes (@ size(I)=[1000 1], total time= 0.004 seconds)
The sum method takes 20.7 % of the time that the loop takes (@ size(I)=[1000 10], total time= 0.005 seconds)
The sum method takes 15.8 % of the time that the loop takes (@ size(I)=[1000 100], total time= 0.027 seconds)
The sum method takes 8.6 % of the time that the loop takes (@ size(I)=[1000 1000], total time= 0.159 seconds)
The sum method takes 5.9 % of the time that the loop takes (@ size(I)=[1000 10000], total time= 3.326 seconds)
The sum method takes 3.7 % of the time that the loop takes (@ size(I)=[1000 100000], total time= 49.634 seconds)
This was generated with this code:
clc
%5 takes about 3.5 seconds on my machine, 6 already 50
timing_perc=zeros(1,6);
repeats=20;%keep high to account for JIT optimization
for sz=0:(numel(timing_perc)-1)
I=randi([0 10],1000,10^sz);
out1=zeros(size(I,1),1);
out2=out1;
tic
for n=1:repeats
out1=sum(I==0,2);
end
t1=toc;
tic
for n=1:repeats
[rows,cols]=size(I);
for r=1:rows
ct=0 ;
for c=1:cols
pic=I(r,c);
if pic == 0
ct = ct+1 ;
else
%%gray(i,j)=0;
end
end
out2(r)=ct;
end
end
t2=toc;
%fprintf('Elapsed time is %8f seconds for sum.\n',t1)
%fprintf('Elapsed time is %8f seconds for loop.\n',t2)
fprintf(['The sum method takes %.1f %% of the time that the ',...
'loop takes (@ size(I)=[%d %d], total time= %.3f seconds)\n'],...
100*t1/t2,size(I,1),size(I,2),t1+t2)
timing_perc(sz+1)=100*t1/t2;
end
figure(1),clf(1)
plot(0:sz,100-timing_perc)
xlabel('order of magnitude of image size')
ylabel('percentage of time gain')
ylim([50 100])

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by