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 comentarios

Rik
Rik el 27 de Ag. de 2020
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?
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)
Deepa S
Deepa S el 31 de Ag. de 2020
Editada: Rik el 3 de Sept. de 2020
@Walter Roberson thankyou for the improvement but still code is not adding the next file into the next row. the code is adding all the file into one single row. I want when one file ends the next file should start from the next row.
@Rik my code is adding the next file into the same row of previous file. suppose I have 5 files I need to put each file matrix in a separate row.
Rik
Rik el 31 de Ag. de 2020
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
Walter Roberson el 31 de Ag. de 2020
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)
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
Rik el 1 de Sept. de 2020
Comment posted as flag by Deepa S:
yes, it was the old editor problem. updated and working now.
Deepa S
Deepa S el 3 de Sept. de 2020
Editada: Deepa S el 3 de Sept. de 2020
since matrix of each image is big it cannot fit into one row, so i tried to put in column but it is appending the next image matrix into the same column. so i need every time code reads a new image it should put matrix for each image in a new column . how to proceed for that?
Rik
Rik el 3 de Sept. de 2020
You will have to read the entire file and write it with the extra column.
Walter Roberson
Walter Roberson el 3 de Sept. de 2020
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?
Deepa S
Deepa S el 3 de Sept. de 2020
Editada: Rik el 3 de Sept. de 2020
@Rik yes but how to do that.
@Walter Roberson i think xlsx file will also be fine.
Rik
Rik el 3 de Sept. de 2020
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
Deepa S el 7 de Sept. de 2020
i got it. thankyou

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 3 de Sept. de 2020
Writing each dcm file to a separate column:
myFolder = '403498/New';
outfile = fullfile(myFolder, 'file.csv');
filePattern = fullfile(myFolder, '*.dcm');
dcmimage = dir(filePattern);
numfiles = length(dcmimage);
images = nan(1, numfiles);
for k = 1:numfiles
baseFileName = dcmimage(k).name;
fullFileName = fullfile(myFolder, baseFileName);
I = reshape(dicomread(fullFileName), [], 1);
numI = numel(I);
cursize = size(images,1);
if cursize < numI
images(end+1:numI,:) = nan;
elseif numI < cursize
I(end+1:cursize) = nan;
end
images(:,k) = I;
end
dlmwrite(outfile, images)
Because each file might be a different length, this pads with nan out to the longest file. It is possible to write out empty cells instead, but the file writing portion would be more work.
Note that it would be common for dcm files to have more than 1048576 elements, which is the maximum row limit for xlsx files. If you were planning and processing this csv file through excel or any tool uses excel to read csv files (such as xlsread()) then you are probably going to have problems.
I think you should be reconsidering writing the pixel values as text all into a common file.

3 comentarios

Rik
Rik el 7 de Sept. de 2020
Comment posted as flag by Deepa S:
it works
Rik
Rik el 7 de Sept. de 2020
If it works, why not mark the answer as accepted? And please don't post comments in flags.
Deepa S
Deepa S el 7 de Sept. de 2020
okay.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 27 de Ag. de 2020

Comentada:

el 7 de Sept. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by