MATLAB: How to find the coordinates of the top and bottom end plate and calculate the height

digital image processingheighthelpImage Processing Toolboxpgoncorners

Hi all,
I am trying to automate the calculation of each height of the vertebra.
I have bwlabel my binary image so that it labels 1 to 24 from top to bottom.
I have used regionprops the get the centroid and bounding box. I understand that the boundingbox does give the height and width however it does not trully reflect the height of each vertebra as some of them are at an angle. (1st picture)
bwboundaries does give me all the coordinates of the boundaries but how do I use that to find an average of height of each of the vertebra? (2nd picture)
Or is there a way to run through the image in a way that I can get 3 coordinates from the top and the bottom end plate of the vertebrae respectively, find the average height and perhaps draw a line through the 3 coordinates?
My end goal is to be able to find the height and draw a line through the top and bottom end plate of each vertebrae if possible like the image below.

Best Answer

The FEX submission pgonCorners (Download) can be used to find approximate corners for each of the vertebrae. Once you have the corner points, you can draw lines through the ones with similar vertical coordinates to demarcate the end plates. You can also average the vertical coordinates of the 4 vertices to define an overall height for each vertebra.
load spine
[m,n]=size(BW);
A=false(m,n);
R=regionprops(BW,'PixelIdxList');
Nv=numel(R); %number of vertebrae
corners=cell(1,Nv);
imshow(BW)
for i=1:Nv
B=A;
B(R(i).PixelIdxList)=1;
corners{i} = pgonCorners(B,4,40);
hold on
plot( corners{i}(:,2),corners{i}(:,1),'ro','MarkerFaceColor','m');
hold off
end
heights=cellfun(@(c) mean(c(:,1)),corners);
[heights,idx]=sort(heights);
corners=corners(idx);