Skip to content

Instantly share code, notes, and snippets.

@qaz10102030
Last active January 25, 2022 06:46
Show Gist options
  • Save qaz10102030/1f2961f62a37a084a309db177a202031 to your computer and use it in GitHub Desktop.
Save qaz10102030/1f2961f62a37a084a309db177a202031 to your computer and use it in GitHub Desktop.
# %%
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from spectral.io import envi
def read_rgb_ir(folder, key):
r = np.array(Image.open(f"{folder}/{key}_R.tif"))
g = np.array(Image.open(f"{folder}/{key}_G.tif"))
b = np.array(Image.open(f"{folder}/{key}_B.tif"))
ir = np.array(Image.open(f"{folder}/{key}_IR.tif"))
return np.stack([r, g, b, ir], 2).astype("f")
# %%
folder = r"PATH_FOR_FORDER"
sample = read_rgb_ir(folder, "sample")
white = read_rgb_ir(folder, "white")
dark = read_rgb_ir(folder, "dark")
ref = (sample - dark) / (white - dark)
meta = {"default bands": [0, 1, 2], "wavelength": [460, 550, 740, 850]}
envi.save_image(
"sample.hdr", sample, force=True, ext="raw", interleave="bil", metadata=meta
)
envi.save_image(
"sample_RT.hdr", ref, force=True, ext="raw", interleave="bil", metadata=meta
)
# %%
plt.figure()
for i in range(4):
ax = plt.subplot(2, 2, i + 1)
ax.axis("off")
ax.imshow(ref[..., i], "gray")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment