Skip to content

Instantly share code, notes, and snippets.

@huangziwei
Created May 2, 2017 15:42
Show Gist options
  • Save huangziwei/8e8bc78c33cdc1f067d23ccb8006124d to your computer and use it in GitHub Desktop.
Save huangziwei/8e8bc78c33cdc1f067d23ccb8006124d to your computer and use it in GitHub Desktop.
rotate coordinates of rois
def rotate_roi(d):
ang_deg = d['wParamsNum'][31] # ratoate angle (degree)
ang_rad = ang_deg * np.pi / 180 # ratoate angle (radian)
d_rois = d['ROIs']
d_rois_rot = scipy.ndimage.interpolation.rotate(d_rois, ang_deg)
(shift_x, shift_y) = 0.5 * (np.array(d_rois_rot.shape) - np.array(d_rois.shape))
(cx, cy) = 0.5 * np.array(d_rois.shape)
labels = np.unique(d['ROIs'])[:-1]
px = [np.vstack(np.where(d['ROIs'] == i)).T[:, 0].mean() for i in labels]
py = [np.vstack(np.where(d['ROIs'] == i)).T[:, 1].mean() for i in labels]
px -= cx
py -= cy
xn = px * np.cos(ang_rad) - py*np.sin(ang_rad)
yn = px * np.sin(ang_rad) + py*np.cos(ang_rad)
xn += (cx + shift_x)
yn += (cy + shift_y)
return d_rois_rot, np.vstack([xn, yn]).T
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment