필터 지우기
필터 지우기

write multiple txt files

조회 수: 3 (최근 30일)
Mahmoud Hassan
Mahmoud Hassan 2019년 1월 5일
댓글: Rik 2019년 1월 5일
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

채택된 답변

Rik
Rik 2019년 1월 5일
편집: Rik 2019년 1월 5일
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
Mahmoud Hassan
Mahmoud Hassan 2019년 1월 5일
편집: Mahmoud Hassan 2019년 1월 5일
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 2019년 1월 5일
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])

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by