def applyLuts(LUTdict, processedimagedirectory, geoTiff): print(geoTiff[-13:-5]) imageStack = gdal.Open(geoTiff).ReadAsArray() imageStack = np.moveaxis(imageStack, 0 ,-1) reflectanceImage = np.zeros(np.shape(imageStack)) for band in np.arange(1,6): rawimage = processedimagedirectory + geoTiff[-13:-5] + '_{}'.format(band) + '.tif' #print(rawimage) metadatadict = metadataReader.metadataGrabber(rawimage) irradiance = metadatadict['Xmp.Camera.Irradiance'] #print(irradiance) refIrradiance = [] for key, value in LUTdict.items() : if key[0] == band-1: refIrradiance.append(key[1]) refIrradiance = np.asarray(refIrradiance) comparison = np.abs(refIrradiance-irradiance) #print(comparison) #print(np.argmin(comparison)) closestIrradiance = refIrradiance[np.argmin(comparison)] #print(closestIrradiance) dictionaryreadkey = (band-1,closestIrradiance) slope = LUTdict[dictionaryreadkey][0][0] intercept = LUTdict[dictionaryreadkey][1][0] reflectanceImage[:,:,band-1] = imageStack[:,:,band-1] * slope + intercept #print(np.argmin(comparison)) print(np.max(reflectanceImage[:,:,band-1])) return reflectanceImage '''
def getImages(imageDirectory, plotting=True): import glob import os import numpy import context import matplotlib from matplotlib import pyplot as plt from context import geoTIFF from geoTIFF import metadataReader dlsDictList = [{},{},{},{},{}] orientation = ['Left', 'Middle', 'Right'] for o in orientation: currentDirectory = imageDirectory+'/'+o+'/*.tif' imageList = sorted(glob.glob(currentDirectory)) dlsDict = {"Blue": [], "Green": [], "Red": [], "NIR": [], "Red edge": []} for image in imageList: metadata = metadataReader.metadataGrabber(image) dlsIrrad = metadata['Xmp.DLS.SpectralIrradiance'] bandName = metadata['Xmp.Camera.BandName'] dlsDict[bandName].append(dlsIrrad) maxList = {k:max(v) for k,v in dlsDict.items()} maxList = sorted(maxList, key=maxList.get, reverse=True) if o == "Middle": dlsDictList[4] = dlsDict["Red edge"] if plotting: xvals = numpy.arange(380, 1000 + 10, 10) for k in dlsDict.keys(): plt.figure(o) plt.title("Downwelling Light Sensor Orientation: {0}".format(o)) plt.plot(xvals, dlsDict[k], label=k) plt.xlabel("Wavelengths [nm]") plt.ylabel("DLS Spectral Irradiance [W/m^2/nm]") plt.xlim(380, 1000) plt.ylim(0, max(dlsDict[maxList[0]]) + .002) plt.legend(loc=5) for a in [0,1]: annotation = "{0} max {1:.5f}".format(maxList[a],max(dlsDict[maxList[a]])) plt.annotate(annotation, (xvals[dlsDict[maxList[a]].index(max(dlsDict[maxList[a]]))], max(dlsDict[maxList[a]]))) plt.show()
def applyLuts(LUTdict, processedimagedirectory, geoTiff): #Apply our fancy LUT to an image to generate a reflectance image print(geoTiff[-13:-5]) #Read in image/raster with gdal imageStack = gdal.Open(geoTiff).ReadAsArray() imageStack = np.moveaxis(imageStack, 0, -1) #Prepare a store of values for reflectance reflectanceImage = np.zeros(np.shape(imageStack)) #Cycle through each band seperately to take advantage of ELM LUTS and doc the LUT used CurrentIrradiance = [] ClosestIrradiance = [] ReferenceImage = [] Band = [] for band in np.arange(1, 6): rawimage = processedimagedirectory + geoTiff[-13:-5] + '_{}'.format( band) + '.tif' metadatadict = metadataReader.metadataGrabber(rawimage) #Load in image irrradiance irradiance = metadatadict['Xmp.Camera.Irradiance'] refIrradiance = [] for key, value in LUTdict.items(): if key[0] == band - 1: refIrradiance.append(key[1]) #Find the closest irradiance between the LUT and current image refIrradiance = np.asarray(refIrradiance) comparison = np.abs(refIrradiance - irradiance) closestIrradiance = refIrradiance[np.argmin(comparison)] dictionaryreadkey = (band - 1, closestIrradiance) #Grab the calibration coefficients from the LUT slope = LUTdict[dictionaryreadkey][0][0] intercept = LUTdict[dictionaryreadkey][1][0] referenceImage = LUTdict[dictionaryreadkey][2] #Generate the reflectance band reflectanceImage[:, :, band - 1] = imageStack[:, :, band - 1] * slope + intercept print(np.max(reflectanceImage[:, :, band - 1])) CurrentIrradiance.append(irradiance) ClosestIrradiance.append(closestIrradiance) ReferenceImage.append(referenceImage) Band.append(band - 1) return reflectanceImage, CurrentIrradiance, ReferenceImage, ClosestIrradiance, Band
def micasenseRawData(geotiffFilename): from geoTIFF import metadataReader import time import numpy as np irradianceDict = {} #For each band we are going to record a value for irradiance for band in np.arange(1, 6): rawextension = '_{}.tif'.format(str(band)) rawFilename = geotiffFilename[:-21] + 'processed/' + geotiffFilename[ -13:-5] + rawextension #print('rawFilename',rawFilename) metadatadict = metadataReader.metadataGrabber(rawFilename) irradianceDict[band] = float( metadatadict['Xmp.DLS.SpectralIrradiance']) #For a single image we record the time in which the frame was captured t = time.strptime(metadatadict['timeStamp'].split('T')[-1].split('.')[0], '%H:%M:%S') frametime = (t.tm_hour - 5) * 60 + t.tm_min altitude = metadatadict['Exif.GPSInfo.GPSAltitude'] resolution = metadatadict['Exif.Photo.FocalPlaneXResolution'] return irradianceDict, frametime, altitude, resolution
def findLUTS(missionDatafilename, processedimagedirectory, cameraresponseSR, logging=True): #Generate numerous LUT to generate reflectance imagery based on downwelling irradiance if logging == True: csvfile = '/'.join( missionDatafilename.split('/')[:-1]) + '/ELM_LUT_logger.csv' with open(csvfile, 'w', newline='\n') as currentTextFile: writer = csv.writer(currentTextFile, delimiter=',') writer.writerow([ 'Frame', 'Altitude', 'Band', 'Irradiance', 'White #', 'White Rho', 'White Avg', 'White Std', 'Black #', 'Black Rho', 'Black Avg', 'Black Std', 'ELM Slope', 'ELM Intercept' ]) #From the target acquisition, grab every frame that had targets internal missionData = np.loadtxt(missionDatafilename, unpack=True, skiprows=1, dtype=str, delimiter=',') frames = list(np.unique(missionData[1])) #We need to figure out when we are at altitude, lets say we have to be within 95% of peak to be good Altitudes = np.loadtxt(missionDatafilename, unpack=True, skiprows=1, usecols=4, dtype=float, delimiter=',') peakAltitude = np.max(Altitudes) requiredAlt = peakAltitude * 0.95 #For each frame we have to filter according to additional criteria LUTdict = {} for name in frames: indices = np.where(missionData[1] == name) scenetargets = missionData[0][indices] framestd = {} svcdict = {} DNdict = {} #We always want the black target, as we do not run into capture issues here #For any of the targets we grab, we need the same data if '4' in scenetargets: indextarget4 = np.where((missionData[1] == name) & (missionData[0] == '4')) #Store the standard deviation std4 = missionData[10:15, indextarget4[0]] #Store the target average pixel value DN4 = missionData[5:10, indextarget4[0]] #Store the corresponding svc file number svcfilenumber = missionData[31, indextarget4] framestd['target4'] = std4 svcdict['target4'] = svcfilenumber DNdict['target4'] = DN4 #Since black will always be a valid target, lets grab a flight altitude here #If we are still climbing or descending, we don't want this frame altitude = missionData[4, indextarget4] if float(altitude[0][0]) < requiredAlt: continue #Ideally we want the brightest white target, but lets grab all 'whites' here #5 is our wooden white cal target if '5' in scenetargets: indextarget5 = np.where((missionData[1] == name) & (missionData[0] == '5')) std5 = missionData[10:15, indextarget5[0]] DN5 = missionData[5:10, indextarget5[0]] svcfilenumber = missionData[31, indextarget5] framestd['target5'] = std5 svcdict['target5'] = svcfilenumber DNdict['target5'] = DN5 #Brightest white on the tribar target if '1' in scenetargets: indextarget1 = np.where((missionData[1] == name) & (missionData[0] == '1')) std1 = missionData[10:15, indextarget1[0]] DN1 = missionData[5:10, indextarget1[0]] svcfilenumber = missionData[31, indextarget1] framestd['target1'] = std1 svcdict['target1'] = svcfilenumber DNdict['target1'] = DN1 #Our gray target on the white tribar if '2' in scenetargets: indextarget2 = np.where((missionData[1] == name) & (missionData[0] == '2')) std2 = missionData[10:15, indextarget2[0]] DN2 = missionData[5:10, indextarget2[0]] svcfilenumber = missionData[31, indextarget2] framestd['target2'] = std2 svcdict['target2'] = svcfilenumber DNdict['target2'] = DN2 #All that has been done so far is grabbing data from our target csv file #Now we need to manipulate all of the values reflectancedict = {} #Lets grab a reflectance value for every SVC file that we have, and #combining that with our camera RSR measurements for key, value in svcdict.items(): SVCfilename = 'T' + value[0][0].zfill(3) + '.sig' SVCfile = glob.glob(SVCdirectory + '*' + SVCfilename) reflectancevalues = [ findReflectance(SVCfile[0], cameraresponseSR, 'Blue'), findReflectance(SVCfile[0], cameraresponseSR, 'Green'), findReflectance(SVCfile[0], cameraresponseSR, 'Red'), findReflectance(SVCfile[0], cameraresponseSR, 'Red Edge'), findReflectance(SVCfile[0], cameraresponseSR, 'NIR') ] reflectancedict[key] = reflectancevalues #If we have more than two targets in scene, we have to make a decision #on what to use for ELM if len(framestd) >= 2: #If our standard deviation indicates saturation, we should avoid that target #print(framestd.values()) for key, value in framestd.items(): if '0' in value: del reflectancedict[key] del DNdict[key] if '0.0' in value: del reflectancedict[key] del DNdict[key] #If we still have two targets in scene, and one is black, we can do ELM if len(DNdict) >= 2 and 'target4' in DNdict.keys(): for key, value in DNdict.items(): #Brightest target after all of this filtering is still priority if 'target5' in DNdict: gotoTarget = 'target5' elif 'target1' in DNdict: gotoTarget = 'target1' elif 'target2' in DNdict: gotoTarget = 'target2' #Grab the reflectance values and DN required for ELM whiteReflect = np.asarray(reflectancedict[gotoTarget]) blackReflect = np.asarray(reflectancedict['target4']) whiteDN = DNdict[gotoTarget].astype(np.float) blackDN = DNdict['target4'].astype(np.float) #Perform ELM for each band for band in np.arange(5): slope = (whiteReflect[band] - blackReflect[band]) / ( whiteDN[band] - blackDN[band]) intercept = whiteReflect[band] - slope * whiteDN[band] #Grab the specialty of our method, irradiance based ELM imagemetadatafile = processedimagedirectory + name[:8] + '_' + str( band + 1) + '.tif' metadatadict = metadataReader.metadataGrabber( imagemetadatafile) irradiance = metadatadict['Xmp.Camera.Irradiance'] #Our series of LUT for generating reflectance imagery LUTdict[(band, irradiance)] = (slope, intercept, name) #If we get this far we need to log some info if logging == True: with open(csvfile, 'a', newline='\n') as currentTextFile: writer = csv.writer(currentTextFile, delimiter=',') writer.writerow([ name, altitude[0][0], band, irradiance, gotoTarget, whiteReflect[band], whiteDN[band][0], framestd[gotoTarget][band][0], 'target4', blackReflect[band], blackDN[band][0], framestd['target4'][band][0], slope[0], intercept[0] ]) return LUTdict
splitted = flightDirectory.split('/') gpsLog = '/'.join(splitted[:-1]) + os.path.sep + "GPSLog.csv" with open(gpsLog,'w') as resultFile: wr = csv.writer(resultFile) images = startIndex*5 #for images in range(0,len(tiffList),5): while images < len(tiffList)-5: print("Current image number: {0}".format(images//5)) imageList = [tiffList[images], tiffList[images+1], tiffList[images+2], tiffList[images+3], tiffList[images+4]] imageOrderDict = {} for image in imageList: imageDict = metadataGrabber(image) imageWavelength = imageDict['Xmp.DLS.CenterWavelength'] imageOrderDict[image] = imageWavelength imageList = sorted(imageOrderDict, key=imageOrderDict.get, reverse=False) matchOrder = OrderImagePairs(imageList, addOne=True) if matchOrder is None: continue imageStack, goodCorr = stackImages(imageList, matchOrder,transformDictionary,'ECC', crop=False, bandQC=False) if stackQC: green, red, ir = imageStack[:,:,1], imageStack[:,:,2], imageStack[:,:,4] cv2.imshow("RGB - ' ' to accept | 'n' to reject [1 sec]", cv2.resize(imageStack[:,:,:3], None, fx=.5, fy=.5, interpolation=cv2.INTER_AREA)) cv2.imshow("False Re", cv2.resize(imageStack[:,:,1:4], None, fx=.3, fy=.3, interpolation=cv2.INTER_AREA)) cv2.imshow("False IR", cv2.resize(np.dstack((green,red, ir)), None, fx=.3, fy=.3, interpolation=cv2.INTER_AREA))
def findLUTS(missionDatafilename,processedimagedirectory,cameraresponseSR): missionData = np.loadtxt(missionDatafilename,unpack = True, skiprows = 1,dtype = str, delimiter = ',') frames = list(np.unique(missionData[1])) LUTdict = {} for name in frames: indices = np.where(missionData[1] == name) scenetargets = missionData[0][indices] framestd = {} svcdict = {} DNdict = {} if '4' in scenetargets: indextarget4 = np.where((missionData[1] == name) & (missionData[0] == '4')) std4 = missionData[10:15,indextarget4[0]] DN4 = missionData[5:10,indextarget4[0]] svcfilenumber = missionData[31,indextarget4] #print(svcfilenumber) framestd['target4'] = std4 svcdict['target4'] = svcfilenumber DNdict['target4'] = DN4 if '5' in scenetargets: indextarget5 = np.where((missionData[1] == name) & (missionData[0] == '5')) std5 = missionData[10:15,indextarget5[0]] DN5 = missionData[5:10,indextarget5[0]] svcfilenumber = missionData[31,indextarget5] framestd['target5'] = std5 svcdict['target5'] = svcfilenumber DNdict['target5'] = DN5 if '1' in scenetargets: indextarget1 = np.where((missionData[1] == name) & (missionData[0] == '1')) std1 = missionData[10:15,indextarget1[0]] DN1 = missionData[5:10,indextarget1[0]] svcfilenumber = missionData[31,indextarget1] framestd['target1'] = std1 svcdict['target1'] = svcfilenumber DNdict['target1'] = DN1 if '2' in scenetargets: indextarget2 = np.where((missionData[1] == name) & (missionData[0] == '2')) std2 = missionData[10:15,indextarget2[0]] DN2 = missionData[5:10,indextarget2[0]] svcfilenumber = missionData[31,indextarget2] framestd['target2'] = std2 svcdict['target2'] = svcfilenumber DNdict['target2'] = DN2 #print(framestd) #print(framestd) reflectancedict = {} for key, value in svcdict.items(): SVCfilename = 'T' + value[0][0].zfill(3) + '.sig' SVCfile = glob.glob(SVCdirectory + '*' + SVCfilename) reflectancevalues = [findReflectance(SVCfile[0],cameraresponseSR,'Blue'),findReflectance(SVCfile[0],cameraresponseSR,'Green'),findReflectance(SVCfile[0],cameraresponseSR,'Red'),findReflectance(SVCfile[0],cameraresponseSR,'Red Edge'),findReflectance(SVCfile[0],cameraresponseSR,'NIR')] #print(reflectancevalues) reflectancedict[key] = reflectancevalues if len(framestd) >= 2: for key, value in framestd.items(): if '0.0' in value: #print('deleting saturated targets') del reflectancedict[key] del DNdict[key] #print(DNdict) #print(DNdict) if len(DNdict) >= 2 and 'target4' in DNdict.keys(): for key,value in DNdict.items(): if 'target5' in DNdict: gotoTarget = 'target5' elif 'target1' in DNdict: gotoTarget = 'target1' elif 'target2' in DNdict: gotoTarget = 'target2' #print(gotoTarget) #print(reflectancedict) #print(DNdict) whiteReflect = np.asarray(reflectancedict[gotoTarget]) blackReflect = np.asarray(reflectancedict['target4']) whiteDN = DNdict[gotoTarget].astype(np.float) blackDN = DNdict['target4'].astype(np.float) #print(type(whiteReflect[0])) #print(whiteReflect) #print(type(blackReflect[0])) #print(blackReflect) #print(type(whiteDN)) #print(whiteDN) #print(type(blackDN)) #print(blackDN) for band in np.arange(5): slope = (whiteReflect[band] - blackReflect[band])/(whiteDN[band] - blackDN[band]) #print('slope',slope) intercept = whiteReflect[band] - slope * whiteDN[band] #print('intercept',intercept) imagemetadatafile = processedimagedirectory + name[:8] + '_' + str(band+1) + '.tif' #print(imagemetadatafile) metadatadict = metadataReader.metadataGrabber(imagemetadatafile) irradiance = metadatadict['Xmp.Camera.Irradiance'] #print('I',irradiance) LUTdict[(band,irradiance)] = (slope,intercept) #print('LUTdict',LUTdict) #print('LUTdict',LUTdict) return LUTdict