<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount("/content/drive", force_remount=True)
# Load libraries
import numpy as np # numerical python for ND array
import matplotlib.pyplot as plt # to make plots and show images
import os
####### Read raw file 
def readDir(dir_path):
    myDir = os.listdir(dir_path)
    dark_hdr =[]
    dark_raw=[]
    white_hdr=[]
    white_raw=[]
    for file in myDir:
        if 'dark' in file.lower() and 'hdr' in file.lower():
            #dark_hdr = dir_path + '/' + file
            dark_hdr = os.path.join(dir_path, file)
        if 'dark' in file.lower() and 'raw' in file.lower():
            #dark_raw = dir_path + '/' + file
            dark_raw = os.path.join(dir_path, file)
        if 'white' in file.lower() and 'hdr' in file.lower():
            #white_hdr = dir_path + '/' + file
            white_hdr = os.path.join(dir_path, file)
        if 'white' in file.lower() and 'raw' in file.lower():
            #white_raw = dir_path + '/' + file
            white_raw = os.path.join(dir_path, file)
        if (not 'white' in file.lower()) and (not 'dark' in file.lower()) and 'hdr' in file.lower():
            #object_hdr = dir_path + '/' + file
            object_hdr = os.path.join(dir_path, file)
        if (not 'white' in file.lower()) and (not 'dark' in file.lower()) and 'raw' in file.lower():
            #object_raw = dir_path + '/' + file
            object_raw = os.path.join(dir_path, file)
    return object_raw, object_hdr, white_raw, white_hdr, dark_raw, dark_hdr  

##### Read HDR content
def read_hdr(hdr_path):
    f=open(hdr_path, "r")
    filelines = f.readlines()
    #print(filelines)
    f.close()
    bands = ''
    for fileline in filelines:
        #print(fileline)
        if 'samples' in fileline.lower():
            samples = int(fileline.replace('samples = ',''))
            #print(samples)
        if bands =='' and 'bands' in fileline.lower():
            bands = int(fileline.replace('bands = ',''))
        if 'lines' in fileline.lower():
            lines = int(fileline.replace('lines = ',''))
    # read waves   
    print(samples, bands, lines)
    n = 0
    for fileline in filelines:
        if 'th = {' in fileline.lower():
          #print(n)
          n1 = n+1
        n = n + 1
    n2 = n1 + bands
    waves = np.zeros(n2-n1,)
    n = 0
    for i in range(n1, n2):
        #print(filelines[i])
        waves[n] = float(filelines[i].replace(',',''))
        #print(waves[n])
        n = n + 1
    
    return samples, bands, lines, waves<jupyter_output><empty_output><jupyter_text>part 2Open ENVI from Japanese spectral camera<jupyter_code>path2 = '/content/drive/MyDrive/ASI/savedata/colorChecker.nh7'
fopen = open(path2, "rb")
raw = np.fromfile(fopen, dtype=np.uint16) #uint16 float32, 2bytes np.uint16
#count=spatial_pixels*sample_lines*spectral_bands
print(f"size: {raw.shape}")  #need to know the file size
fopen.close()<jupyter_output>size: (197918720,)<jupyter_text>Known the sensor size is 1280x1024, and the data type is unit16<jupyter_code>lines = 1024
samples = 1280
bands = int(197918720/1280/1024) # 151
spectral_image = np.reshape(raw, (lines, bands, samples))
print('spectral_image.shape: ', spectral_image.shape)
plt.rcParams['figure.dpi'] = 100 # Resolution
# show gray scale slice of 3D spectral image
plt.imshow(spectral_image[:,60,:], cmap='gray')
plt.show()<jupyter_output>spectral_image.shape:  (1024, 151, 1280)<jupyter_text>part 3Open ENVI from Kuopio microsurgery center<jupyter_code># Read wavelengths from hdr file
hdr_path = '/content/drive/MyDrive/ASI/savedata/Image of coin (Senop camera)/HSI_snapshot__20212101144836.hdr'
f=open(hdr_path, "r")
filelines = f.readlines()
print(filelines)
samples = 1024
lines = 1024
bands = 18
path3 = '/content/drive/MyDrive/ASI/savedata/Image of coin (Senop camera)/HSI_snapshot__20212101144836.dat'
fopen = open(path3, "rb")
raw = np.fromfile(fopen, dtype='>u2') #datatype 12
print(f"size: {raw.shape}")
fopen.close()
spectral_image = np.reshape(raw,(bands,lines,samples))

print('spectral_image.shape: ', spectral_image.shape)
plt.rcParams['figure.dpi'] = 100 # Resolution
# show gray scale slice of 3D spectral image
plt.imshow(spectral_image[1,:,:], cmap='gray')
plt.show()
plt.rcParams['figure.dpi'] = 100 # Resolution
# show gray scale slice of 3D spectral image
plt.imshow(spectral_image[4,:,:], cmap='gray')
plt.show()
x = [500.0,526.5,552.9,579.4,605.9,632.4,658.8,685.3,711.8,738.2,764.7,791.2,817.6,844.1,870.6,897.1,923.5,950.0]
plt.plot(x,spectral_image[:,500,500])
plt.show()<jupyter_output><empty_output><jupyter_text>part 4Save ENVI spectral image with interleave = bil/bip<jupyter_code># Read wavelengths from hdr file
hdr_path = '/content/drive/MyDrive/ASI/Lectures+Exercises/Lecture 2/Spectral images/Colorchecker 121 ms (for Freelook demo)/capture/Colorchecker.hdr'
f=open(hdr_path, "r")
filelines = f.readlines()
print(filelines)
[samples, bands, lines, waves] = read_hdr(hdr_path)
path4 = '/content/drive/MyDrive/ASI/Lectures+Exercises/Lecture 2/Spectral images/Colorchecker 121 ms (for Freelook demo)/capture/Colorchecker.raw'
fopen = open(path4, "rb")
raw = np.fromfile(fopen, dtype=np.uint16)
print(f"size: {raw.shape}")
fopen.close()

spectral_image = np.reshape(raw, (lines, bands, samples))
print('spectral_image.shape: ', spectral_image.shape)
plt.rcParams['figure.dpi'] = 150 # Resolution
# show gray scale slice of 3D spectral image
plt.imshow(spectral_image[:,10,:], cmap='gray')
plt.show()
rotated = np.rot90(spectral_image, k=1, axes=(1,2))
#rotated = spectral_image.swapaxes(1,2)
#copy the hdr header-file
import shutil
des = '/content/drive/MyDrive/ASI/savedata/Colorchecker 121/rotated.hdr'
shutil.copyfile(hdr_path, des)
print("File copied successfully.")
f=open(des, "r")
filelines = f.readlines()
f.close()
for fileline in filelines:
  if 'interleave' in fileline.lower():
    fileline = fileline.replace("BIL","BIP")
    print(fileline)
#print(filelines)
save_path = '/content/drive/MyDrive/ASI/savedata/Colorchecker 121/rotated.raw'
rotated.astype(np.uint16).tofile(save_path)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14776800260671227, 'fraction_numerical': 0.041544477028347994, 'mean_word_length': 3.9428341384863126, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '567898', 'license': 'MIT', 'n_tokens_mistral': 2404, 'n_tokens_neox': 2213, 'n_words': 491, 'path': 'task2(part2,3,4).ipynb', 'repo_name': 'RoseSarlake/spectral-image', 'size': '6138'}