I am thinking the best way to do this will be analog. What i mean is just choose two sets of coordinates, compute their slope and apply the rotation yourself. It will be the least painful if you don't need to repeat this frequently.
Doing this autonomously will be quite a bit more difficult. There are ways to get it done, but guarenteeing good performance over your analog work will be difficult. Here is the way to do this analog:
Now to do this autonomouly will require some work. You will need to apply an image processing technique to segment the objects present, and identify the spine. You'll need to play around with the methods to see which one works the best. Here are some ideas that I have:
- Use a hough transform,this algorithm identifies lines in an image(see: https://www.mathworks.com/help/images/ref/hough.html). Locate the longest line(which should be the spine, calculate its slope and perform the rotation). Try processing without the image pre processing ans see if you can detect the spine.
[H,theta,rho] = hough(BW_final);
P = houghpeaks(H,45,'threshold',ceil(0.1*max(H(:))));
lines = houghlines(BW_im,theta,rho,P,'FillGap',5,'MinLength',7);
2. Use Haar like features
All of these methods except 4, will require you to parse the output and figure out the slope of the spine, then apply the rotation. A Haar like feature(just google these), can be used to identify objects. Here, we will construct one for the spine, and rotate it in intervals of 5 degrees(you can change this). The Haar like features will convolute the image, and whichever angle yields the highest value(from the convolution) will give a +-2.5 degrees on your spine angle. The following block of code explains how to use a Haar like feature and the logic to determine the rotation.
I hope this answer is not overbearing, i just like medical imaging problems and think they're fun to solve.
Hope this helps,