Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lensless/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,6 @@ def load_image(
if flip_lr:
img = np.fliplr(img)

if verbose:
print_image_info(img)

if bg is not None:

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

if verbose:
print_image_info(img)

return img


Expand Down Expand Up @@ -292,7 +292,7 @@ def load_psf(
else:
psf = load_image(
fp,
verbose=verbose,
verbose=False,
flip=flip,
flip_ud=flip_ud,
flip_lr=flip_lr,
Expand Down Expand Up @@ -377,6 +377,9 @@ def load_psf(
else:
psf = psf.astype(original_dtype)

if verbose:
print_image_info(psf)

if return_bg:
return psf, bg
else:
Expand Down
1 change: 1 addition & 0 deletions lensless/utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def plot_cross_section(
width = 2 * np.abs(first_crossing)
ax.axvline(x=-first_crossing, c="k", linestyle="--")
ax.axvline(x=+first_crossing, c="k", linestyle="--")
print(f"{plot_db_drop}dB width : {width} pixels")

ax.set_title("Cross-section")
ax.set_xlabel(f"-{plot_db_drop}dB width = {width}")
Expand Down
34 changes: 27 additions & 7 deletions scripts/measure/analyze_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,31 @@
type=str,
help="File name to save color correct bayer as RGB.",
)
@click.option(
"--save_auto",
is_flag=True,
help="Save autocorrelation instead of pop-up window.",
)
@click.option(
"--nbits",
default=None,
type=int,
help="Number of bits for output. Only used for Bayer data",
)
@click.option(
"--down",
default=1,
type=int,
help="Factor by which to downsample.",
)
@click.option(
"--back",
type=str,
help="File path for background image, e.g. for screen.",
)
def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, save, nbits, back):
def analyze_image(
fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, save, save_auto, nbits, down, back
):
assert fp is not None, "Must pass file path."

# initialize plotting axis
Expand All @@ -131,6 +144,7 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
red_gain=rg,
nbits_out=nbits,
return_float=False,
downsample=down,
)[0]
else:
img = load_image(
Expand All @@ -141,10 +155,10 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
red_gain=rg,
nbits_out=nbits,
back=back,
downsample=down,
)
if nbits is None:
nbits = int(np.ceil(np.log2(img.max())))

# plot RGB and grayscale
ax = plot_image(img, gamma=gamma, normalize=True, ax=ax_rgb[0])
ax.set_title("RGB")
Expand All @@ -167,8 +181,9 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
plot_cross_section(
img_grey, color="gray", plot_db_drop=width, ax=ax_gray[2], plot_width=plot_width
)
_, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
fig_auto, ax_cross = plt.subplots(ncols=3, nrows=1, num="RGB widths", figsize=(15, 5))
for i, c in enumerate(["r", "g", "b"]):
print(f"-- {c} channel")
ax, _ = plot_cross_section(
img[:, :, i],
color=c,
Expand All @@ -181,18 +196,18 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
ax.set_ylabel("")

elif lensless:

# plot autocorrelations and width
# -- grey
_, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
fig_auto, ax_auto = plt.subplots(ncols=4, nrows=2, num="Autocorrelations", figsize=(15, 5))
_, autocorr_grey = plot_autocorr2d(img_grey, ax=ax_auto[0][0])
print("-- grayscale")
plot_cross_section(
autocorr_grey, color="gray", plot_db_drop=width, ax=ax_auto[1][0], plot_width=plot_width
)
# -- rgb
for i, c in enumerate(["r", "g", "b"]):
_, autocorr_c = plot_autocorr2d(img[:, :, i], ax=ax_auto[0][i + 1])

print(f"-- {c} channel")
ax, _ = plot_cross_section(
autocorr_c,
color=c,
Expand All @@ -214,7 +229,12 @@ def analyze_image(fp, gamma, width, bayer, lens, lensless, bg, rg, plot_width, s
save_image(img, save_8bit, normalize=True)
print(f"\n8bit version saved to: {save_8bit}")

plt.show()
if save_auto:
auto_fp = os.path.join(os.path.dirname(fp), "autocorrelation.png")
fig_auto.savefig(auto_fp)
print(f"\nAutocorrelation saved to: {auto_fp}")
else:
plt.show()


if __name__ == "__main__":
Expand Down