Skip to content

Commit 0f3117e

Browse files
authored
Add downsampling option for analyzing autocorrelation.
2 parents 5c7f81c + 4e9e638 commit 0f3117e

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

lensless/utils/io.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ def load_image(
166166
if flip_lr:
167167
img = np.fliplr(img)
168168

169-
if verbose:
170-
print_image_info(img)
171-
172169
if bg is not None:
173170

174171
# if bg is float vector, turn into int-valued vector
@@ -204,6 +201,9 @@ def load_image(
204201
dtype = original_dtype
205202
img = img.astype(dtype)
206203

204+
if verbose:
205+
print_image_info(img)
206+
207207
return img
208208

209209

@@ -292,7 +292,7 @@ def load_psf(
292292
else:
293293
psf = load_image(
294294
fp,
295-
verbose=verbose,
295+
verbose=False,
296296
flip=flip,
297297
flip_ud=flip_ud,
298298
flip_lr=flip_lr,
@@ -377,6 +377,9 @@ def load_psf(
377377
else:
378378
psf = psf.astype(original_dtype)
379379

380+
if verbose:
381+
print_image_info(psf)
382+
380383
if return_bg:
381384
return psf, bg
382385
else:

lensless/utils/plot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ def plot_cross_section(
244244
width = 2 * np.abs(first_crossing)
245245
ax.axvline(x=-first_crossing, c="k", linestyle="--")
246246
ax.axvline(x=+first_crossing, c="k", linestyle="--")
247+
print(f"{plot_db_drop}dB width : {width} pixels")
247248

248249
ax.set_title("Cross-section")
249250
ax.set_xlabel(f"-{plot_db_drop}dB width = {width}")

scripts/measure/analyze_image.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,31 @@
100100
type=str,
101101
help="File name to save color correct bayer as RGB.",
102102
)
103+
@click.option(
104+
"--save_auto",
105+
is_flag=True,
106+
help="Save autocorrelation instead of pop-up window.",
107+
)
103108
@click.option(
104109
"--nbits",
105110
default=None,
106111
type=int,
107112
help="Number of bits for output. Only used for Bayer data",
108113
)
114+
@click.option(
115+
"--down",
116+
default=1,
117+
type=int,
118+
help="Factor by which to downsample.",
119+
)
109120
@click.option(
110121
"--back",
111122
type=str,
112123
help="File path for background image, e.g. for screen.",
113124
)
114-
def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, save, nbits, back):
125+
def analyze_image(
126+
fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, save, save_auto, nbits, down, back
127+
):
115128
assert fp is not None, "Must pass file path."
116129

117130
# initialize plotting axis
@@ -131,6 +144,7 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
131144
red_gain=rg,
132145
nbits_out=nbits,
133146
return_float=False,
147+
downsample=down,
134148
)[0]
135149
else:
136150
img = load_image(
@@ -141,10 +155,10 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
141155
red_gain=rg,
142156
nbits_out=nbits,
143157
back=back,
158+
downsample=down,
144159
)
145160
if nbits is None:
146161
nbits = int(np.ceil(np.log2(img.max())))
147-
148162
# plot RGB and grayscale
149163
ax = plot_image(img, gamma=gamma, normalize=True, ax=ax_rgb[0])
150164
ax.set_title("RGB")
@@ -167,8 +181,9 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
167181
plot_cross_section(
168182
img_grey, color="gray", plot_db_drop=width, ax=ax_gray[2], plot_width=plot_width
169183
)
170-
_, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
184+
fig_auto, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
171185
for i, c in enumerate(["r", "g", "b"]):
186+
print(f"-- {c} channel")
172187
ax, _ = plot_cross_section(
173188
img[:, :, i],
174189
color=c,
@@ -181,18 +196,18 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
181196
ax.set_ylabel("")
182197

183198
elif lensless:
184-
185199
# plot autocorrelations and width
186200
# -- grey
187-
_, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
201+
fig_auto, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
188202
_, autocorr_grey = plot_autocorr2d(img_grey, ax=ax_auto[0][0])
203+
print("-- grayscale")
189204
plot_cross_section(
190205
autocorr_grey, color="gray", plot_db_drop=width, ax=ax_auto[1][0], plot_width=plot_width
191206
)
192207
# -- rgb
193208
for i, c in enumerate(["r", "g", "b"]):
194209
_, autocorr_c = plot_autocorr2d(img[:, :, i], ax=ax_auto[0][i + 1])
195-
210+
print(f"-- {c} channel")
196211
ax, _ = plot_cross_section(
197212
autocorr_c,
198213
color=c,
@@ -214,7 +229,12 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
214229
save_image(img, save_8bit, normalize=True)
215230
print(f"\n8bit version saved to: {save_8bit}")
216231

217-
plt.show()
232+
if save_auto:
233+
auto_fp = os.path.join(os.path.dirname(fp), "autocorrelation.png")
234+
fig_auto.savefig(auto_fp)
235+
print(f"\nAutocorrelation saved to: {auto_fp}")
236+
else:
237+
plt.show()
218238

219239

220240
if __name__ == "__main__":

0 commit comments

Comments
 (0)