def normalizeElevations(dems): avgElevation = {} validIndicesDict = {} ranges = {} for burnName, dem in dems.items(): validIndices = np.where(np.isfinite(dem)) validIndicesDict[burnName] = validIndices validPixels = dem[validIndices] avgElevation[burnName] = np.mean(validPixels) ranges[burnName] = validPixels.max() - validPixels.min() # vis = dem.copy() # vis[validIndices] = 42 # plt.imshow(dem) # plt.figure("valid") # plt.imshow(vis) # plt.show() maxRange = max(ranges.values()) results = {} for burnName, dem in dems.items(): validIndices = validIndicesDict[burnName] # print(validIndices) validPixels = dem[validIndices] # print('valid pixels size:', validPixels.size) normed = util.normalize(validPixels) blank = np.zeros_like(dem, dtype=np.float32) thisRange = ranges[burnName] scaleFactor = thisRange / maxRange blank[validIndices] = scaleFactor * normed # print('scaleFactor is ', scaleFactor) # plt.imshow(blank) # plt.show() results[burnName] = blank return results
def normalizeNonElevations(nonDems): splitIndices = [0] validPixelsList = [] validIndicesList = [] names = list(nonDems.keys()) for name in names: layer = nonDems[name] validIndices = np.where(np.isfinite(layer)) validPixels = layer[validIndices] validPixelsList += validPixels.tolist() splitIndices.append(splitIndices[-1] + len(validPixels)) validIndicesList.append(validIndices) arr = np.array(validPixelsList) normed = util.normalize(arr) splitIndices = splitIndices[1:] splitBackUp = np.split(normed, splitIndices) results = {} for name, validIndices, normedPixels in zip(names,validIndicesList,splitBackUp): src = nonDems[name] img = np.zeros_like(src, dtype=np.float32) img[validIndices] = normedPixels results[name] = img return results
def normalizeNonElevations(nonDems): splitIndices = [0] validPixelsList = [] validIndicesList = [] names = list(nonDems.keys()) for name in names: layer = nonDems[name] validIndices = np.where(np.isfinite(layer)) validPixels = layer[validIndices] # print('valid indices:', validIndices) # print('len of valid pixels', validPixels.shape, len(validPixels)) # vis = np.zeros_like(layer) # vis[validIndices] = 42 # plt.figure('before '+name) # plt.imshow(layer) # plt.figure('valid '+name) # plt.imshow(vis) # plt.show() validPixelsList += validPixels.tolist() splitIndices.append(splitIndices[-1] + len(validPixels)) validIndicesList.append(validIndices) # now layers.shape is (nburns, height, width) arr = np.array(validPixelsList) # print('array is', arr, arr.min(), arr.max()) normed = util.normalize(arr) # print(normed) # print('split indices', splitIndices) splitIndices = splitIndices[1:] # print('split indices', splitIndices) splitBackUp = np.split(normed, splitIndices) # print('split back up:', splitBackUp.shape) results = {} for name, validIndices, normedPixels in zip(names, validIndicesList, splitBackUp): # print(name, validIndices, normedPixels) src = nonDems[name] img = np.zeros_like(src, dtype=np.float32) img[validIndices] = normedPixels results[name] = img # if normedPixels.size > 0: # print('min and max of ', name, normedPixels.min(), normedPixels.max()) # plt.figure('normed'+name) # plt.imshow(img) # plt.show() return results
def median_images(images): print( "---------------------- INITIALISATION DES CALCULS POUR RECUPERER LE BACKGROUND 2/4 ------------------------------------------" ) images_normalized = normalize(images) img_dest = Image.new("RGB", images[0].size) dest = img_dest.load() N = len(images) images_pixels = [] for img in images_normalized: images_pixels.append(img.load()) printProgressBar(0, images_normalized[0].size[1], prefix='Progress:', suffix='Complete', length=50) for y in range(images_normalized[0].size[1]): for x in range(images_normalized[0].size[0]): rp = [] #listes pour les valeurs des pixels gp = [] bp = [] for i in range(N): #pour travailler sur chaque pixel de chaque img pix = images_pixels[i] r1, g1, b1 = pix[x, y] rp.append(r1) #valeurs rouge de chaque img gp.append(g1) #valeurs verte de chaque img bp.append(b1) #valeurs bleu de chaque img rp.sort() #trie des listes gp.sort() bp.sort() dest[x, y] = rp[N // 2], gp[N // 2], bp[N // 2] #la valeur med de chaque couleur printProgressBar(y + 1, images_normalized[0].size[1], prefix='Progress:', suffix='Complete', length=50) print( "---------------------- FIN DES CALCULS POUR RECUPERER LE BACKGROUND 2/4 ------------------------------------------" ) print() return img_dest
def binary_merge(bkg_img, item,img_result): _images = normalize([bkg_img,item[0],item[1],img_result]) bkg_pixels = _images[0].load() img_pixels = _images[1].load() mask_img = _images[2].load() width = _images[0].size[0] height = _images[0].size[1] img_result_pixels = _images[3].load() for x in range(width): for y in range(height): r1,g1,b1 = bkg_pixels[x,y] r2,g2,b2 = img_pixels[x,y] if mask_img[x,y] == (0,0,0): img_result_pixels[x,y] = (r2,g2,b2)
def get_mask_old(bkg_img, img, img_result): _images = normalize([bkg_img, img, img_result]) bkg_pixels = _images[0].load() img_pixels = _images[1].load() img_result_pixels = _images[2].load() width = _images[0].size[0] height = _images[0].size[1] for x in range(width): for y in range(height): r1, g1, b1 = bkg_pixels[x, y] r2, g2, b2 = img_pixels[x, y] is_background = abs(r1 - r2) < MASK_TRESHOLD and abs( g1 - g2) < MASK_TRESHOLD and abs(b1 - b2) < MASK_TRESHOLD if not is_background: img_result_pixels[x, y] = (0, 0, 0)
def get_mask(bkg_img, img): mask_image = create_image((255, 255, 255), bkg_img) _images = normalize([bkg_img, img, mask_image]) bkg_pixels = _images[0].load() img_pixels = _images[1].load() img_mask_pixels = _images[2].load() width = _images[0].size[0] height = _images[0].size[1] for x in range(width): for y in range(height): r1, g1, b1 = bkg_pixels[x, y] r2, g2, b2 = img_pixels[x, y] is_background = abs(r1 - r2) < MASK_TRESHOLD and abs( g1 - g2) < MASK_TRESHOLD and abs(b1 - b2) < MASK_TRESHOLD if not is_background: img_mask_pixels[x, y] = (0, 0, 0) return mask_image
def calculateWeatherMetrics(dataset): '''Return a dictionary mapping from (burnName, date) id's to a dictionary of named weather metrics.''' metrics = {} for burnName, date in dataset.getUsedBurnNamesAndDates(): wm = dataset.data.getWeather(burnName, date) # print('for the Day', burnName, date) # print('weather matrix is', wm) precip = totalPrecipitation(wm) temp = maximumTemperature1(wm) temp2 = maximumTemperature2(wm) hum = averageHumidity(wm) winds = windMetrics(wm) entry = [precip, temp, temp2, hum] + winds metrics[(burnName, date)] = entry # now normalize all of them # ensure we keep order ids = list(metrics.keys()) arr = np.array([metrics[i] for i in ids]) normed = util.normalize(arr, axis=0) metrics = {i: nums for (i, nums) in zip(ids, normed)} return metrics
def normalizeElevations(dems): avgElevation = {} validIndicesDict = {} ranges = {} for locName, dem in dems.items(): validIndices = np.where(np.isfinite(dem)) validIndicesDict[locName] = validIndices validPixels = dem[validIndices] avgElevation[locName] = np.mean(validPixels) ranges[locName] = validPixels.max()-validPixels.min() maxRange = max(ranges.values()) results = {} for locName, dem in dems.items(): validIndices = validIndicesDict[locName] validPixels = dem[validIndices] normed = util.normalize(validPixels) blank = np.zeros_like(dem, dtype=np.float32) thisRange = ranges[locName] scaleFactor = thisRange/maxRange blank[validIndices] = scaleFactor * normed results[locName] = blank return results
def createCanvases(dataset): result = {} for burnName, date in dataset.getUsedBurnNamesAndDates(): burn = dataset.data.burns[burnName] day = dataset.data.getDay(burnName, date) h, w = day.startingPerim.shape # canvas = np.zeros((h,w,3), dtype=np.uint8) normedDEM = util.normalize(burn.layers['dem']) canvas = cv2.cvtColor(normedDEM, cv2.COLOR_GRAY2RGB) im2, startContour, hierarchy = cv2.findContours( day.startingPerim.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) im2, endContour, heirarchy = cv2.findContours( day.endingPerim.astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(canvas, endContour, -1, (0, 0, 1), 1) cv2.drawContours(canvas, startContour, -1, (0, 1, 0), 1) result[(burnName, date)] = canvas # plt.imshow(canvas) # plt.show() return result