write multiple txt files
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
1 개 추천
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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
