-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_tools.py
55 lines (45 loc) · 1.77 KB
/
tree_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from scipy import spatial
import random
import numpy as np
def set_tree(values):
"""Set KDTree for given values
Args:
values: Values to set on tree
Returns:
KDTree
Raises:
None
"""
return spatial.KDTree(values)
def find_best_match(target_res : tuple,
mos_template : np.ndarray,
tree,
rand_choice : int=1) -> np.ndarray:
"""Find the tree index for the best image match
Loop through each pixel in the low-res reference image and find the index
of the image that has the closest colour match to the mosaic template.
The 'match' variable returns a list of the closest rand_choice matches
and one is randomly chosen - this is to reduce repeat images for similar
colours that are close to each other. This number would need to be smaller
than the number of images available.
Args:
target_res: Target resolution as integer tuple of number of source
images in format (height, width)
mos_template: The mosaic template image as a array
tree: Spatial KDTree that contains the source image rgb means
rand_choice: How many of the closest match images to randomly choose
from
Returns:
Numpy two-dimensional array, where each entry corresponds to a tree
index for each source image for the mosaic
Raises:
None
"""
image_idx = np.zeros(target_res, dtype=np.uint32)
for i in range(target_res[0]):
for j in range(target_res[1]):
template = mos_template[i, j]
match = tree.query(template, k=rand_choice)
pick = random.randint(0, rand_choice-1)
image_idx[i, j] = match[1][pick]
return image_idx