write multiple txt files
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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
採用された回答
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
2019 年 1 月 5 日
編集済み: Mahmoud Hassan
2019 年 1 月 5 日
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 

Mahmoud Hassan
2019 年 1 月 5 日
do you have any idea why i get these errors ?!
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.
Mahmoud Hassan
2019 年 1 月 5 日
I have understood what you said and your code ... but i dont know why i get the same damn error :/
Rik
2019 年 1 月 5 日
It appears that Matlab has trouble determining the format. Maybe Matlab expects tiff-files to have the extension tiff instead of tif.
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
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
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 件)
カテゴリ
ヘルプ センター および File Exchange で Desktop についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
