r/learnpython • • Jun 27 '24

3D Rotation

Hello all,

I am trying to do a rotation about the x-axis. I'm finished with the code, but trying to find an alternative that could reduce the computation timing. I have almost 200 images and it took me about 1 hour to calculate them. Yes, it is a lot, but I need them for analysis and this is the least number of images that I could get (some of them have more than 300 images).

I included the function code here. Any suggestions?

Thanks

def rotate_G(mask, Angle):
    
     # Stack all rows together into a 3D array
     mask_xyz = np.repeat(mask[:, :, np.newaxis], max(mask.shape[0], mask.shape[1]), axis=2)
    
     # Rotate all rows at once around x-axis
     rotated_mask = rotate(mask_xyz, -Angle, axes=(1,2), reshape=False, order=1)
    
     # Extract the midline
     mid_z = rotated_mask.shape[2] // 2
     midline_z = rotated_mask[:, :, mid_z]
    
     # Convert to array
     projected_mask = np.array(midline_z)
    
     return projected_mask
1 Upvotes

14 comments sorted by

View all comments

Show parent comment

1

u/slittyslit Jun 27 '24

I did that before, but the interpolation got worse when the degree was close to 90 or 270. Initially, I used a rotation matrix, but calculating only y, because x is just the same. But the interpolation is hard as I need to make 600 pixels of data out of 3 or 4 pixels in a row. This makes me use scipy. rotation

1

u/Ok_Tea_7319 Jun 27 '24

Did you try the higher order 2D interpolator? scipy.interpolate.CloughTocher2DInterpolator

1

u/slittyslit Jun 27 '24 edited Jun 27 '24

I just tried to do that, and it seems to have a similar problem with angle close to 90 or 270. And it takes a bit longer compare to the current code, like 4-5 seconds longer per image.