Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active August 29, 2015 13:57
Show Gist options
  • Save kmader/9659038 to your computer and use it in GitHub Desktop.
Save kmader/9659038 to your computer and use it in GitHub Desktop.
The starting point for a matlab function to perform shape analysis and fitting an ellipse to your data
% this function (will) performs the ellipsoid based shape analysis on a 3D image
% the input is a labeled image probably from the bwlabel function
% the output is an array formatted like such
% labelId, volume, centerX, centerY, centerZ, extentsX, extentsY, extentsZ, pca1X, pca1Y, pca1Z, score1, score2, score3
function [out_table]=ellipse_analysis(in_image)
% create an array the same size as the image of x,y, and z points. This way we can calculate
% center of volume and other parameters by using multiplication
[xgrid,ygrid,zgrid]=meshgrid(1:size(in_image,1),1:size(in_image,2),1:size(in_image,3));
num_of_obj=max(in_image(:));
num_of_outputs=14;
% assign the columns to the output table
label_col=1;
vol_col=2;
covx_col=vol_col+1;
covy_col=covx_col+1;
covz_col=covy_col+1;
extx_col=covz_col+1;
exty_col=extx_col+1;
extz_col=exty_col+1;
pca1_col=extz_col+1;
out_table=zeros(num_of_obj,num_of_outputs)
for cur_obj = 1 : num_of_obj
% save the label and the volume
out_table(cur_obj,label_col)=cur_obj;
out_table(cur_obj,vol_col)=sum(in_image(:)==cur_obj);
% get the x,y, and z positions of each voxel
xpts=xgrid(in_image==cur_obj);
ypts=ygrid(in_image==cur_obj);
zpts=zgrid(in_image==cur_obj);
out_table(cur_obj,covx_col)=mean(xpts);
out_table(cur_obj,covy_col)=mean(ypts);
out_table(cur_obj,covz_col)=mean(zpts);
covar_pts=cov([xpts ypts zpts]);
% write the code to finish the eigentransform (hint: eigen)
% save the first (largest) eigenvector as pca1,
% and the scores (eigenvalues) as score1...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment