Save Movie Video to Computer

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

ssmith
ssmith el 1 de Nov. de 2021
@Walter Roberson is there a way to take dcm files from a folder saved on the computer and enter the folder into matlab to be read?
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
ssmith el 1 de Nov. de 2021
@Walter Roberson This is what I have my code to look like
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);
imshow(imageArray);
end
But now it will not print anything despite using imshow function.
Walter Roberson
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
ssmith el 1 de Nov. de 2021
@Walter Roberson Ok, I included drawnow; below the imshow function however, nothing prints out. My workspace says theFiles 0x1 struct.
What shows up if you
ls(Folder)
ssmith
ssmith el 1 de Nov. de 2021
@Walter Roberson where would I try this? after my for loop ends? or in the command window?
ssmith
ssmith el 1 de Nov. de 2021
When I enter it after my for loop closed, it prints out a bunch of the data points from my dcm files, but I am trying to print an image that is created from those data points rather than the numbers themselves.
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
ssmith el 1 de Nov. de 2021
@Walter Roberson Oh I see. It does print the files that are present in my folder. But do I have to write imshow and drawnow after the for loop? I am not getting anything printing out.
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
ssmith el 1 de Nov. de 2021
@Walter Roberson Yes, but the for loop does not print out an image. If I want my image in grayscale how can i use imshow after the for loop?
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
ssmith el 1 de Nov. de 2021
Where you wrote Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
Should I write matlabroot and then the name of my file in ' '? I have the same code as you but for some reason mine is still not printing.
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
ssmith el 1 de Nov. de 2021
Editada: ssmith el 1 de Nov. de 2021
@Walter Roberson So it appear that I have DICOM files but no DICOM images. Is there a way to do the code with DICOM files in 2D images with x,y,z coordinates and grayscale value?
ssmith
ssmith el 1 de Nov. de 2021
Editada: Walter Roberson el 1 de Nov. de 2021
This is what I have so far
Folder = fullfile('Scans');
filePattern = fullfile(Folder, '*');
theFiles = dir(filePattern);
theFiles([theFiles.isdir]) = [];
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 = discomread(fullFileName);
dcm_count = dcm_count + 1;
all_filenames{dcm_count} = fullFileName;
all_images{dcm_count} = imageArray;
catch ME
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) = [];
all_images(dcm_count+1:end) = [];
for k = 1 : dcm_count
imshow(all_images{K}, []);
title(all_filenames{K});
pause(1);
end
You have
for k = 1 : dcm_count
but that should be
for K = 1 : dcm_count
ssmith
ssmith el 1 de Nov. de 2021
@Walter Roberson If I am getting an error saying my folder has files but no dicom images, is there a way to use the dicom files to print the CT scans above?
Walter Roberson
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
ssmith
ssmith el 2 de Nov. de 2021
Editada: ssmith el 2 de Nov. de 2021
@Walter Roberson This is one of the 600 files that I was given. Hopefully this helps. I was not taught MATLAB previously and am struggling in class. They are listed as file in the folder, but when I open them to load them here, they become .txt files.
Walter Roberson
Walter Roberson el 2 de Nov. de 2021
Please zip and attach the .zip; when you use a .txt extension, some characters get translated.
ssmith
ssmith el 2 de Nov. de 2021
Editada: ssmith el 2 de Nov. de 2021
@Walter Roberson Try this. Also the pixel size is 0.25 by 0.25 mm with slice thickness 0.5 mm. Is there a way for me to replace dicomread with dicomreadvolume?
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
ssmith el 2 de Nov. de 2021
@Walter Roberson This gives me a warning saying fragmentary files might not be DICOM. Is there a way to use dicomreadvolume instead?
Walter Roberson
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
ssmith el 2 de Nov. de 2021
@Walter Roberson When you ran this code
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
Do you know if there is a way to stack all the images you printed out on top of each other to make them into a 3D/4D image rather than in 2D?
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
ssmith
ssmith el 3 de Nov. de 2021
@Walter Roberson What is the reason you write - Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
Would I just replace the matlabroot, 'toolbox', 'images', 'imdata', 'dog' part with the name of my folder?
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
Error using matlab.internal.lang.capability.Capability.require (line 94)
This functionality is not available on remote platforms.

Error in implay (line 51)
matlab.internal.lang.capability.Capability.require(...
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
ssmith el 4 de Nov. de 2021
Editada: Walter Roberson el 4 de Nov. de 2021
@Walter Roberson Would I use
Folder = fullfile('Scans');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
To replace
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArray = dicomread(fullFileName);
figure()
imshow(imageArray, []);
ssmith
ssmith el 4 de Nov. de 2021
@Walter Roberson Is there a way to include
Folder = fullfile('C:\Users\Scans');
Data = dicomreadVolume(Folder);
Data = squeeze(Data);
into the code to produce the image in a 512x512x595
Folder = fullfile('C:\Users\Scans');
Data = dicomreadVolume(Folder);
Data = squeeze(Data);
Data = rescale(Data);
implay(Data);
Walter Roberson
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.
ssmith
ssmith el 4 de Nov. de 2021
Editada: ssmith el 4 de Nov. de 2021
@Walter Roberson Thank you for clearifying! By chance do you know how to make the video figure that prints out to be clearer? It is very grainy. Do you know what command will lighten the video or change the grayscale?
Walter Roberson
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
ssmith el 8 de Nov. de 2021
@Walter Roberson Do you know how to rotate the movie view to produce three different axal views? Is there a way to adjust implay to rotate its output video?
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.
ssmith
ssmith el 8 de Nov. de 2021
Editada: ssmith el 8 de Nov. de 2021
@Walter Roberson Where did you get the number [1 3 2] and [2 3 1] from?
Walter Roberson
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
ssmith el 8 de Nov. de 2021
@Walter Roberson Do you know if it is necessary to do another dimension with Z, X, Y (3 1 2) as that is the only pattern not done?
Walter Roberson
Walter Roberson el 9 de Nov. de 2021
No, 3 1 2 is just the sideways version of 1 3 2
ssmith
ssmith el 9 de Nov. de 2021
@Walter Roberson Do you know what the function is to zoom out in the videos made using implay() and how do you save the videos after? I am only able to save an image of the video.
Walter Roberson
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
ssmith el 10 de Nov. de 2021
@Walter Roberson Do you know how to use VideoWriter and writeVideo to save my movie player video to my computer folder?
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);

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
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
ssmith el 9 de Nov. de 2021
@Image Analyst That is not what I am looking for. I simply want to save my videos made in matlab but there is no save button under the file tab.
Image Analyst
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
ssmith el 9 de Nov. de 2021
@Image Analyst I read data using implay() which converted it into a movie video and I want to save the movie video that was created from the implay() function. Do you know how to save it without a save as feature in the file drop down menu?
Image Analyst
Image Analyst el 9 de Nov. de 2021
How did you call implay()? What did you pass into it - a filename or something else?
ssmith
ssmith el 9 de Nov. de 2021
Editada: ssmith el 9 de Nov. de 2021
data = folder('scans');
scans = data;
implay(scans);
I think I need to use VideoWriter but I am not sure how to do that
Image Analyst
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.

Iniciar sesión para comentar.

Preguntada:

el 31 de Oct. de 2021

Comentada:

el 10 de Nov. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by