Skip to content

Commit ad2387a

Browse files
committed
Add downsample option.
1 parent dae8917 commit ad2387a

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

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,
@@ -376,6 +376,9 @@ def load_psf(
376376
bg /= max_val
377377
else:
378378
psf = psf.astype(original_dtype)
379+
380+
if verbose:
381+
print_image_info(psf)
379382

380383
if return_bg:
381384
return psf, bg

lensless/utils/plot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def plot_cross_section(
239239
ax.set_title(f"-{plot_db_drop}dB width = {width}")
240240
ax.axvline(x=zero_crossings[0], c="k", linestyle="--")
241241
ax.axvline(x=zero_crossings[-1], c="k", linestyle="--")
242+
print(f"{plot_db_drop}dB width : {width} pixels")
242243
else:
243244
warnings.warn(
244245
"Width could not be determined. Did not detect two -{} points : {}".format(

scripts/measure/analyze_image.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,29 @@
100100
type=str,
101101
help="File name to save color correct bayer as RGB.",
102102
)
103+
@click.option(
104+
"--save_auto",
105+
type=str,
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(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, save, save_auto, nbits, down, back):
115126
assert fp is not None, "Must pass file path."
116127

117128
# initialize plotting axis
@@ -131,6 +142,7 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
131142
red_gain=rg,
132143
nbits_out=nbits,
133144
return_float=False,
145+
downsample=down
134146
)[0]
135147
else:
136148
img = load_image(
@@ -141,10 +153,10 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
141153
red_gain=rg,
142154
nbits_out=nbits,
143155
back=back,
156+
downsample=down
144157
)
145158
if nbits is None:
146159
nbits = int(np.ceil(np.log2(img.max())))
147-
148160
# plot RGB and grayscale
149161
ax = plot_image(img, gamma=gamma, normalize=True, ax=ax_rgb[0])
150162
ax.set_title("RGB")
@@ -167,8 +179,9 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
167179
plot_cross_section(
168180
img_grey, color="gray", plot_db_drop=width, ax=ax_gray[2], plot_width=plot_width
169181
)
170-
_, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
182+
fig_auto, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
171183
for i, c in enumerate(["r", "g", "b"]):
184+
print(f"-- {c} channel")
172185
ax, _ = plot_cross_section(
173186
img[:, :, i],
174187
color=c,
@@ -181,18 +194,18 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
181194
ax.set_ylabel("")
182195

183196
elif lensless:
184-
185197
# plot autocorrelations and width
186198
# -- grey
187-
_, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
199+
fig_auto, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
188200
_, autocorr_grey = plot_autocorr2d(img_grey, ax=ax_auto[0][0])
201+
print(f"-- grayscale")
189202
plot_cross_section(
190203
autocorr_grey, color="gray", plot_db_drop=width, ax=ax_auto[1][0], plot_width=plot_width
191204
)
192205
# -- rgb
193206
for i, c in enumerate(["r", "g", "b"]):
194207
_, autocorr_c = plot_autocorr2d(img[:, :, i], ax=ax_auto[0][i + 1])
195-
208+
print(f"-- {c} channel")
196209
ax, _ = plot_cross_section(
197210
autocorr_c,
198211
color=c,
@@ -217,7 +230,14 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
217230
save_image(img, save_8bit, normalize=True)
218231
print(f"\n8bit version saved to: {save_8bit}")
219232

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

222242

223243
if __name__ == "__main__":

0 commit comments

Comments
 (0)