from spectral import * import numpy as np import cv2 as cv from matplotlib import pyplot as plt import matplotlib.animation as animation import sys; import os; import os.path; import math def unit_vector(vector): """ Returns the unit vector of the vector. """ return vector / np.linalg.norm(vector) def angle_between(v1, v2): """ Returns the angle in radians between vectors 'v1' and 'v2':: >>> angle_between((1, 0, 0), (0, 1, 0)) 1.5707963267948966 >>> angle_between((1, 0, 0), (1, 0, 0)) 0.0 >>> angle_between((1, 0, 0), (-1, 0, 0)) 3.141592653589793 """ v1_u = unit_vector(v1) v2_u = unit_vector(v2) return np.arccos(np.clip(np.dot(v1_u, v2_u), -1.0, 1.0)) def anglemap_to_hsv(anglemap): hsv = np.zeros((anglemap.shape[0],anglemap.shape[1],3), np.uint8) degrees = np.rad2deg(anglemap) hue = (degrees / 2.0)+90 # normalizalas hue_norm = cv.normalize(hue, 0,179, norm_type=cv.NORM_MINMAX) # hsv[:,:,0] = hue_norm.astype(np.uint8) # hue.astype(np.uint8) hsv[:,:,1] = 255 hsv[:,:,2] = 255 return hsv def spectral_angle_mapping(img): # accumulate the spectral intensity for each pixels and the higher # will be the referrence accumulated_img = np.zeros(img.shape[:2], dtype=np.float32) accum_max = 0; kernel = np.ones((img.shape[0]//3, 6), np.uint16) for b in range(img.shape[2]): white_ref = cv.erode(img[:,:,b], kernel) img_avg = white_ref / img.shape[2] #print(img_avg.shape) accumulated_img = cv.add(accumulated_img, img_avg[:,:].astype(np.float32)) _, accum_max, _, accum_max_pos = cv.minMaxLoc(accumulated_img) max_pos_spectral_vector = img[accum_max_pos[0], accum_max_pos[1], :] #print(max_pos_spectral_vector.shape) print(accum_max, accum_max_pos) anglemap = np.zeros(img.shape[:2], dtype=np.float32) for y in range(img.shape[1]): for x in range(img.shape[0]): spectral_vector = img[x,y,:] #print(spectral_vector.shape) spectral_angle = angle_between(np.ravel(max_pos_spectral_vector),np.ravel( spectral_vector)) anglemap[x,y] = spectral_angle return anglemap, accum_max_pos def drawing_reference_point(img, point): cv.circle(img , point, 40, (255), 9) cv.line(img, (point[0]-5, point[1]), (point[0]+5, point[1]), (255),3) cv.line(img, (point[0], point[1]-5), (point[0], point[1]+5), (255),3) # -------- main ------------------- hyperspectral_hdr_file = os.path.basename(sys.argv[1]); img = open_image(sys.argv[1]); print(img); #arr = img.load(); #print arr.info(); #print img.bands(); width = img.shape[0]; height = img.shape[1]; nbands = img.shape[2]; print(nbands); anglemap, max_pos = spectral_angle_mapping(img) norm_anglemap = cv.normalize(anglemap, 0,255, norm_type=cv.NORM_MINMAX) #maxPos = cv.normalize(maxPos, 0,255, norm_type=cv.NORM_MINMAX) #maximage = cv.normalize(maximage, 0,255, norm_type=cv.NORM_MINMAX) #maximage = np.uint8(maximage); #maxPos = np.uint8(maxPos) #bgrImage = cv.cvtColor(np.uint8(hsvImage), cv.COLOR_HSV2BGR) #cv.circle(norm_anglemap, max_pos, 40, (255), 15) drawing_reference_point(norm_anglemap, max_pos) cv.imwrite('norm_anglemap.png', norm_anglemap) hsvImage = anglemap_to_hsv(anglemap) bgrImage = cv.cvtColor(np.uint8(hsvImage), cv.COLOR_HSV2BGR) #cv.imwrite('hsvImage.png', hsvImage) cv.imwrite('anglemap_bgr.png',bgrImage) while(1): cv.imshow('anglemap_bgr', bgrImage) k = cv.waitKey(5) & 0xFF if k == 27: break cv.destroyAllWindows()