Save Movie Video to Computer
17 次查看(过去 30 天)
显示 更早的评论
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 个评论
ssmith
2021-11-1
@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?
Walter Roberson
2021-11-1
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
2021-11-1
@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
2021-11-1
Remember to put in drawnow() inside the loop to see the images as they are created.
What shows up for size(theFiles) ?
ssmith
2021-11-1
@Walter Roberson Ok, I included drawnow; below the imshow function however, nothing prints out. My workspace says theFiles 0x1 struct.
ssmith
2021-11-1
@Walter Roberson where would I try this? after my for loop ends? or in the command window?
ssmith
2021-11-1
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.
Walter Roberson
2021-11-1
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
2021-11-1
@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.
Walter Roberson
2021-11-1
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
2021-11-1
@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?
Walter Roberson
2021-11-1
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786373/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786378/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786383/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786388/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786393/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786398/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786403/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786408/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786413/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786418/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786423/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786428/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786433/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786438/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786443/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786448/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786453/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786458/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786463/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786468/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786473/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/786478/image.png)
ssmith
2021-11-1
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.
Walter Roberson
2021-11-1
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
2021-11-1
编辑:ssmith
2021-11-1
@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
2021-11-1
编辑:Walter Roberson
2021-11-1
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
ssmith
2021-11-1
@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
2021-11-2
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
2021-11-2
编辑:ssmith
2021-11-2
@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
2021-11-2
Please zip and attach the .zip; when you use a .txt extension, some characters get translated.
ssmith
2021-11-2
编辑:ssmith
2021-11-2
@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?
Walter Roberson
2021-11-2
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
2021-11-2
@Walter Roberson This gives me a warning saying fragmentary files might not be DICOM. Is there a way to use dicomreadvolume instead?
Walter Roberson
2021-11-2
编辑:Walter Roberson
2021-11-2
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
2021-11-2
@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?
Walter Roberson
2021-11-2
Folder = fullfile(matlabroot, 'toolbox', 'images', 'imdata', 'dog');
imageArrays = dicomreadVolume(Folder);
imageArrays = rescale(imageArrays);
implay(imageArrays)
ssmith
2021-11-3
@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?
Walter Roberson
2021-11-3
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.
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
2021-11-4
编辑:Walter Roberson
2021-11-4
@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
2021-11-4
@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
Walter Roberson
2021-11-4
Folder = fullfile('C:\Users\Scans');
Data = dicomreadVolume(Folder);
Data = squeeze(Data);
Data = rescale(Data);
implay(Data);
Walter Roberson
2021-11-4
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
2021-11-4
编辑:ssmith
2021-11-4
@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
2021-11-5
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
2021-11-8
@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?
Walter Roberson
2021-11-8
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
2021-11-8
编辑:ssmith
2021-11-8
@Walter Roberson Where did you get the number [1 3 2] and [2 3 1] from?
Walter Roberson
2021-11-8
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
2021-11-8
@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?
ssmith
2021-11-9
@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
2021-11-9
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
2021-11-10
@Walter Roberson Do you know how to use VideoWriter and writeVideo to save my movie player video to my computer folder?
Walter Roberson
2021-11-10
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);
回答(1 个)
Image Analyst
2021-11-9
@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 个评论
ssmith
2021-11-9
@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
2021-11-9
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
2021-11-9
@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
2021-11-9
How did you call implay()? What did you pass into it - a filename or something else?
ssmith
2021-11-9
编辑:ssmith
2021-11-9
@Image Analyst I said
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
2021-11-10
"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.
另请参阅
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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)