12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- % Input folder path containing fMRI data
- input_folder = "C:\Users\aswen\Desktop\TestingData\Aswendt_qc_data\proc_data";
- % Get all rsfMRI files in the input folder
- searchpath = fullfile(input_folder, '**', 'func', '*nii.gz');
- rsfmri_files = dir(searchpath);
- rsfmri_files = fullfile({rsfmri_files.folder}, {rsfmri_files.name});
- % Parameters for motion correction
- transformType = 'translation'; % Type of transformation (e.g., translation)
- [optimizer,metric] = imregconfig("multimodal")
- % Loop through each rsfMRI file for motion correction
- for i = 1:numel(rsfmri_files)
- % Read file
- file = rsfmri_files{i};
-
- % Load the 4D rsfMRI image and its info
- Im = niftiread(file);
- info = niftiinfo(file);
- % Drop the first 10 volumes
- Im = Im(:,:,:,11:end);
- info.ImageSize(4) = size(Im, 4); % Adjust header info to the new size
-
- % Reference image for registration (using the first volume)
- fixed = Im(:,:,:,1);
-
- % Perform motion correction for each volume
- for t = 2:size(Im, 4)
- % Register the current volume to the reference volume
- moving = Im(:,:,:,t);
- moving_reg = imregister(moving, fixed, transformType, optimizer, metric);
-
- % Replace the original volume with the registered volume
- Im(:,:,:,t) = moving_reg;
- end
-
- % Save the motion-corrected 4D rsfMRI image
- [filepath, filename, ext] = fileparts(file);
- output_filename = [filename '_mc' ext]; % Appending '_mc' to the filename
- output_path = fullfile(filepath, output_filename);
- niftiwrite(Im, output_path, info, 'Compressed', true);
- end
|