Created
May 2, 2017 15:42
-
-
Save huangziwei/67079f6c46d24808deb9c7122d74a2cf to your computer and use it in GitHub Desktop.
rotate coordinates of rois
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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