Displaying a matrix as an image
2 views (last 30 days)
Show older comments
Hi everyone,
I got an image matrix as an output with help of a code from File Exchange in this website. This code was for converting a range of DICOM files to a single 3D image.
However, I cannot display or 'see' this image matrix output. This matrix is 636x563x465 double and saved as .mat format.
It would be very thankful, if someone could help me.
The following is the code that I went through.
function [volume_image, slice_data, image_meta_data] = ...
dicom23D(dicom_directory, dicom_fields, save_format)
default_dicom_fields = {...
'Filename',...
'Height', ...
'Width', ...
'Rows',...
'Columns', ...
'PixelSpacing',...
'SliceThickness',...
'SliceLocation',...
'SpacingBetweenSlices'...
'ImagePositionPatient',...
'ImageOrientationPatient',...
'FrameOfReferenceUID',...
};
no_pixel_spacing = false;
no_slice_thickness = false;
extra_fields = {...
'PhysicalHeight',... % Height (cols) of slice in mm
'PhysicalWidth',... % Width (rows) of slice in mm
'PixelSliceLocation',... % Slice z-location in pixels
'PixelSliceThickness',... % Slice thickness in pixels
'SliceData'... % The slice image data
};
image_meta_data = struct(...
'PhysicalTotalZ',[],... % Total extent of image in Z direction
'NumberOfSlices',[],... % Number of slices
'PhysicalAspectRatio',[]);... % Aspect ratio of image as a whole :=
% [PhysicalWidth, PhysicalHeight, SliceThickness*NumberOfSlices]
if nargin<1
dicom_directory = uigetdir();
end
if isempty(dicom_directory)
dicom_directory = uigetdir();
end
if nargin<3
save_format = 'mat';
end
if nargin<2
dicom_fields = default_dicom_fields;
end
all_fields = [dicom_fields, extra_fields];
warning off;
N = numel(listing); % How many entries in the directory listing
if (N<3)
error('Empty folder');
return
end
slice_data(N) = cell2struct(cell(size(all_fields)), all_fields, 2);
h = waitbar(0,'Reading DICOM Files...','WindowStyle','modal');
true_index = 0; % a sequential index of dicom files, that is ignoring
% files of other types.
for i = 3:length(listing) % loop through directory listing, but skip '.' and '..'
filename = listing(i).name;
[dummy_path, just_the_name, extension] = fileparts(filename);
full_path = fullfile(dicom_directory, filename);
goodfile = false;
% Check for good dicom file
if isdicom(full_path)
true_index = true_index + 1;
header = dicominfo(full_path);
slice_image = dicomread(header);
% Save selected header data into the structure slice_data
for j = 1:numel(dicom_fields) % loop through dicom field names
current_field = dicom_fields{j};
% Deal with requested fields not found in header
if isfield(header, current_field)
slice_data(true_index).(current_field) = header.(current_field);
else
['header did not contain the field ' current_field]
end %if
end % loop through dicom field names
% done saving filtered header data
% Save slice data
slice_data(true_index).SliceData = slice_image;
% Save extra fields
needed_header_tags = [...
isfield(header, 'PixelSpacing'), ...
isfield(header, 'SliceThickness'), ...
isfield(header, 'SliceLocation')...
];
if all(needed_header_tags)
pixel_spacing = header.PixelSpacing;
slice_data(true_index).PhysicalHeight = ...
double(pixel_spacing(1)*header.Columns);
slice_data(true_index).PhysicalWidth = ...
double(pixel_spacing(2)*header.Rows);
% need to double check which aspect ratio goes with cols/rows
slice_data(true_index).PixelSliceLocation = ...
header.SliceLocation / mean(pixel_spacing);
slice_data(true_index).PixelSliceThickness = ...
header.SliceThickness / mean(pixel_spacing);
else
no_pixel_spacing = true;
end % if pixel spacing
end % if isdicom
waitbar(i/N,h);
end % loop through directory listing
% Eliminate empty structs at end.
slice_data = slice_data(1:true_index);
waitbar(1,h);
close(h);
warning on;
% Check that some dicom slice was found
if true_index < 1
'No dicom slices found...returning empty'
volume_image = [];
slice_data = [];
image_meta_data = [];
return
end
% If SliceLocation is known, sort by that. This is deemed more
% accurate than going by filename order (or file number).
if isfield(slice_data(1), 'SliceLocation')
[S,I] = sort([slice_data.SliceLocation]);
slice_data = slice_data(I);
end
% Pre-allocate volume image array
[rows, cols] = size(slice_data(1).SliceData);
volume_image = ...
zeros(rows, cols, length(slice_data));
% Build volume image array
h = waitbar(0,'Writing slice images to volume image array...','WindowStyle','modal');
for i = 1:length(slice_data)
waitbar(i/N,h);
volume_image(:,:,i) = slice_data(i).SliceData;
end
close(h);
% If SliceThickness is known, calculate the total Z extent of slices.
image_meta_data.NumberOfSlices = length(slice_data);
if isfield(slice_data(1), 'SliceThickness')
image_meta_data.PhysicalTotalZ = ...
double(slice_data(1).SliceThickness*length(slice_data));
else
no_slice_thickness = true;
end
% if PixelSpacing and SliceThickness is known, create
% PhysicalAspectRatio.
if ~no_pixel_spacing && ~no_slice_thickness
image_meta_data.PhysicalAspectRatio = [...
slice_data(1).PixelSpacing(1),...
slice_data(1).PixelSpacing(2),...
slice_data(1).SliceThickness...
];
end
% Save the data to the dicom directory.
h = waitbar(0, 'Saving mat files...', 'WindowStyle', 'modal');
waitbar(0,h);
save(fullfile(dicom_directory,'VOLUME_IMAGE'),...
'volume_image', 'slice_data', 'image_meta_data');
% local_directory = pwd;
% eval(['cd ' dicom_directory]);
% save VOLUME_IMAGE volume_image slice_data image_meta_data
% eval(['cd ' local_directory]);
close(h);
0 Comments
Answers (1)
Yash
on 20 Nov 2023
Edited: Yash
on 20 Nov 2023
Hi Jaehyun,
I understand that you are interested in analysing a 3D volume stored in a .mat file.
To achieve this, you first need to extract the matrix from the .mat file using the "load" function. Following is the link for the documentation of the "load" function: https://www.mathworks.com/help/matlab/ref/load.html
load('data.mat')
Once the matrix has been loaded in the workspace, you can then use the Volume Viewer tool to analyse the volume. Given below is an example:
% The matrix A has 6 matrices of size 10*50*50 with increasing values
A = [zeros(10,50,50); 0.2*ones(10,50,50); 0.4*ones(10,50,50); 0.6*ones(10,50,50); 0.8*ones(10,50,50); ones(10,50,50)];
volumeViewer(A)
Since volumeViewer is not supported in MATLAB Online, I am attaching a screenshot of obtained output:
For more details about the Volume Viewer Tool, you can visit its documentation at the following link: https://www.mathworks.com/help/images/ref/volumeviewer-app.html
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!