|
1 | 1 | import numpy as np |
2 | 2 | from skimage import io |
3 | 3 | import cv2 |
4 | | -from scipy.interpolate import splprep, splev, interp1d |
5 | 4 |
|
6 | 5 |
|
7 | | -def load_image(filename): |
| 6 | +def load_image(filename: str) -> np.ndarray: |
8 | 7 | """ |
9 | 8 | Given the filename of a TIFF, creates numpy array with pixel intensities |
10 | 9 |
|
11 | | - :param filename: |
12 | | - :return: |
13 | | - """ |
14 | | - |
15 | | - # img = np.array(Image.open(filename), dtype=np.float64) |
16 | | - # img[img == 0] = np.nan |
17 | | - return io.imread(filename).astype(float) |
18 | | - |
19 | | - |
20 | | -def offset_coordinates(roi, offsets): |
21 | | - """ |
22 | | - Reads in coordinates, adjusts according to offsets |
23 | | -
|
24 | | - :param roi: two column array containing x and y coordinates. e.g. coors = np.loadtxt(filename) |
25 | | - :param offsets: array the same length as coors. Direction? |
26 | | - :return: array in same format as coors containing new coordinates |
| 10 | + Args: |
| 11 | + filename: full path to the file to import (including extension) |
27 | 12 |
|
28 | | - To save this in a fiji readable format run: |
29 | | - np.savetxt(filename, newcoors, fmt='%.4f', delimiter='\t') |
| 13 | + Returns: |
| 14 | + A numpy array of the image |
30 | 15 |
|
31 | 16 | """ |
32 | 17 |
|
33 | | - # Calculate gradients |
34 | | - xcoors = roi[:, 0] |
35 | | - ycoors = roi[:, 1] |
36 | | - ydiffs = np.diff(ycoors, prepend=ycoors[-1]) |
37 | | - xdiffs = np.diff(xcoors, prepend=xcoors[-1]) |
38 | | - grad = ydiffs / xdiffs |
39 | | - tangent_grad = -1 / grad |
40 | | - |
41 | | - # Offset coordinates |
42 | | - xchange = ((offsets ** 2) / (1 + tangent_grad ** 2)) ** 0.5 |
43 | | - ychange = xchange / abs(grad) |
44 | | - newxs = xcoors + np.sign(ydiffs) * np.sign(offsets) * xchange |
45 | | - newys = ycoors - np.sign(xdiffs) * np.sign(offsets) * ychange |
46 | | - newcoors = np.swapaxes(np.vstack([newxs, newys]), 0, 1) |
47 | | - return newcoors |
| 18 | + return io.imread(filename).astype(float) |
48 | 19 |
|
49 | 20 |
|
50 | | -def make_mask(shape, roi): |
| 21 | +def make_mask(shape: tuple, roi: np.ndarray) -> np.ndarray: |
51 | 22 | return cv2.fillPoly(np.zeros(shape) * np.nan, [np.int32(roi)], 1) |
52 | | - |
53 | | - |
54 | | -def spline_roi(roi, periodic=True, s=0): |
55 | | - """ |
56 | | - Fits a spline to points specifying the coordinates of the cortex, then interpolates to pixel distances |
57 | | -
|
58 | | - :param roi: |
59 | | - :return: |
60 | | - """ |
61 | | - |
62 | | - # Append the starting x,y coordinates |
63 | | - if periodic: |
64 | | - x = np.r_[roi[:, 0], roi[0, 0]] |
65 | | - y = np.r_[roi[:, 1], roi[0, 1]] |
66 | | - else: |
67 | | - x = roi[:, 0] |
68 | | - y = roi[:, 1] |
69 | | - |
70 | | - # Fit spline |
71 | | - tck, u = splprep([x, y], s=s, per=periodic) |
72 | | - |
73 | | - # Evaluate spline |
74 | | - xi, yi = splev(np.linspace(0, 1, 10000), tck) |
75 | | - |
76 | | - # Interpolate |
77 | | - return interp_roi(np.vstack((xi, yi)).T, periodic=periodic) |
78 | | - |
79 | | - |
80 | | -def interp_roi(roi, periodic=True): |
81 | | - """ |
82 | | - Interpolates coordinates to one pixel distances (or as close as possible to one pixel) |
83 | | - Linear interpolation |
84 | | -
|
85 | | - :param roi: |
86 | | - :return: |
87 | | - """ |
88 | | - |
89 | | - if periodic: |
90 | | - c = np.append(roi, [roi[0, :]], axis=0) |
91 | | - else: |
92 | | - c = roi |
93 | | - |
94 | | - # Calculate distance between points in pixel units |
95 | | - distances = ((np.diff(c[:, 0]) ** 2) + (np.diff(c[:, 1]) ** 2)) ** 0.5 |
96 | | - distances_cumsum = np.r_[0, np.cumsum(distances)] |
97 | | - total_length = sum(distances) |
98 | | - |
99 | | - # Interpolate |
100 | | - fx, fy = interp1d(distances_cumsum, c[:, 0], kind='linear'), interp1d(distances_cumsum, c[:, 1], kind='linear') |
101 | | - positions = np.linspace(0, total_length, int(round(total_length))) |
102 | | - xcoors, ycoors = fx(positions), fy(positions) |
103 | | - newpoints = np.c_[xcoors[:-1], ycoors[:-1]] |
104 | | - return newpoints |
0 commit comments