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

 Accepted Answer

Rik
Rik on 5 Jan 2019
Edited: Rik on 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 Comments

Mahmoud Hassan
Mahmoud Hassan on 5 Jan 2019
Edited: Mahmoud Hassan on 5 Jan 2019
first, now i face these errors ... and i dont need to sum because i want the dark points in each row so i just need to count then ct = 0 after each row
note: i am using matlab 2014
do you have any idea why i get these errors ?!
Rik
Rik on 5 Jan 2019
Edited: Rik on 5 Jan 2019
The imread error is something you need to solve from your end. The creation of the file name goes as it should:
>> sprintf('%d.tif',4)
ans =
4.tif
And did you actually look at what I did with the sum? I didn't take the sum of the pixel values, but I first converted it to a logical matrix where all pixels with intensity 0 would be true. This means you can skip the col-loop. You could also omit the row-loop:
for H=1:45
I=imread(sprintf('%d.tif',H));%consider using a full path
file=fopen(sprintf('%d.txt',H),'wt');
fprintf('Rotated: %6.2f.\n');%no input, and printed to the command line
sums_list=sum(I==0,2);
fprintf(file,'%2.0f, \n',sums_list);
fprintf('Rotated: %6.2f.\n',ct);%printed to the command line
fclose(file);
end
This code is all predicated on the assumption that you actually can read the image file and that it only contains intensity values, and not RGB-values.
I have understood what you said and your code ... but i dont know why i get the same damn error :/
It appears that Matlab has trouble determining the format. Maybe Matlab expects tiff-files to have the extension tiff instead of tif.
To remedy this, the documentation (R2014b) explains the syntax for forcing the format:
A = imread(sprintf('%d.tif',H), 'tiff')
Also, I have just noticed I made a mistake in copy-paste. If you look at the .txt file, I forgot to change the extension to .txt, which caused the images to be overwritten. I'll edit my posts to fix this.
Mahmoud Hassan
Mahmoud Hassan on 5 Jan 2019
Edited: Mahmoud Hassan on 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 ?!
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])

Sign in to comment.

More Answers (0)

Categories

Asked:

on 5 Jan 2019

Commented:

Rik
on 5 Jan 2019

Community Treasure Hunt

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

Start Hunting!