Skip to content

Commit 44cd60a

Browse files
committed
Merge remote-tracking branch 'origin/bspline_integration'
2 parents d79eddf + 5826d7c commit 44cd60a

19 files changed

+1829
-0
lines changed
File renamed without changes.
File renamed without changes.

fit_routines/__init__.py

Whitespace-only changes.

fit_routines/b_spline_curve_fit.py

+383
Large diffs are not rendered by default.

fit_routines/params.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
import json
3+
import numpy as np
4+
5+
class fit_params:
6+
def __init__(
7+
self,
8+
json_string = None,
9+
):
10+
self.load_json(json_string)
11+
12+
def is_param(self, str):
13+
return str in self.__dict__.keys()
14+
15+
def get_param(self, str):
16+
return self.__dict__[str]
17+
18+
def add_param(self, str, val):
19+
self.__dict__[str] = val
20+
21+
def update_param(self, str, val):
22+
self.__dict__[str] = val
23+
24+
def json_dump(self):
25+
return json.dumps(self.__dict__, indent=4)
26+
27+
def load_json(self, json_str: str):
28+
self.__dict__ = json.loads(json_str)
29+
for key in self.__dict__.keys():
30+
if isinstance(self.__dict__[key], list):
31+
self.__dict__[key] = np.array(self.__dict__[key])
32+
33+
if __name__ == "__main__":
34+
fp = fit_params(
35+
1.0, residuals=np.array([0.6, 0.3, 0.2]), ts=np.array([0.2, 0.7, 1.0])
36+
)
37+
print(fp.json_dump())
38+
pass

reconstruction/basis_function.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
from numpy.polynomial import polynomial as P
3+
from numpy.polynomial import Polynomial
4+
import numpy as np
5+
import matplotlib.pyplot as plt
6+
7+
lut = [ [1],
8+
[1,1],
9+
[1,2,1],
10+
[1,3,3,1],
11+
[1,4,6,4,1],
12+
[1,5,10,10,5,1],
13+
[1,6,15,20,15,6,1]]
14+
15+
def binomial(n : int, k: int):
16+
while n >= len(lut):
17+
s = len(lut)
18+
nextRow = [1] * s
19+
for i in range(1, s):
20+
nextRow[i] = lut[s-1][i-1] + lut[s-1][i]
21+
nextRow.append(1)
22+
lut.append(nextRow)
23+
return lut[n][k]
24+
25+
def bezier_basis(n):
26+
basis = []
27+
fig, ax = plt.subplots()
28+
for k in range (0, n+1):
29+
print(f"n: {n} k: {k}")
30+
term1 = P.polypow([1, -1], n-k)
31+
term2 = P.polypow([0, 1], k)
32+
mult = P.polymul([binomial(n,k)], term1)
33+
mult2 = P.polymul(mult, term2)
34+
p = Polynomial(coef=mult2)
35+
m = p.linspace(100, [0, 1])
36+
ax.plot(m[0], m[1])
37+
basis.append(mult2)
38+
print(basis)
39+
plt.show()
40+
return basis
41+
42+
if __name__ == "__main__":
43+
bezier_basis(12)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import cv2
4+
import sys
5+
import numpy as np
6+
7+
""" Create mask images assuming data formatted as "labelled_id.png" is available at
8+
one folder level below the path passed to the script
9+
"""
10+
11+
LABELS = {
12+
(69, 244, 139): "spur",
13+
(255, 255, 255): "trunk",
14+
(87, 192, 255): "branch",
15+
(0, 0, 244): "leader",
16+
}
17+
18+
# (0 0 0): background tree,
19+
# (255, 90, 67): sky,
20+
# (255 0 0) poles,
21+
# (0 0 244) leader,
22+
23+
def generate_masks(files, output_dir):
24+
for file in files:
25+
img = cv2.imread(file)
26+
# black mask
27+
curr_mask = np.zeros(img.shape[:2], dtype=np.uint8)
28+
for color in LABELS.keys():
29+
color_arr = np.array(color)
30+
31+
mask = cv2.inRange(img, color_arr - 10, color_arr + 10)
32+
# or with previous mask
33+
curr_mask = cv2.bitwise_or(mask, curr_mask)
34+
start_index = file.find("labelled") + len("labelled")
35+
# mask_name = f"mask_{LABELS[color]}_{file[start_index:]}"
36+
# print(f"Saving mask {mask_name} in {output_dir}")
37+
# cv2.imwrite(os.path.join(output_dir, mask_name), mask)
38+
mask_name = f"mask_all_{file[start_index:]}"
39+
cv2.imwrite(os.path.join(output_dir, mask_name), curr_mask)
40+
print(f"Saving mask {mask_name} in {output_dir}")
41+
42+
if __name__ == '__main__':
43+
dir_path = sys.argv[1]
44+
output_dir = dir_path
45+
os.makedirs(output_dir, exist_ok=True)
46+
for subfolder in os.listdir(dir_path):
47+
subdir_path = os.path.join(dir_path, subfolder)
48+
files = [os.path.join(subdir_path, x) for x in os.listdir(subdir_path) if x.endswith(".png") and x.startswith("labelled")]
49+
generate_masks(files, os.path.join(output_dir, subfolder))
50+

reconstruction/image_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def get_contiguous_distance(pts, matches, vec):
2+
current_start = None
3+
dist = 0
4+
for i, match in enumerate(matches):
5+
if current_start is None and match:
6+
current_start = i
7+
elif current_start is not None and not match:
8+
offsets = pts[current_start + 1 : i] - pts[current_start : i - 1]
9+
dist += (offsets * vec).sum()
10+
current_start = None
11+
if current_start is not None:
12+
i = len(matches)
13+
offsets = pts[current_start + 1 : i] - pts[current_start : i - 1]
14+
dist += (offsets * vec).sum()
15+
16+
return dist

0 commit comments

Comments
 (0)