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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (C) 2023, Raspberry Pi Ltd
#
# ctt_cac.py - CAC (Chromatic Aberration Correction) tuning tool
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from ctt_dots_locator import find_dots_locations
# This is the wrapper file that creates a JSON entry for you to append
# to your camera tuning file.
# It calculates the chromatic aberration at different points throughout
# the image and uses that to produce a martix that can then be used
# in the camera tuning files to correct this aberration.
def pprint_array(array):
# Function to print the array in a tidier format
array = array
output = ""
for i in range(len(array)):
for j in range(len(array[0])):
output += str(round(array[i, j], 2)) + ", "
# Add the necessary indentation to the array
output += "\n "
# Cut off the end of the array (nicely formats it)
return output[:-22]
def plot_shifts(red_shifts, blue_shifts):
# If users want, they can pass a command line option to show the shifts on a graph
# Can be useful to check that the functions are all working, and that the sample
# images are doing the right thing
Xs = np.array(red_shifts)[:, 0]
Ys = np.array(red_shifts)[:, 1]
Zs = np.array(red_shifts)[:, 2]
Zs2 = np.array(red_shifts)[:, 3]
Zs3 = np.array(blue_shifts)[:, 2]
Zs4 = np.array(blue_shifts)[:, 3]
fig, axs = plt.subplots(2, 2)
ax = fig.add_subplot(2, 2, 1, projection='3d')
ax.scatter(Xs, Ys, Zs, cmap=cm.jet, linewidth=0)
ax.set_title('Red X Shift')
ax = fig.add_subplot(2, 2, 2, projection='3d')
ax.scatter(Xs, Ys, Zs2, cmap=cm.jet, linewidth=0)
ax.set_title('Red Y Shift')
ax = fig.add_subplot(2, 2, 3, projection='3d')
ax.scatter(Xs, Ys, Zs3, cmap=cm.jet, linewidth=0)
ax.set_title('Blue X Shift')
ax = fig.add_subplot(2, 2, 4, projection='3d')
ax.scatter(Xs, Ys, Zs4, cmap=cm.jet, linewidth=0)
ax.set_title('Blue Y Shift')
fig.tight_layout()
plt.show()
def shifts_to_yaml(red_shift, blue_shift, image_dimensions, output_grid_size=9):
# Convert the shifts to a numpy array for easier handling and initialise other variables
red_shifts = np.array(red_shift)
blue_shifts = np.array(blue_shift)
# create a grid that's smaller than the output grid, which we then interpolate from to get the output values
xrgrid = np.zeros((output_grid_size - 1, output_grid_size - 1))
xbgrid = np.zeros((output_grid_size - 1, output_grid_size - 1))
yrgrid = np.zeros((output_grid_size - 1, output_grid_size - 1))
ybgrid = np.zeros((output_grid_size - 1, output_grid_size - 1))
xrsgrid = []
xbsgrid = []
yrsgrid = []
ybsgrid = []
xg = np.zeros((output_grid_size - 1, output_grid_size - 1))
yg = np.zeros((output_grid_size - 1, output_grid_size - 1))
# Format the grids - numpy doesn't work for this, it wants a
# nice uniformly spaced grid, which we don't know if we have yet, hence the rather mundane setup
for x in range(output_grid_size - 1):
xrsgrid.append([])
yrsgrid.append([])
xbsgrid.append([])
ybsgrid.append([])
for y in range(output_grid_size - 1):
xrsgrid[x].append([])
yrsgrid[x].append([])
xbsgrid[x].append([])
ybsgrid[x].append([])
image_size = (image_dimensions[0], image_dimensions[1])
gridxsize = image_size[0] / (output_grid_size - 1)
gridysize = image_size[1] / (output_grid_size - 1)
# Iterate through each dot, and it's shift values and put these into the correct grid location
for red_shift in red_shifts:
xgridloc = int(red_shift[0] / gridxsize)
ygridloc = int(red_shift[1] / gridysize)
xrsgrid[xgridloc][ygridloc].append(red_shift[2])
yrsgrid[xgridloc][ygridloc].append(red_shift[3])
for blue_shift in blue_shifts:
xgridloc = int(blue_shift[0] / gridxsize)
ygridloc = int(blue_shift[1] / gridysize)
xbsgrid[xgridloc][ygridloc].append(blue_shift[2])
ybsgrid[xgridloc][ygridloc].append(blue_shift[3])
# Now calculate the average pixel shift for each square in the grid
for x in range(output_grid_size - 1):
for y in range(output_grid_size - 1):
xrgrid[x, y] = np.mean(xrsgrid[x][y])
yrgrid[x, y] = np.mean(yrsgrid[x][y])
xbgrid[x, y] = np.mean(xbsgrid[x][y])
ybgrid[x, y] = np.mean(ybsgrid[x][y])
# Next, we start to interpolate the central points of the grid that gets passed to the tuning file
input_grids = np.array([xrgrid, yrgrid, xbgrid, ybgrid])
output_grids = np.zeros((4, output_grid_size, output_grid_size))
# Interpolate the centre of the grid
output_grids[:, 1:-1, 1:-1] = (input_grids[:, 1:, :-1] + input_grids[:, 1:, 1:] + input_grids[:, :-1, 1:] + input_grids[:, :-1, :-1]) / 4
# Edge cases:
output_grids[:, 1:-1, 0] = ((input_grids[:, :-1, 0] + input_grids[:, 1:, 0]) / 2 - output_grids[:, 1:-1, 1]) * 2 + output_grids[:, 1:-1, 1]
|