Quite often you need to convert an image (or multiple images) to a MatLab matrix for further analysis and visualization (e.g. extracting time series, multivariate pattern analysis, etc). SPM provides handy functions for this:
P = spm_select; % select 1 or more images V = spm_vol(P); M = spm_read_vols(V);
The dimension of M is 3D or 4D depending on how many images you selected. The 1st 3 dimensions are spatial and the last one is temporal.
If you want to save a matrix to a image file, use
V.fname = 'b.img';% spm_write_vol(V, M);
3 Replies to “Convert images to matrix”
i need to generate .mat file for ORL database(say). how to do it?also can i use the same .mat file in regression/classification?how to split database into predictor and response arrays (X & Y) for regression/classification.
please provide suitable matlab functions.
How can I correct the order of images in the file used by spm_vol.
If I remember correctly, the order is actually correct.