Save Movie Video to Computer
Mostrar comentarios más antiguos
I have almost 600 dcm files saved into one large folder that I want to load into my MATLAB code to read the data files and print out a grayscale image. I am not sure how to call the 600 dcm files. I tried dicomread but it is not reading. Any help would be appreciated.
47 comentarios
Walter Roberson
el 31 de Oct. de 2021
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
The code at the link I posted show how to designate a directory name (the assignment to myFolder) and to find a certain kind of files in the directory, and to loop reading one file at a time from the directory. You would change the assignment to myFolder and you would change
filePattern = fullfile(myFolder, '*.png')
to
filePattern = fullfile(myFolder, '*.dcm')
and you would change
imageArray = imread(fullFileName);
to
imageArray = dicomread(fullFileName);
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
Remember to put in drawnow() inside the loop to see the images as they are created.
What shows up for size(theFiles) ?
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
What shows up if you
ls(Folder)
ssmith
el 1 de Nov. de 2021
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
Enter that at the command line.
ls(Folder)
will not show the content of any of your files. It will, however, list which files exist there.
By looking at the listing, you might notice a difference between the file names you were expecting (ending with .dcm) and the files actually present. For example, perhaps you might find that the files actually present have (say) a .dicom file extension. Or perhaps they do not have any file extension at all (DICOM does not care what the extension is.)
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
No, the point is that your files are NOT named with .dcm extension, but with something else instead. For example if you look at the ls listing and see that they are named with a .DICOM extension then you would change
filePattern = fullfile(Folder, '*.dcm');
to
filePattern = fullfile(Folder, '*.DICOM');
The ls() is only for debugging purposes, to allow you to see what is really in your folder so that you can change the .dcm to whetever your files are named instead.
Once you have adjusted the .dcm to whatever is in your directory, then you will not need the ls() and the rest of the code should work.
ssmith
el 1 de Nov. de 2021
Works when I try.
Note that you are not storing the data you read in. You can certainly do that, such as by storing into a cell array.
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
filePattern = fullfile(Folder, '*.dcm');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
imageArray = dicomread(fullFileName);
figure()
imshow(imageArray, []);
end
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
Modify the code you were using before, with your current version of Folder
filePattern = fullfile(Folder, '*');
theFiles = dir(filePattern);
theFiles([theFiles.isdir]) = []; %get rid of folders including . and ..
nfiles = length(theFiles);
if nfiles == 0
error('Folder "%s" contains no files', Folder);
end
all_filenames = cell(nfiles, 1);
all_images = cell(nfiles, 1);
dcm_count = 0;
for k = 1 : nfiles
fullFileName = fullfile(theFiles(k).folder, theFiles(k).name);
try
imageArray = dicomread(fullFileName);
%success
dcm_count = dcm_count + 1;
all_filenames{dcm_count} = fullFileName;
all_images{dcm_count} = imageArray;
catch ME
%current file is either not dicom at all or there was a failure
end
end
if dcm_count == 0
error('Folder "%s" had files but no dicom images', Folder);
end
fprintf('Succeeded in reading %d DICOM files\n', dcm_count);
all_filenames(dcm_count+1:end) = []; %remove excess
all_images(dcm_count+1:end) = []; %remove excess
At this point, all of the DICOM files have been read in and stored in the cell array all_images . The all_filenames array holds the corresponding file name.
Every file in the given folder is attempted as a potential DICOM image. It has been too difficult to find out from you what file extension your DICOM files are using, so I will have to assume that the extensions are missing or inconsistent.
Once all of the images have been read in, you can display them:
for K = 1 : dcm_count
imshow(all_images{K}, []);
title(all_filenames{K});
pause(1);
end
ssmith
el 1 de Nov. de 2021
Editada: Walter Roberson
el 1 de Nov. de 2021
Walter Roberson
el 1 de Nov. de 2021
You have
for k = 1 : dcm_count
but that should be
for K = 1 : dcm_count
ssmith
el 1 de Nov. de 2021
Walter Roberson
el 2 de Nov. de 2021
No. There is nothing in the directory that can be read as DICOM data of any kind.
Can you attach one of the files that you believe contains dicom data? You will need to zip it and attach the zip. Do not do that if the files might contain private patient information.
Which operating system are you using? Some tests are easier on Mac or Linux
Walter Roberson
el 2 de Nov. de 2021
Please zip and attach the .zip; when you use a .txt extension, some characters get translated.
Walter Roberson
el 2 de Nov. de 2021
Your code had
imageArray = discomread(fullFileName);
which should have been
imageArray = dicomread(fullFileName);
In future, please note that at the top right corner of "code blocks" that are posted here, there is a Copy button that will copy the text of the code to your clipboard so you can just paste it into your files instead of retyping it.
ssmith
el 2 de Nov. de 2021
Walter Roberson
el 2 de Nov. de 2021
Editada: Walter Roberson
el 2 de Nov. de 2021
I don't know. You could try
images = dicomreadVolume(Folder);
Remember that because I do not know what files are in the directory, I had to resort to trying to read all files in the directory. Perhaps you could
filePattern = fullfile(Folder, '0001*');
ssmith
el 2 de Nov. de 2021
Walter Roberson
el 2 de Nov. de 2021
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
ssmith
el 3 de Nov. de 2021
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
This shows that dicomreadVolume worked ... but I cannot happen to use implay in this online facility.
Yes, for your purposes you would use
Folder = fullfile('Scans');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
ssmith
el 4 de Nov. de 2021
Editada: Walter Roberson
el 4 de Nov. de 2021
ssmith
el 4 de Nov. de 2021
Walter Roberson
el 4 de Nov. de 2021
Folder = fullfile('C:\Users\Scans');
Data = dicomreadVolume(Folder);
Data = squeeze(Data);
Data = rescale(Data);
implay(Data);
Walter Roberson
el 4 de Nov. de 2021
To answer your earlier question: the version with dicomreadVolume reads all of the files, and then opens a player tool; the version with dicomread() not in a loop reads only a single image and displays it.
Walter Roberson
el 5 de Nov. de 2021
use imhist before rescale to determine the range that most of the data is in. Then when you rescale, use the optional parameters to indicate lower and upper bounds for the mapping, with the lower bound mapped to 0 and the upper bound mapped to 1.
Determining the best lower and upper bounds can be a bit tricky.
ssmith
el 8 de Nov. de 2021
Walter Roberson
el 8 de Nov. de 2021
implay() goes through each 2D image in turn, with number of frames equal to the length of the third dimension (when you call it with that size of array.)
You can slice the image a different way, such as
DataXY = Data;
DataXZ = permute(Data, [1 3 2]);
DataYZ = permute(Data, [2 3 1]);
However, you cannot really put those together in a single video, because each one has a different number of frames.
Walter Roberson
el 8 de Nov. de 2021
Those are dimension numbers for rearrangement using the permute() function. [1 3 2] asks you to leave dimension 1 where it already is, and the new second dimension is to be what was the third dimension before, and the new third dimension is to be what was the second dimension before. If the data order was X Y Z before, then after it will be X Z Y
(Though this is a bit misleading, because probably the data order starts out as Y X Z -- with rows corresponding to vertical distance and columns corresponding to horizontal distance, so you might want to adjust the above permute() commands. Remember that the dimension that will become vertical distance on the graph needs to become the new first dimension, and that the dimension that will become horizontla distance on the graph needs to become the new second dimension.)
ssmith
el 8 de Nov. de 2021
Walter Roberson
el 9 de Nov. de 2021
No, 3 1 2 is just the sideways version of 1 3 2
ssmith
el 9 de Nov. de 2021
Walter Roberson
el 9 de Nov. de 2021
What do you mean by "zoom out" in this case? You want lower resolution? You want black padding on the sides so that for a fixed-size axes the medical data would take up a smaller fraction of the axes? You want larger images (by interpolation) ?
ssmith
el 10 de Nov. de 2021
Walter Roberson
el 10 de Nov. de 2021
Yes, just like the VideoWriter documentation shows for those functions. Just make sure that when you pass the file name to VideoWriter() that you pass the complete path, such as
filename = fullfile(output_directory, 'XY_view.avi');
obj = VideoWriter(filename);
Respuestas (1)
Image Analyst
el 9 de Nov. de 2021
0 votos
@ssmith, please see a variety of demos attached where I deal with videos.
You should be able to adapt one of them to make your video. For example to create a zoomed video, extract the portion of the image you want to zoom into, then resize it, then write it to the output video.
6 comentarios
ssmith
el 9 de Nov. de 2021
Image Analyst
el 9 de Nov. de 2021
Then I'm not sure what you mean. You can have a variable that is a "movie" with all the frames that you create, and you can cycle through them to display them as a real time movie, but to have that as a movie file on disk, you're going to have to use VideoWriter to save the movie frames to disk. There is nothing on the MATLAB tool ribbon to save one of your variables as a movie. You have to do it with VideoWriter programmatically in your code. You said you "made" the videos but I don't know what that means. Did you make the movie variable (a structure array)? Did you then save it to disk with VideoWriter? What exactly does "made" mean to you and how is "made" different than "save"?
ssmith
el 9 de Nov. de 2021
Image Analyst
el 9 de Nov. de 2021
How did you call implay()? What did you pass into it - a filename or something else?
Image Analyst
el 10 de Nov. de 2021
"folder()" is not a function. In my ExtractMovieFrames.m file, I show you how to create a movie from a bunch of individual still images. Use that.
Categorías
Más información sobre Blocked Images en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





















