can you create a fonction that read all the dcm images from a folder and concatane it into a 3d matrix in matlab

You can use the dicomread function to read all the dcm images from a folder and use the cat function to concatenate them in a 3D matrix. Here's how you can do it:

main.m
% Define the folder path
folder_path = 'path/to/folder';

% Get a list of all the dcm files in the folder
files = dir(fullfile(folder_path, '*.dcm'));

% Loop through the files and read them one by one
for i = 1:length(files)
    filename = fullfile(folder_path, files(i).name);
    img = dicomread(filename);
    if i == 1
        % Initialize the 3D matrix with the size of the first image
        img_stack = zeros(size(img, 1), size(img, 2), length(files));
    end
    % Concatenate the images in the 3D matrix
    img_stack(:, :, i) = img;
end
550 chars
18 lines

The resulting img_stack variable will be a 3D matrix where the first two dimensions are the size of the images and the third dimension is the number of images. You can access each individual image using indexing, for example img_stack(:, :, 1) will give you the first image.

related categories

gistlibby LogSnag