def main(): usage = ''' Usage: ----------------------------------------------------------------------- python %s [-d spatialDimensions] [-p bandPositions [-r resolution ratio] [-b registration band] msfilename panfilename ----------------------------------------------------------------------- bandPositions and spatialDimensions are lists, e.g., -p [1,2,3] -d [0,0,400,400] Outfile name is msfilename_pan_dwt with same format as msfilename Note: PAN image must completely overlap MS image subset chosen -----------------------------------------------------''' %sys.argv[0] options, args = getopt.getopt(sys.argv[1:],'hd:p:r:b:') ratio = 4 dims1 = None pos1 = None k1 = 0 for option, value in options: if option == '-h': print usage return elif option == '-r': ratio = eval(value) elif option == '-d': dims1 = eval(value) elif option == '-p': pos1 = eval(value) elif option == '-b': k1 = eval(value)-1 if len(args) != 2: print 'Incorrect number of arguments' print usage sys.exit(1) gdal.AllRegister() file1 = args[0] file2 = args[1] path = os.path.dirname(file1) basename1 = os.path.basename(file1) root1, ext1 = os.path.splitext(basename1) outfile = '%s/%s_pan_dwt%s'%(path,root1,ext1) # MS image inDataset1 = gdal.Open(file1,GA_ReadOnly) try: cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount except Exception as e: print 'Error: %e --Image could not be read'%e sys.exit(1) if pos1 is None: pos1 = range(1,bands+1) num_bands = len(pos1) if dims1 is None: dims1 = [0,0,cols,rows] x10,y10,cols1,rows1 = dims1 # PAN image inDataset2 = gdal.Open(file2,GA_ReadOnly) try: bands = inDataset2.RasterCount except Exception as e: print 'Error: %e --Image could not be read'%e sys.exit(1) if bands>1: print 'PAN image must be a single band' sys.exit(1) geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() if (geotransform1 is None) or (geotransform2 is None): print 'Image not georeferenced, aborting' sys.exit(1) print '=========================' print ' DWT Pansharpening' print '=========================' print time.asctime() print 'MS file: '+file1 print 'PAN file: '+file2 # image arrays band = inDataset1.GetRasterBand(1) tmp = band.ReadAsArray(0,0,1,1) dt = tmp.dtype MS = np.asarray(np.zeros((num_bands,rows1,cols1)),dtype=dt) k = 0 for b in pos1: band = inDataset1.GetRasterBand(b) MS[k,:,:] = band.ReadAsArray(x10,y10,cols1,rows1) k += 1 # if integer assume 11bit quantization otherwise must be byte if MS.dtype == np.int16: fact = 8.0 MS = auxil.byteStretch(MS,(0,2**11)) else: fact = 1.0 # read in corresponding spatial subset of PAN image if (geotransform1 is None) or (geotransform2 is None): print 'Image not georeferenced, aborting' return # upper left corner pixel in PAN gt1 = list(geotransform1) gt2 = list(geotransform2) ulx1 = gt1[0] + x10*gt1[1] uly1 = gt1[3] + y10*gt1[5] x20 = int(round(((ulx1 - gt2[0])/gt2[1]))) y20 = int(round(((uly1 - gt2[3])/gt2[5]))) cols2 = cols1*ratio rows2 = rows1*ratio band = inDataset2.GetRasterBand(1) PAN = band.ReadAsArray(x20,y20,cols2,rows2) # if integer assume 11-bit quantization, otherwise must be byte if PAN.dtype == np.int16: PAN = auxil.byteStretch(PAN,(0,2**11)) # compress PAN to resolution of MS image panDWT = auxil.DWTArray(PAN,cols2,rows2) r = ratio while r > 1: panDWT.filter() r /= 2 bn0 = panDWT.get_quadrant(0) lines0,samples0 = bn0.shape bn1 = MS[k1,:,:] # register (and subset) MS image to compressed PAN image (scale,angle,shift) = auxil.similarity(bn0,bn1) tmp = np.zeros((num_bands,lines0,samples0)) for k in range(num_bands): bn1 = MS[k,:,:] bn2 = ndii.zoom(bn1, 1.0/scale) bn2 = ndii.rotate(bn2, angle) bn2 = ndii.shift(bn2, shift) tmp[k,:,:] = bn2[0:lines0,0:samples0] MS = tmp # compress pan once more, extract wavelet quadrants, and restore panDWT.filter() fgpan = panDWT.get_quadrant(1) gfpan = panDWT.get_quadrant(2) ggpan = panDWT.get_quadrant(3) panDWT.invert() # output array sharpened = np.zeros((num_bands,rows2,cols2),dtype=np.float32) aa = np.zeros(3) bb = np.zeros(3) print 'Wavelet correlations:' for i in range(num_bands): # make copy of panDWT and inject ith ms band msDWT = copy.deepcopy(panDWT) msDWT.put_quadrant(MS[i,:,:],0) # compress once more msDWT.filter() # determine wavelet normalization coefficents ms = msDWT.get_quadrant(1) aa[0],bb[0],R = auxil.orthoregress(fgpan.ravel(), ms.ravel()) Rs = 'Band '+str(i+1)+': %8.3f'%R ms = msDWT.get_quadrant(2) aa[1],bb[1],R = auxil.orthoregress(gfpan.ravel(), ms.ravel()) Rs += '%8.3f'%R ms = msDWT.get_quadrant(3) aa[2],bb[2],R = auxil.orthoregress(ggpan.ravel(), ms.ravel()) Rs += '%8.3f'%R print Rs # restore once and normalize wavelet coefficients msDWT.invert() msDWT.normalize(aa,bb) # restore completely and collect result r = 1 while r < ratio: msDWT.invert() r *= 2 sharpened[i,:,:] = msDWT.get_quadrant(0) sharpened *= fact # write to disk driver = inDataset1.GetDriver() outDataset = driver.Create(outfile,cols2,rows2,num_bands,GDT_Float32) projection1 = inDataset1.GetProjection() if projection1 is not None: outDataset.SetProjection(projection1) gt1 = list(geotransform1) gt1[0] += x10*ratio gt1[3] -= y10*ratio gt1[1] = gt2[1] gt1[2] = gt2[2] gt1[4] = gt2[4] gt1[5] = gt2[5] outDataset.SetGeoTransform(tuple(gt1)) for k in range(num_bands): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(sharpened[k,:,:],0,0) outBand.FlushCache() outDataset = None print 'Result written to %s'%outfile inDataset1 = None inDataset2 = None
def main(): usage = ''' Usage: ----------------------------------------------------------------------- python %s [-d spatialDimensions] [-p bandPositions [-r resolution ratio] [-b registration band] msfilename panfilename ----------------------------------------------------------------------- bandPositions and spatialDimensions are lists, e.g., -p [1,2,3] -d [0,0,400,400] Outfile name is msfilename_pan_atwt with same format as msfilename Note: PAN image must completely overlap MS image subset chosen -----------------------------------------------------''' %sys.argv[0] options, args = getopt.getopt(sys.argv[1:],'hd:p:r:b:') ratio = 4 dims1 = None pos1 = None k1 = 1 for option, value in options: if option == '-h': print usage return elif option == '-r': ratio = eval(value) elif option == '-d': dims1 = eval(value) elif option == '-p': pos1 = eval(value) elif option == '-b': k1 = eval(value) if len(args) != 2: print 'Incorrect number of arguments' print usage sys.exit(1) gdal.AllRegister() file1 = args[0] file2 = args[1] path = os.path.dirname(file1) basename1 = os.path.basename(file1) root1, ext1 = os.path.splitext(basename1) outfile = '%s/%s_pan_atwt%s'%(path,root1,ext1) # MS image inDataset1 = gdal.Open(file1,GA_ReadOnly) try: cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount except Exception as e: print 'Error: %e --Image could not be read'%e sys.exit(1) if pos1 is None: pos1 = range(1,bands+1) num_bands = len(pos1) if dims1 is None: dims1 = [0,0,cols,rows] x10,y10,cols1,rows1 = dims1 # PAN image inDataset2 = gdal.Open(file2,GA_ReadOnly) try: bands = inDataset2.RasterCount except Exception as e: print 'Error: %e --Image could not be read'%e sys.exit(1) if bands>1: print 'PAN image must be a single band' sys.exit(1) geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() if (geotransform1 is None) or (geotransform2 is None): print 'Image not georeferenced, aborting' sys.exit(1) print '=========================' print ' ATWT Pansharpening' print '=========================' print time.asctime() print 'MS file: '+file1 print 'PAN file: '+file2 # read in MS image band = inDataset1.GetRasterBand(1) tmp = band.ReadAsArray(0,0,1,1) dt = tmp.dtype MS = np.asarray(np.zeros((num_bands,rows1,cols1)),dtype = dt) k = 0 for b in pos1: band = inDataset1.GetRasterBand(b) MS[k,:,:] = band.ReadAsArray(x10,y10,cols1,rows1) k += 1 # if integer assume 11-bit quantization, otherwise must be byte if MS.dtype == np.int16: fact = 8.0 MS = auxil.byteStretch(MS,(0,2**11)) else: fact = 1.0 # read in corresponding spatial subset of PAN image gt1 = list(geotransform1) gt2 = list(geotransform2) ulx1 = gt1[0] + x10*gt1[1] uly1 = gt1[3] + y10*gt1[5] x20 = int(round(((ulx1 - gt2[0])/gt2[1]))) y20 = int(round(((uly1 - gt2[3])/gt2[5]))) cols2 = cols1*ratio rows2 = rows1*ratio band = inDataset2.GetRasterBand(1) PAN = band.ReadAsArray(x20,y20,cols2,rows2) # if integer assume 11-bit quantization, otherwise must be byte if PAN.dtype == np.int16: PAN = auxil.byteStretch(PAN,(0,2**11)) # out array sharpened = np.zeros((num_bands,rows2,cols2),dtype=np.float32) # compress PAN to resolution of MS image using DWT panDWT = auxil.DWTArray(PAN,cols2,rows2) r = ratio while r > 1: panDWT.filter() r /= 2 bn0 = panDWT.get_quadrant(0) # register (and subset) MS image to compressed PAN image using selected MSband lines0,samples0 = bn0.shape bn1 = MS[k1-1,:,:] # register (and subset) MS image to compressed PAN image (scale,angle,shift) = auxil.similarity(bn0,bn1) tmp = np.zeros((num_bands,lines0,samples0)) for k in range(num_bands): bn1 = MS[k,:,:] bn2 = ndii.zoom(bn1, 1.0/scale) bn2 = ndii.rotate(bn2, angle) bn2 = ndii.shift(bn2, shift) tmp[k,:,:] = bn2[0:lines0,0:samples0] MS = tmp smpl = np.random.randint(cols2*rows2,size=100000) print 'Wavelet correlations:' # loop over MS bands for k in range(num_bands): msATWT = auxil.ATWTArray(PAN) r = ratio while r > 1: msATWT.filter() r /= 2 # sample PAN wavelet details X = msATWT.get_band(msATWT.num_iter) X = X.ravel()[smpl] # resize the ms band to scale of the pan image ms_band = ndii.zoom(MS[k,:,:],ratio) # sample details of MS band tmpATWT = auxil.ATWTArray(ms_band) r = ratio while r > 1: tmpATWT.filter() r /= 2 Y = tmpATWT.get_band(msATWT.num_iter) Y = Y.ravel()[smpl] # get band for injection bnd = tmpATWT.get_band(0) tmpATWT = None aa,bb,R = auxil.orthoregress(X,Y) print 'Band '+str(k+1)+': %8.3f'%R # inject the filtered MS band msATWT.inject(bnd) # normalize wavelet components and expand msATWT.normalize(aa,bb) r = ratio while r > 1: msATWT.invert() r /= 2 sharpened[k,:,:] = msATWT.get_band(0) sharpened *= fact # rescale dynamic range msATWT = None # write to disk driver = inDataset1.GetDriver() outDataset = driver.Create(outfile,cols2,rows2,num_bands,GDT_Float32) gt1[0] += x10*ratio gt1[3] -= y10*ratio gt1[1] = gt2[1] gt1[2] = gt2[2] gt1[4] = gt2[4] gt1[5] = gt2[5] outDataset.SetGeoTransform(tuple(gt1)) projection1 = inDataset1.GetProjection() if projection1 is not None: outDataset.SetProjection(projection1) for k in range(num_bands): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(sharpened[k,:,:],0,0) outBand.FlushCache() outDataset = None print 'Result written to %s'%outfile inDataset1 = None inDataset2 = None
def main(): usage = ''' Usage: -------------------------------------- Perform Gaussian mixture clustering on multispectral imagery python %s [OPTIONS] filename Options: -h this help -p <list> band positions e.g. -p [1,2,3,4,5,7] -d <list> spatial subset [x,y,width,height] e.g. -d [0,0,200,200] -K <int> number of clusters (default 6) -M <int> maximum scale (default 2) -m <int> minimum scale (default 0) -t <float> initial annealing temperature (default 0.5) -s <float> spatial mixing factor (default 0.5) -P generate class probabilities image If the input file is named path/filenbasename.ext then The output classification file is named path/filebasename_em.ext and the class probabilities output file is named path/filebasename_emprobs.ext -------------------------------------'''%sys.argv[0] options, args = getopt.getopt(sys.argv[1:],'hp:d:K:M:m:t:s:P') pos = None dims = None K,max_scale,min_scale,T0,beta,probs = (6,2,0,0.5,0.5,False) for option, value in options: if option == '-h': print usage return elif option == '-p': pos = eval(value) elif option == '-d': dims = eval(value) elif option == '-K': K = eval(value) elif option == '-M': max_scale = eval(value) elif option == '-m': min_scale = min(eval(value),3) elif option == '-t': T0 = eval(value) elif option == '-s': beta = eval(value) elif option == '-P': probs = True if len(args) != 1: print 'Incorrect number of arguments' print usage sys.exit(1) infile = args[0] gdal.AllRegister() try: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount except Exception as e: print 'Error: %s --Image could not be read'%e sys.exit(1) if pos is not None: bands = len(pos) else: pos = range(1,bands+1) if dims: x0,y0,cols,rows = dims else: x0 = 0 y0 = 0 class_image = np.zeros((rows,cols),dtype=np.byte) path = os.path.dirname(infile) basename = os.path.basename(infile) root, ext = os.path.splitext(basename) outfile = path+'/'+root+'_em'+ext if probs: probfile = path+'/'+root+'_emprobs'+ext print '--------------------------' print ' EM clustering' print '--------------------------' print 'infile: %s'%infile print 'clusters: %i'%K print 'T0: %f'%T0 print 'beta: %f'%beta print 'scale: %i'%max_scale start = time.time() # read in image and compress path = os.path.dirname(infile) basename = os.path.basename(infile) root, ext = os.path.splitext(basename) DWTbands = [] for b in pos: band = inDataset.GetRasterBand(b) DWTband = auxil.DWTArray(band.ReadAsArray(x0,y0,cols,rows).astype(float),cols,rows) for i in range(max_scale): DWTband.filter() DWTbands.append(DWTband) rows,cols = DWTbands[0].get_quadrant(0).shape G = np.transpose(np.array([DWTbands[i].get_quadrant(0,float=True).ravel() for i in range(bands)])) # initialize membership matrix n = G.shape[0] U = np.random.random((K,n)) den = np.sum(U,axis=0) for j in range(K): U[j,:] = U[j,:]/den # cluster at minimum scale try: U,Ms,Cs,Ps,pdens = em(G,U,T0,beta,rows,cols) except: print 'em failed' return # sort clusters wrt partition density idx = np.argsort(pdens) idx = idx[::-1] U = U[idx,:] # clustering at increasing scales for i in range(max_scale-min_scale): # expand U and renormalize U = np.reshape(U,(K,rows,cols)) rows = rows*2 cols = cols*2 U = ndi.zoom(U,(1,2,2)) U = np.reshape(U,(K,rows*cols)) idx = np.where(U<0.0) U[idx] = 0.0 den = np.sum(U,axis=0) for j in range(K): U[j,:] = U[j,:]/den # expand the image for i in range(bands): DWTbands[i].invert() G = [DWTbands[i].get_quadrant( 0,float=True).ravel() for i in range(bands)] G = np.transpose(np.array(G)) # cluster unfrozen = np.where(np.max(U,axis=0) < 0.90) try: U,Ms,Cs,Ps,pdens=em(G,U,0.0,beta,rows,cols, unfrozen=unfrozen) except: print 'em failed' return print 'Cluster mean vectors' print Ms print 'Cluster covariance matrices' for k in range(K): print 'cluster: %i'%k print Cs[k] # up-sample class memberships if necessary if min_scale>0: U = np.reshape(U,(K,rows,cols)) f = 2**min_scale rows = rows*f cols = cols*f U = ndi.zoom(U,(1,f,f)) U = np.reshape(U,(K,rows*cols)) idx = np.where(U<0.0) U[idx] = 0.0 den = np.sum(U,axis=0) for j in range(K): U[j,:] = U[j,:]/den # classify labels = np.byte(np.argmax(U,axis=0)+1) class_image[0:rows,0:cols] = np.reshape(labels,(rows,cols)) rows1,cols1 = class_image.shape # write to disk driver = inDataset.GetDriver() outDataset = driver.Create(outfile,cols1,rows1,1,GDT_Byte) projection = inDataset.GetProjection() geotransform = inDataset.GetGeoTransform() if geotransform is not None: gt = list(geotransform) gt[0] = gt[0] + x0*gt[1] gt[3] = gt[3] + y0*gt[5] outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) outBand = outDataset.GetRasterBand(1) outBand.WriteArray(class_image,0,0) outBand.FlushCache() outDataset = None # write class membership probability file if desired if probs: outDataset = driver.Create(probfile,cols,rows,K,GDT_Byte) if geotransform is not None: outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) for k in range(K): probs = np.reshape(U[k,:],(rows,cols)) probs = np.byte(probs*255) outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(probs,0,0) outBand.FlushCache() outDataset = None print 'class probabilities written to: %s'%probfile inDataset = None print 'classified image written to: '+outfile print 'elapsed time: '+str(time.time()-start) print '--done------------------------'