Skip to content

Instantly share code, notes, and snippets.

@qaz10102030
Created August 20, 2019 12:46
Show Gist options
  • Save qaz10102030/8356bb88064da0841403794a39403cc2 to your computer and use it in GitHub Desktop.
Save qaz10102030/8356bb88064da0841403794a39403cc2 to your computer and use it in GitHub Desktop.
import time
import numpy as np
def Algorithm(cube, tar, num, off_set=0):
[w, h, di] = cube.shape
flatten_array = cube.reshape(w * h, di).T
result_bands = []
for i in range(num):
result = [] # local result before sort
for j in range(di):
T = result_bands.copy()
if j in T:
result.append(np.iinfo(int).max)
continue
T.append(j)
A = np.delete(flatten_array, T, 0)
R = np.dot(A, A.T) / (w * h)
inv_R = np.linalg.inv(R)
B = np.delete(tar, T)
result.append(float(1 / np.dot(np.dot(B.T, inv_R), B)))
result_bands.append(np.argmin(result))
result_cube = np.zeros((w, h, num))
for i, v in enumerate(result_bands):
result_cube[:, :, i] = cube[:, :, v]
return result_cube, np.array(result_bands) + off_set
cube = np.random.random_sample((450, 700, 150)).astype(np.float32)
start_time = time.time()
sb_new_cube, sb_bands = Algorithm(cube, cube[200, 100, :], 3, off_set=50)
print('Elapes time:', time.time() - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment