dicom pixel value matrix into CVS file
古いコメントを表示
I have 50 dicom files in one folder. i need to transfer pixel matrix of each image file into one common csv file and also the next image matrix must start from the next row in the same csv file. here is my code.
myFolder = ' ';
filePattern = fullfile(myFolder, '*.dcm');
image = dir(filePattern);
for k = 1:length(image)
baseFileName = image(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I= dicomread(fullFileName);
rowImage = reshape(I, 1, []);
if k == 1
csvwrite('file.csv', rowImage, 'delimeter',',');
else
csvwrite('file.csv', rowImage, 'delimiter',',','-append');
end
end
my code is not doing the thing i want . Anyone who can help with this code would be appreciated.
13 件のコメント
Rik
2020 年 8 月 27 日
This code looks like it should work. How is it not doing what you want?
Note that images tend to be very large when converted to text and might not actually fit on a single line of a text file.
Also, why do you want to do this?
Walter Roberson
2020 年 8 月 28 日
Code looks okay, but for efficiency I would recommend opening the file for writing once, and then using fprintf() statements.
fid = fopen('file.csv', 'wt');
[...]
%then instead of csvwrite...
fprintf(fid, '%g,', rowImage(1:end-1));
fprintf(fid, '%g\n', rowImage(end));
Eventually
fclose(fid)
Rik
2020 年 8 月 31 日
What editor are you using to see the result of Walters code? If you're using notepad or a similar program: some older programs don't recognize \n as a line ending and require \r to be present as well. Newer/smarter programs like Notepad++ and most programs on non-Windows machines will show a line end with only \n.
Walter Roberson
2020 年 8 月 31 日
However the 'wt' option I gave in my suggestion would take care of putting in cr and lf if run on Windows. (There could still be an issue if the code was run on Mac or Linux and the file was copied to Windows)
Walter Roberson
2020 年 8 月 31 日
Worked fine in my test.
myFolder = '403498/New';
outfile = fullfile(myFolder, 'file.csv');
filePattern = fullfile(myFolder, '*.dcm');
dcmimage = dir(filePattern);
fid = fopen(outfile, 'wt');
for k = 1:length(dcmimage)
baseFileName = dcmimage(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I= dicomread(fullFileName);
rowImage = reshape(I, 1, []);
fprintf(fid, '%g,', rowImage(1:end-1));
fprintf(fid, '%g\n', rowImage(end));
end
fclose(fid);
Rik
2020 年 9 月 3 日
You will have to read the entire file and write it with the extra column.
Walter Roberson
2020 年 9 月 3 日
Text files can only do that by being rewritten in full.
xlsx are really text files underneath by the way. But it is an easier interface to use them.
Perhaps it would be practical to put each file as a separate worksheet of xlsx file?
Rik
2020 年 9 月 3 日
You don't need to spread the things you want to say over separate comments.
If you know how to read a file and how to write a file, what steps are unclear to you?
Deepa S
2020 年 9 月 7 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Use COM Objects in MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!