def main(): gdal.AllRegister() infile = auxil.select_infile(filt='*.xml') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # BSQ array image = zeros((len(pos),rows,cols),dtype=complex64) k = 0 for b in pos: band = inDataset.GetRasterBand(b) image[k,:,:]=band.ReadAsArray(x0,y0,cols,rows)\ .astype(complex) k += 1 inDataset = None # display magnitude in linear 2% stretch band0 = abs(image[0,:,:]) band0 = auxil.lin2pcstr(band0) mn = amin(band0) mx = amax(band0) plt.imshow((band0-mn)/(mx-mn), cmap='gray') plt.show()
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spectral and spatial subsets pos = auxil.select_pos(bands) bands = len(pos) x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # data matrix for difference images D = zeros((rows*cols,bands)) i = 0 for b in pos: band = inDataset.GetRasterBand(b) tmp = band.ReadAsArray(x0,y0,cols,rows)\ .astype(float) D[:,i] = (tmp-(roll(tmp,1,axis=0)+\ roll(tmp,1,axis=1))/2).ravel() i += 1 # noise covariance matrix S_N = mat(D).T*mat(D)/(rows*cols-1) print 'Noise covariance matrix, file %s'%infile print S_N
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spectral and spatial subsets pos = auxil.select_pos(bands) x0, y0, rows, cols = auxil.select_dims([0, 0, rows, cols]) # BSQ array image = zeros((len(pos), rows, cols)) k = 0 for b in pos: band = inDataset.GetRasterBand(b) image[k,:,:]=band.ReadAsArray(x0,y0,cols,rows)\ .astype(float) k += 1 inDataset = None # display first band band0 = image[0, :, :] mn = amin(band0) mx = amax(band0) plt.imshow((band0 - mn) / (mx - mn), cmap='gray') plt.show()
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) bands = len(pos) x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) K = auxil.select_integer(6,msg='Number clusters') G = zeros((rows*cols,len(pos))) k = 0 for b in pos: band = inDataset.GetRasterBand(b) G[:,k] = band.ReadAsArray(x0,y0,cols,rows)\ .astype(float).ravel() k += 1 centers, _ = kmeans(G,K) labels, _ = vq(G,centers) outfile,fmt = auxil.select_outfilefmt() if outfile: driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols,rows,1,GDT_Byte) outBand = outDataset.GetRasterBand(1) outBand.WriteArray(reshape(labels,(rows,cols))\ ,0,0) outBand.FlushCache() outDataset = None inDataset = None
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spectral and spatial subsets pos = auxil.select_pos(bands) bands = len(pos) x0, y0, rows, cols = auxil.select_dims([0, 0, rows, cols]) # data matrix for difference images D = zeros((rows * cols, bands)) i = 0 for b in pos: band = inDataset.GetRasterBand(b) tmp = band.ReadAsArray(x0,y0,cols,rows)\ .astype(float) D[:,i] = (tmp-(roll(tmp,1,axis=0)+\ roll(tmp,1,axis=1))/2).ravel() i += 1 # noise covariance matrix S_N = mat(D).T * mat(D) / (rows * cols - 1) print 'Noise covariance matrix, file %s' % infile print S_N
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spectral and spatial subsets pos = auxil.select_pos(bands) x0,y0,rows,cols = auxil.select_dims([0,0,rows,cols]) # BSQ array image = zeros((len(pos),rows,cols)) k = 0 for b in pos: band = inDataset.GetRasterBand(b) image[k,:,:]=band.ReadAsArray(x0,y0,cols,rows)\ .astype(float) k += 1 inDataset = None # display first band band0 = image[0,:,:] mn = amin(band0) mx = amax(band0) plt.imshow((band0-mn)/(mx-mn), cmap='gray' ) plt.show()
def dispms(filename=None,dims=None,rgb=None,enhance=None): gdal.AllRegister() if filename == None: filename = auxil.select_infile(title='Choose an image to display') if filename: inDataset = gdal.Open(filename,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return if dims == None: dims = auxil.select_dims([0,0,cols,rows]) if dims: x0,y0,cols,rows = dims else: return if rgb == None: rgb = auxil.select_rgb(bands) if rgb: r,g,b = rgb else: return if enhance == None: enhance = auxil.select_enhance('3') if enhance == '1': enhance = 'linear255' elif enhance == '2': enhance = 'linear' elif enhance == '3': enhance = 'linear2pc' elif enhance == '4': enhance = 'equalization' else: return redband = inDataset.GetRasterBand(r).ReadAsArray(x0,y0,cols,rows) greenband = inDataset.GetRasterBand(g).ReadAsArray(x0,y0,cols,rows) blueband = inDataset.GetRasterBand(b).ReadAsArray(x0,y0,cols,rows) if str(redband.dtype) == 'uint8': dt = 1 elif str(redband.dtype) == 'uint16': dt = 2 elif str(redband.dtype) == 'int16': dt = 2 elif str(redband.dtype) == 'float32': dt = 4 elif str(redband.dtype) == 'float64': dt = 6 else: print 'Unrecognized format' return redband = redband.tostring() greenband = greenband.tostring() blueband = blueband.tostring() if dt != 1: redband = auxil.byte_stretch(redband,dtype=dt) greenband = auxil.byte_stretch(greenband,dtype=dt) blueband = auxil.byte_stretch(blueband,dtype=dt) r,g,b = auxil.stretch(redband,greenband,blueband,enhance) bip = '' for i in range(cols*rows): bip += r[i]+g[i]+b[i] im = Image.fromstring('RGB', (cols,rows), bip, 'raw', ('RGB',3*cols,1)) print 'close image to finish' im.show() print 'done'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # output file outfile,fmt = auxil.select_outfilefmt() if not outfile: return print '=========================' print ' ENL Estimation' print '=========================' print time.asctime() print 'infile: %s'%infile start = time.time() if bands == 9: print 'Quad polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() # C12 (a) band = inDataset.GetRasterBand(2) a = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() band = inDataset.GetRasterBand(3) im = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() a = a + 1j*im # C13 (rho) band = inDataset.GetRasterBand(4) rho = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() band = inDataset.GetRasterBand(5) im = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() rho = rho + 1j*im # C22 (xsi) band = inDataset.GetRasterBand(6) xsi = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() # C23 (b) band = inDataset.GetRasterBand(7) b = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() band = inDataset.GetRasterBand(8) im = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() b = b + 1j*im # C33 (zeta) band = inDataset.GetRasterBand(9) zeta = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() det = k*xsi*zeta + 2*np.real(a*b*np.conj(rho)) - xsi*(abs(rho)**2) - k*(abs(b)**2) - zeta*(abs(a)**2) d = 2 elif bands == 4: print 'Dual polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() # C12 (a) band = inDataset.GetRasterBand(2) a = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() band = inDataset.GetRasterBand(3) im = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() a = a + 1j*im # C22 (xsi) band = inDataset.GetRasterBand(4) xsi = np.nan_to_num(band.ReadAsArray(x0,y0,cols,rows)).ravel() det = k*xsi - abs(a)**2 d = 1 elif bands == 1: print 'Single polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = band.ReadAsArray(x0,y0,cols,rows).ravel() det = k d = 0 enl_ml = np.zeros((rows,cols), dtype= np.float32) lu = lookup.table() print 'filtering...' print 'row: ', sys.stdout.flush() start = time.time() for i in range(3,rows-3): if i%50 == 0: print '%i '%i, sys.stdout.flush() windex = get_windex(i,cols) for j in range(3,cols-3): detC = det[windex] if np.min(detC) > 0.0: avlogdetC = np.sum(np.log(detC))/49 if bands == 9: k1 = np.sum(k[windex])/49 a1 = np.sum(a[windex])/49 rho1 = np.sum(rho[windex])/49 xsi1 = np.sum(xsi[windex])/49 b1 = np.sum(b[windex])/49 zeta1 = np.sum(zeta[windex])/49 detavC = k1*xsi1*zeta1 + 2*np.real(a1*b1*np.conj(rho1)) - xsi1*(np.abs(rho1)**2) - k1*(np.abs(b1)**2) - zeta1*(np.abs(a1)**2) elif bands == 4: k1 = np.sum(k[windex])/49 xsi1 = np.sum(xsi[windex])/49 a1 = np.sum(a[windex])/49 detavC = k1*xsi1 - np.abs(a1)**2 else: detavC = np.sum(k[windex])/49 logdetavC = np.log(detavC) arr = avlogdetC - logdetavC + lu[:,d] ell = np.where(arr*np.roll(arr,1)<0)[0] if ell != []: enl_ml[i,j] = float(ell[-1])/10.0 windex += 1 driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile,cols,rows,1,GDT_Float32) 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(enl_ml,0,0) outBand.FlushCache() outDataset = None ya,xa = np.histogram(enl_ml,bins=50) ya[0] = 0 plt.plot(xa[0:-1],ya) plt.show() print '' print 'ENL image written to: %s'%outfile print 'elapsed time: '+str(time.time()-start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') #path = arcpy.GetParameterAsText(0) if path: os.chdir(path) file1 = auxil.select_infile(title='Choose first image') #file1 = arcpy.GetParameterAsText(1) if file1: inDataset1 = gdal.Open(file1, GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x10, y10, cols1, rows1 = dims else: return # second image file2 = auxil.select_infile(title='Choose second image') #file2 = arcpy.GetParameterAsText(2) if file2: inDataset2 = gdal.Open(file2, GA_ReadOnly) cols = inDataset2.RasterXSize rows = inDataset2.RasterYSize bands = inDataset2.RasterCount else: return pos2 = auxil.select_pos(bands) if not pos2: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x20, y20, cols, rows = dims else: return # penalization lam = auxil.select_penal(0.0) if lam is None: return # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # match dimensions bands = len(pos2) if (rows1 != rows) or (cols1 != cols) or (len(pos1) != bands): sys.stderr.write("Size mismatch") sys.exit(1) print('=========================') print(' iMAD') print('=========================') print(time.asctime()) print('time1: ' + file1) print('time2: ' + file2) print('Delta [canonical correlations]') # iteration of MAD cpm = auxil.Cpm(2 * bands) delta = 1.0 oldrho = np.zeros(bands) itr = 0 tile = np.zeros((cols, 2 * bands)) sigMADs = 0 means1 = 0 means2 = 0 A = 0 B = 0 rasterBands1 = [] rasterBands2 = [] for b in pos1: rasterBands1.append(inDataset1.GetRasterBand(b)) for b in pos2: rasterBands2.append(inDataset2.GetRasterBand(b)) while (delta > 0.001) and (itr < 100): # spectral tiling for statistics for row in range(rows): for k in range(bands): tile[:, k] = rasterBands1[k].ReadAsArray(x10, y10 + row, cols, 1) tile[:, bands + k] = rasterBands2[k].ReadAsArray( x20, y20 + row, cols, 1) # eliminate no-data pixels (assuming all zeroes) tst1 = np.sum(tile[:, 0:bands], axis=1) tst2 = np.sum(tile[:, bands::], axis=1) idx1 = set(np.where((tst1 > 0))[0]) idx2 = set(np.where((tst2 > 0))[0]) idx = list(idx1.intersection(idx2)) if itr > 0: mads = np.asarray((tile[:, 0:bands] - means1) * A - (tile[:, bands::] - means2) * B) chisqr = np.sum((mads / sigMADs)**2, axis=1) wts = 1 - stats.chi2.cdf(chisqr, [bands]) cpm.update(tile[idx, :], wts[idx]) else: cpm.update(tile[idx, :]) # weighted covariance matrices and means S = cpm.covariance() means = cpm.means() # reset prov means object cpm.__init__(2 * bands) s11 = S[0:bands, 0:bands] s11 = (1 - lam) * s11 + lam * np.eye(bands) s22 = S[bands:, bands:] s22 = (1 - lam) * s22 + lam * np.eye(bands) s12 = S[0:bands, bands:] s21 = S[bands:, 0:bands] c1 = s12 * linalg.inv(s22) * s21 b1 = s11 c2 = s21 * linalg.inv(s11) * s12 b2 = s22 # solution of generalized eigenproblems if bands > 1: mu2a, A = auxil.geneiv(c1, b1) mu2b, B = auxil.geneiv(c2, b2) # sort a idx = np.argsort(mu2a) A = A[:, idx] # sort b idx = np.argsort(mu2b) B = B[:, idx] mu2 = mu2b[idx] else: mu2 = c1 / b1 A = 1 / np.sqrt(b1) B = 1 / np.sqrt(b2) # canonical correlations mu = np.sqrt(mu2) a2 = np.diag(A.T * A) b2 = np.diag(B.T * B) sigma = np.sqrt((2 - lam * (a2 + b2)) / (1 - lam) - 2 * mu) rho = mu * (1 - lam) / np.sqrt((1 - lam * a2) * (1 - lam * b2)) # stopping criterion delta = max(abs(rho - oldrho)) print(delta, rho) oldrho = rho # tile the sigmas and means sigMADs = np.tile(sigma, (cols, 1)) means1 = np.tile(means[0:bands], (cols, 1)) means2 = np.tile(means[bands::], (cols, 1)) # ensure sum of positive correlations between X and U is positive D = np.diag(1 / np.sqrt(np.diag(s11))) s = np.ravel(np.sum(D * s11 * A, axis=0)) A = A * np.diag(s / np.abs(s)) # ensure positive correlation between each pair of canonical variates cov = np.diag(A.T * s12 * B) B = B * np.diag(cov / np.abs(cov)) itr += 1 # write results to disk driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, bands + 1, GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.GetGeoTransform() if geotransform is not None: gt = list(geotransform) gt[0] = gt[0] + x10 * gt[1] gt[3] = gt[3] + y10 * gt[5] outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) outBands = [] for k in range(bands + 1): outBands.append(outDataset.GetRasterBand(k + 1)) for row in range(rows): for k in range(bands): tile[:, k] = rasterBands1[k].ReadAsArray(x10, y10 + row, cols, 1) tile[:, bands + k] = rasterBands2[k].ReadAsArray( x20, y20 + row, cols, 1) mads = np.asarray((tile[:, 0:bands] - means1) * A - (tile[:, bands::] - means2) * B) chisqr = np.sum((mads / sigMADs)**2, axis=1) for k in range(bands): outBands[k].WriteArray(np.reshape(mads[:, k], (1, cols)), 0, row) outBands[bands].WriteArray(np.reshape(chisqr, (1, cols)), 0, row) for outBand in outBands: outBand.FlushCache() outDataset = None inDataset1 = None inDataset2 = None print('result written to: ' + outfile) print('--------done---------------------')
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0, y0, rows, cols = auxil.select_dims([0, 0, rows, cols]) # output file outfile, fmt = auxil.select_outfilefmt() if not outfile: return print '=========================' print ' ENL Estimation' print '=========================' print time.asctime() print 'infile: %s' % infile start = time.time() if bands == 9: print 'Quad polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() # C12 (a) band = inDataset.GetRasterBand(2) a = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() band = inDataset.GetRasterBand(3) im = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() a = a + 1j * im # C13 (rho) band = inDataset.GetRasterBand(4) rho = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() band = inDataset.GetRasterBand(5) im = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() rho = rho + 1j * im # C22 (xsi) band = inDataset.GetRasterBand(6) xsi = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() # C23 (b) band = inDataset.GetRasterBand(7) b = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() band = inDataset.GetRasterBand(8) im = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() b = b + 1j * im # C33 (zeta) band = inDataset.GetRasterBand(9) zeta = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() det = k * xsi * zeta + 2 * np.real(a * b * np.conj(rho)) - xsi * ( abs(rho)**2) - k * (abs(b)**2) - zeta * (abs(a)**2) d = 2 elif bands == 4: print 'Dual polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() # C12 (a) band = inDataset.GetRasterBand(2) a = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() band = inDataset.GetRasterBand(3) im = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() a = a + 1j * im # C22 (xsi) band = inDataset.GetRasterBand(4) xsi = np.nan_to_num(band.ReadAsArray(x0, y0, cols, rows)).ravel() det = k * xsi - abs(a)**2 d = 1 elif bands == 1: print 'Single polarimetry' # C11 (k) band = inDataset.GetRasterBand(1) k = band.ReadAsArray(x0, y0, cols, rows).ravel() det = k d = 0 enl_ml = np.zeros((rows, cols), dtype=np.float32) lu = lookup.table() print 'filtering...' print 'row: ', sys.stdout.flush() start = time.time() for i in range(3, rows - 3): if i % 50 == 0: print '%i ' % i, sys.stdout.flush() windex = get_windex(i, cols) for j in range(3, cols - 3): detC = det[windex] if np.min(detC) > 0.0: avlogdetC = np.sum(np.log(detC)) / 49 if bands == 9: k1 = np.sum(k[windex]) / 49 a1 = np.sum(a[windex]) / 49 rho1 = np.sum(rho[windex]) / 49 xsi1 = np.sum(xsi[windex]) / 49 b1 = np.sum(b[windex]) / 49 zeta1 = np.sum(zeta[windex]) / 49 detavC = k1 * xsi1 * zeta1 + 2 * np.real( a1 * b1 * np.conj(rho1)) - xsi1 * ( np.abs(rho1)** 2) - k1 * (np.abs(b1)**2) - zeta1 * (np.abs(a1)**2) elif bands == 4: k1 = np.sum(k[windex]) / 49 xsi1 = np.sum(xsi[windex]) / 49 a1 = np.sum(a[windex]) / 49 detavC = k1 * xsi1 - np.abs(a1)**2 else: detavC = np.sum(k[windex]) / 49 logdetavC = np.log(detavC) arr = avlogdetC - logdetavC + lu[:, d] ell = np.where(arr * np.roll(arr, 1) < 0)[0] if ell != []: enl_ml[i, j] = float(ell[-1]) / 10.0 windex += 1 driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, 1, GDT_Float32) 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(enl_ml, 0, 0) outBand.FlushCache() outDataset = None ya, xa = np.histogram(enl_ml, bins=50) ya[0] = 0 plt.plot(xa[0:-1], ya) plt.show() print '' print 'ENL image written to: %s' % outfile print 'elapsed time: ' + str(time.time() - start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # MS image file1 = auxil.select_infile(title='Choose MS image') if file1: inDataset1 = gdal.Open(file1, GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return num_bands = len(pos1) dims = auxil.select_dims([0, 0, cols, rows]) if dims: x10, y10, cols1, rows1 = dims else: return # PAN image file2 = auxil.select_infile(title='Choose PAN image') if file2: inDataset2 = gdal.Open(file2, GA_ReadOnly) bands = inDataset2.RasterCount else: return if bands > 1: print 'Must be a single band (panchromatic) image' return geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # resolution ratio ratio = auxil.select_integer(4, 'Resolution ratio (2 or 4)') if not ratio: return # MS registration band k1 = auxil.select_integer(1, 'MS band for registration') if not k1: return # fine adjust roll = auxil.select_integer(0, 'Fine adjust (-2 ... 2)') if roll is None: return 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 - 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 if roll != 0: # fine adjust PAN = np.roll(PAN, roll, axis=0) PAN = np.roll(PAN, roll, axis=1) panDWT = auxil.DWTArray(PAN, cols2, rows2) r = ratio while r > 1: panDWT.filter() r /= 2 # 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 = gdal.GetDriverByName(fmt) 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 setChannels(self): self.displayChannelIndices = list(auxil.select_dims("Select the Channels to Visualize the Image"));
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # MS image file1 = auxil.select_infile(title='Choose MS image') if file1: inDataset1 = gdal.Open(file1,GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return num_bands = len(pos1) dims = auxil.select_dims([0,0,cols,rows]) if dims: x10,y10,cols1,rows1 = dims else: return # PAN image file2 = auxil.select_infile(title='Choose PAN image') if file2: inDataset2 = gdal.Open(file2,GA_ReadOnly) cols = inDataset2.RasterXSize rows = inDataset2.RasterYSize bands = inDataset2.RasterCount else: return if bands>1: print 'Must be a single band (panchromatic) image' return dims=auxil.select_dims([0,0,cols,rows]) if dims: x20,y20,cols2,rows2 = dims else: return # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # resolution ratio ratio = auxil.select_integer(4, 'Resolution ratio (2 or 4)') if not ratio: return # MS registration band k1 = auxil.select_integer(1, 'MS band for registration') if not k1: return print '=========================' print ' ATWT 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) # result will be float32 sharpened = np.zeros((num_bands,rows2,cols2),dtype=np.float32) k = 0 for b in pos1: band = inDataset1.GetRasterBand(b) MS[k,:,:] = band.ReadAsArray(x10,y10,cols1,rows1) k += 1 band = inDataset2.GetRasterBand(1) PAN = band.ReadAsArray(x20,y20,cols2,rows2) # if integer assume 11bit quantization, otherwise must be byte if PAN.dtype == np.int16: PAN = auxil.byteStretch(PAN,(0,2**11)) if MS.dtype == np.int16: MS = auxil.byteStretch(MS,(0,2**11)) # 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 MSband 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 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) # write to disk if outfile: driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols2,rows2,num_bands,GDT_Float32) projection1 = inDataset1.GetProjection() geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() if geotransform2 is not None: gt2 = list(geotransform2) if geotransform1 is not None: gt1 = list(geotransform1) gt1[0] += x10*gt2[1] # using PAN pixel sizes gt1[3] += y10*gt2[5] gt1[1] = gt2[1] gt1[2] = gt2[2] gt1[4] = gt2[4] gt1[5] = gt2[5] outDataset.SetGeoTransform(tuple(gt1)) 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(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) infile = auxil.select_infile(title='Select an image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) if not pos: return bands = len(pos) dims = auxil.select_dims([0, 0, cols, rows]) if dims: x0, y0, cols, rows = dims else: return class_image = np.zeros((rows, cols), dtype=np.byte) K = auxil.select_integer(6, 'Number of clusters') max_scale = auxil.select_integer(2, 'Maximum scaling factor') max_scale = min((max_scale, 3)) min_scale = auxil.select_integer(0, 'Minimum scaling factor') min_scale = min((max_scale, min_scale)) T0 = auxil.select_float(0.5, 'Initial annealing temperature') beta = auxil.select_float(0.5, 'Spatial mixing parameter') outfile, outfmt = auxil.select_outfilefmt( 'Select output classification file') if not outfile: return probfile, probfmt = auxil.select_outfilefmt( 'Select output probability file (optional)') print '=========================' print ' EM clustering' print '=========================' print 'infile: %s' % infile print 'clusters: %i' % K print 'T0: %f' % T0 print 'beta: %f' % beta start = time.time() # read in image and compress 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 = np.transpose( np.array([ DWTbands[i].get_quadrant(0, float=True).ravel() for i in range(bands) ])) # 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 = gdal.GetDriverByName(outfmt) 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 probfile: driver = gdal.GetDriverByName(probfmt) 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 if (outfmt == 'ENVI') and (K < 19): # try to make an ENVI classification header file hdr = header.Header() headerfile = outfile + '.hdr' f = open(headerfile) line = f.readline() envihdr = '' while line: envihdr += line line = f.readline() f.close() hdr.read(envihdr) hdr['file type'] = 'ENVI Classification' hdr['classes'] = str(K + 1) classlookup = '{0' for i in range(1, 3 * (K + 1)): classlookup += ', ' + str(str(ctable[i])) classlookup += '}' hdr['class lookup'] = classlookup hdr['class names'] = ['class %i' % i for i in range(K + 1)] f = open(headerfile, 'w') f.write(str(hdr)) f.close() print 'classification written to: ' + outfile print 'elapsed time: ' + str(time.time() - start) print '--done------------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) infile = auxil.select_infile(title='Select an image') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) if not pos: return dims = auxil.select_dims([0,0,cols,rows]) if dims: x0,y0,cols,rows = dims else: return m = auxil.select_integer(1000,'Select training sample size') K = auxil.select_integer(6,'Select number of clusters') outfile, outfmt = auxil.select_outfilefmt() if not outfile: return kernel = auxil.select_integer(1,'Select kernel: 0=linear, 1=Gaussian') print '=========================' print ' kkmeans' print '=========================' print 'infile: '+infile print 'samples: '+str(m) if kernel == 0: print 'kernel: '+'linear' else: print 'kernel: '+'Gaussian' start = time.time() # input data matrix XX = np.zeros((cols*rows,bands)) k = 0 for b in pos: band = inDataset.GetRasterBand(b) band = band.ReadAsArray(x0,y0,cols,rows).astype(float) XX[:,k] = np.ravel(band) k += 1 # training data matrix idx = np.fix(np.random.random(m)*(cols*rows)).astype(np.integer) X = XX[idx,:] print 'kernel matrix...' # uncentered kernel matrix KK, gma = auxil.kernelMatrix(X,kernel=kernel) if gma is not None: print 'gamma: '+str(round(gma,6)) # initial (random) class labels labels = np.random.randint(K,size = m) # iteration change = True itr = 0 onesm = np.mat(np.ones(m,dtype=float)) while change and (itr < 100): change = False U = np.zeros((K,m)) for i in range(m): U[labels[i],i] = 1 M = np.diag(1.0/(np.sum(U,axis=1)+1.0)) MU = np.mat(np.dot(M,U)) Z = (onesm.T)*np.diag(MU*KK*(MU.T)) - 2*KK*(MU.T) Z = np.array(Z) labels1 = (np.argmin(Z,axis=1) % K).ravel() if np.sum(labels1 != labels): change = True labels = labels1 itr += 1 print 'iterations: %i'%itr # classify image print 'classifying...' i = 0 A = np.diag(MU*KK*(MU.T)) A = np.tile(A,(cols,1)) class_image = np.zeros((rows,cols),dtype=np.byte) while i < rows: XXi = XX[i*cols:(i+1)*cols,:] KKK,_ = auxil.kernelMatrix(X,XXi,gma=gma,kernel=kernel) Z = A - 2*(KKK.T)*(MU.T) Z= np.array(Z) labels = np.argmin(Z,axis=1).ravel() class_image[i,:] = (labels % K) +1 i += 1 sys.stdout.write("\n") # write to disk driver = gdal.GetDriverByName(outfmt) outDataset = driver.Create(outfile,cols,rows,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 inDataset = None if (outfmt == 'ENVI') and (K<19): # try to make an ENVI classification header file hdr = header.Header() headerfile = outfile+'.hdr' f = open(headerfile) line = f.readline() envihdr = '' while line: envihdr += line line = f.readline() f.close() hdr.read(envihdr) hdr['file type'] ='ENVI Classification' hdr['classes'] = str(K) classlookup = '{0' for i in range(1,3*K): classlookup += ', '+str(str(ctable[i])) classlookup +='}' hdr['class lookup'] = classlookup hdr['class names'] = [str(i+1) for i in range(K)] f = open(headerfile,'w') f.write(str(hdr)) f.close() print 'result written to: '+outfile print 'elapsed time: '+str(time.time()-start) print '--done------------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # MS image file1 = auxil.select_infile(title='Choose MS image') if file1: inDataset1 = gdal.Open(file1,GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return num_bands = len(pos1) dims = auxil.select_dims([0,0,cols,rows]) if dims: x10,y10,cols1,rows1 = dims else: return # PAN image file2 = auxil.select_infile(title='Choose PAN image') if file2: inDataset2 = gdal.Open(file2,GA_ReadOnly) bands = inDataset2.RasterCount else: return if bands>1: print 'Must be a single band (panchromatic) image' return geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # resolution ratio ratio = auxil.select_integer(4, 'Resolution ratio (2 or 4)') if not ratio: return # MS registration band k1 = auxil.select_integer(1, 'MS band for registration') if not k1: return # fine adjust roll = auxil.select_integer(0, 'Fine adjust (-2 ... 2)') if roll is None: return 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-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 if roll != 0: # fine adjust PAN = np.roll(PAN,roll,axis=0) PAN = np.roll(PAN,roll,axis=1) panDWT = auxil.DWTArray(PAN,cols2,rows2) r = ratio while r > 1: panDWT.filter() r /= 2 # 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 = gdal.GetDriverByName(fmt) 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(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # reference image file1 = auxil.select_infile(title='Choose reference image') if file1: inDataset1 = gdal.Open(file1, GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x10, y10, cols1, rows1 = dims else: return # target image file2 = auxil.select_infile(title='Choose target image') if file2: inDataset2 = gdal.Open(file2, GA_ReadOnly) cols = inDataset2.RasterXSize rows = inDataset2.RasterYSize bands = inDataset2.RasterCount else: return pos2 = auxil.select_pos(bands) if not pos2: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x20, y20, cols2, rows2 = dims else: return # match dimensions bands = len(pos2) if (rows1 != rows2) or (cols1 != cols2) or (len(pos1) != bands): sys.stderr.write("Size mismatch") sys.exit(1) # iMAD image file3 = auxil.select_infile(title='Choose iMAD image') if file3: inDataset3 = gdal.Open(file3, GA_ReadOnly) cols = inDataset3.RasterXSize rows = inDataset3.RasterYSize imadbands = inDataset3.RasterCount else: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x30, y30, cols, rows = dims else: return if (rows1 != rows) or (cols1 != cols): sys.stderr.write("Size mismatch") sys.exit(1) # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # full scene fsfile = auxil.select_infile(title='Choose full target scene if desired') # no-change threshold ncpThresh = auxil.select_ncp(0.95) if ncpThresh is None: return chisqr = inDataset3.GetRasterBand(imadbands).ReadAsArray( x30, y30, cols, rows).ravel() ncp = 1 - stats.chi2.cdf(chisqr, [imadbands - 1]) idx = np.where(ncp > ncpThresh)[0] # split train/test in ratio 2:1 tmp = np.asarray(range(len(idx))) tst = idx[np.where(np.mod(tmp, 3) == 0)] trn = idx[np.where(np.mod(tmp, 3) > 0)] print '=========================================' print ' RADCAL' print '=========================================' print time.asctime() print 'reference: ' + file1 print 'target : ' + file2 print 'no-change probability threshold: ' + str(ncpThresh) print 'no-change pixels (train): ' + str(len(trn)) print 'no-change pixels (test): ' + str(len(tst)) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, bands, GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.GetGeoTransform() if geotransform is not None: gt = list(geotransform) gt[0] = gt[0] + x10 * gt[1] gt[3] = gt[3] + y10 * gt[5] outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) aa = [] bb = [] i = 1 for k in pos1: x = inDataset1.GetRasterBand(k).ReadAsArray( x10, y10, cols, rows).astype(float).ravel() y = inDataset2.GetRasterBand(k).ReadAsArray( x20, y20, cols, rows).astype(float).ravel() b, a, R = auxil.orthoregress(y[trn], x[trn]) print '--------------------' print 'spectral band: ', k print 'slope: ', b print 'intercept: ', a print 'correlation: ', R print 'means(tgt,ref,nrm): ', np.mean(y[tst]), np.mean( x[tst]), np.mean(a + b * y[tst]) print 't-test, p-value: ', stats.ttest_rel(x[tst], a + b * y[tst]) print 'vars(tgt,ref,nrm) ', np.var(y[tst]), np.var( x[tst]), np.var(a + b * y[tst]) print 'F-test, p-value: ', auxil.fv_test(x[tst], a + b * y[tst]) aa.append(a) bb.append(b) outBand = outDataset.GetRasterBand(i) outBand.WriteArray(np.resize(a + b * y, (rows, cols)), 0, 0) outBand.FlushCache() if i <= 10: plt.figure(i) ymax = max(y[idx]) xmax = max(x[idx]) plt.plot(y[idx], x[idx], 'k.', [0, ymax], [a, a + b * ymax], 'k-') plt.axis([0, ymax, 0, xmax]) plt.title('Band ' + str(k)) plt.xlabel('Target') plt.ylabel('Reference') i += 1 outDataset = None print 'result written to: ' + outfile if fsfile is not None: path = os.path.dirname(fsfile) basename = os.path.basename(fsfile) root, ext = os.path.splitext(basename) fsoutfile = path + '/' + root + '_norm' + ext print 'normalizing ' + fsfile + '...' fsDataset = gdal.Open(fsfile, GA_ReadOnly) cols = fsDataset.RasterXSize rows = fsDataset.RasterYSize driver = fsDataset.GetDriver() outDataset = driver.Create(fsoutfile, cols, rows, bands, GDT_Float32) projection = fsDataset.GetProjection() geotransform = fsDataset.GetGeoTransform() if geotransform is not None: outDataset.SetGeoTransform(geotransform) if projection is not None: outDataset.SetProjection(projection) j = 0 for k in pos2: inBand = fsDataset.GetRasterBand(k) outBand = outDataset.GetRasterBand(j + 1) for i in range(rows): y = inBand.ReadAsArray(0, i, cols, 1) outBand.WriteArray(aa[j] + bb[j] * y, 0, i) outBand.FlushCache() j += 1 outDataset = None print 'result written to: ' + fsoutfile plt.show() print '-------done-----------------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) infile = auxil.select_infile(title='Select an image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) if not pos: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x0, y0, cols, rows = dims else: return m = auxil.select_integer(1000, 'Select training sample size') K = auxil.select_integer(6, 'Select number of clusters') outfile, outfmt = auxil.select_outfilefmt() if not outfile: return kernel = auxil.select_integer(1, 'Select kernel: 0=linear, 1=Gaussian') print '=========================' print ' kkmeans' print '=========================' print 'infile: ' + infile print 'samples: ' + str(m) if kernel == 0: print 'kernel: ' + 'linear' else: print 'kernel: ' + 'Gaussian' start = time.time() # input data matrix XX = np.zeros((cols * rows, bands)) k = 0 for b in pos: band = inDataset.GetRasterBand(b) band = band.ReadAsArray(x0, y0, cols, rows).astype(float) XX[:, k] = np.ravel(band) k += 1 # training data matrix idx = np.fix(np.random.random(m) * (cols * rows)).astype(np.integer) X = XX[idx, :] print 'kernel matrix...' # uncentered kernel matrix KK, gma = auxil.kernelMatrix(X, kernel=kernel) if gma is not None: print 'gamma: ' + str(round(gma, 6)) # initial (random) class labels labels = np.random.randint(K, size=m) # iteration change = True itr = 0 onesm = np.mat(np.ones(m, dtype=float)) while change and (itr < 100): change = False U = np.zeros((K, m)) for i in range(m): U[labels[i], i] = 1 M = np.diag(1.0 / (np.sum(U, axis=1) + 1.0)) MU = np.mat(np.dot(M, U)) Z = (onesm.T) * np.diag(MU * KK * (MU.T)) - 2 * KK * (MU.T) Z = np.array(Z) labels1 = (np.argmin(Z, axis=1) % K).ravel() if np.sum(labels1 != labels): change = True labels = labels1 itr += 1 print 'iterations: %i' % itr # classify image print 'classifying...' i = 0 A = np.diag(MU * KK * (MU.T)) A = np.tile(A, (cols, 1)) class_image = np.zeros((rows, cols), dtype=np.byte) while i < rows: XXi = XX[i * cols:(i + 1) * cols, :] KKK, _ = auxil.kernelMatrix(X, XXi, gma=gma, kernel=kernel) Z = A - 2 * (KKK.T) * (MU.T) Z = np.array(Z) labels = np.argmin(Z, axis=1).ravel() class_image[i, :] = (labels % K) + 1 i += 1 sys.stdout.write("\n") # write to disk driver = gdal.GetDriverByName(outfmt) outDataset = driver.Create(outfile, cols, rows, 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 inDataset = None if (outfmt == 'ENVI') and (K < 19): # try to make an ENVI classification header file hdr = header.Header() headerfile = outfile + '.hdr' f = open(headerfile) line = f.readline() envihdr = '' while line: envihdr += line line = f.readline() f.close() hdr.read(envihdr) hdr['file type'] = 'ENVI Classification' hdr['classes'] = str(K) classlookup = '{0' for i in range(1, 3 * K): classlookup += ', ' + str(str(ctable[i])) classlookup += '}' hdr['class lookup'] = classlookup hdr['class names'] = [str(i + 1) for i in range(K)] f = open(headerfile, 'w') f.write(str(hdr)) f.close() print 'result written to: ' + outfile print 'elapsed time: ' + str(time.time() - start) print '--done------------------------'
def main(): gdal.AllRegister() infile = auxil.select_infile() if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spectral and spatial subsets pos = auxil.select_pos(bands) bands = len(pos) x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # data matrix G = zeros((rows*cols,len(pos))) k = 0 for b in pos: band = inDataset.GetRasterBand(b) tmp = band.ReadAsArray(x0,y0,cols,rows)\ .astype(float).ravel() G[:,k] = tmp - mean(tmp) k += 1 # covariance matrix C = mat(G).T*mat(G)/(cols*rows-1) # diagonalize lams,U = linalg.eigh(C) # sort idx = argsort(lams)[::-1] lams = lams[idx] U = U[:,idx] # project PCs = reshape(array(G*U),(rows,cols,bands)) # write to disk outfile,fmt = auxil.select_outfilefmt() if outfile: driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols,rows,bands,GDT_Float32) 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) for k in range(bands): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(PCs[:,:,k],0,0) outBand.FlushCache() outDataset = None inDataset = None
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # number of looks m = auxil.select_integer(5,msg='Number of looks') if not m: return # output file outfile,fmt = auxil.select_outfilefmt() if not outfile: return # get filter weights from span image b = np.ones((rows,cols)) band = inDataset.GetRasterBand(1) span = band.ReadAsArray(x0,y0,cols,rows).ravel() if bands==9: band = inDataset.GetRasterBand(6) span += band.ReadAsArray(x0,y0,cols,rows).ravel() band = inDataset.GetRasterBand(9) span += band.ReadAsArray(x0,y0,cols,rows).ravel() elif bands==4: band = inDataset.GetRasterBand(4) span += band.ReadAsArray(x0,y0,cols,rows).ravel() edge_idx = np.zeros((rows,cols),dtype=int) print '=========================' print ' MMSE_FILTER' print '=========================' print time.asctime() print 'infile: %s'%infile print 'number of looks: %i'%m print 'Determining filter weights from span image' start = time.time() print 'row: ', sys.stdout.flush() for j in range(3,rows-3): if j%50 == 0: print '%i '%j, sys.stdout.flush() windex = get_windex(j,cols) for i in range(3,cols-3): wind = np.reshape(span[windex],(7,7)) # 3x3 compression w = congrid.congrid(wind,(3,3),method='spline',centre=True) # get appropriate edge mask es = [np.sum(edges[p]*w) for p in range(4)] idx = np.argmax(es) if idx == 0: if np.abs(w[1,1]-w[1,0]) < np.abs(w[1,1]-w[1,2]): edge_idx[j,i] = 0 else: edge_idx[j,i] = 4 elif idx == 1: if np.abs(w[1,1]-w[2,0]) < np.abs(w[1,1]-w[0,2]): edge_idx[j,i] = 1 else: edge_idx[j,i] = 5 elif idx == 2: if np.abs(w[1,1]-w[0,1]) < np.abs(w[1,1]-w[2,1]): edge_idx[j,i] = 6 else: edge_idx[j,i] = 2 elif idx == 3: if np.abs(w[1,1]-w[0,0]) < np.abs(w[1,1]-w[2,2]): edge_idx[j,i] = 7 else: edge_idx[j,i] = 3 edge = templates[edge_idx[j,i]] wind = wind.ravel()[edge] gbar = np.mean(wind) varg = np.var(wind) if varg > 0: b[j,i] = np.max( ((1.0 - gbar**2/(varg*m))/(1.0+1.0/m), 0.0) ) windex += 1 print ' done' # filter the image outim = np.zeros((rows,cols),dtype=np.float32) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile,cols,rows,bands,GDT_Float32) 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)) projection = inDataset.GetProjection() if projection is not None: outDataset.SetProjection(projection) print 'Filtering covariance matrix elememnts' for k in range(1,bands+1): print 'band: %i'%(k) band = inDataset.GetRasterBand(k) band = band.ReadAsArray(0,0,cols,rows) gbar = band*0.0 # get window means for j in range(3,rows-3): windex = get_windex(j,cols) for i in range(3,cols-3): wind = band.ravel()[windex] edge = templates[edge_idx[j,i]] wind = wind[edge] gbar[j,i] = np.mean(wind) windex += 1 # apply adaptive filter and write to disk outim = np.reshape(gbar + b*(band-gbar),(rows,cols)) outBand = outDataset.GetRasterBand(k) outBand.WriteArray(outim,0,0) outBand.FlushCache() outDataset = None print 'result written to: '+outfile print 'elapsed time: '+str(time.time()-start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0, y0, rows, cols = auxil.select_dims([0, 0, rows, cols]) # number of looks m = auxil.select_integer(5, msg='Number of looks') if not m: return # number of iterations niter = auxil.select_integer(1, msg='Number of iterations') # output file outfile, fmt = auxil.select_outfilefmt() if not outfile: return # process diagonal bands only driver = gdal.GetDriverByName(fmt) if bands == 9: outDataset = driver.Create(outfile, cols, rows, 3, GDT_Float32) inimage = np.zeros((3, rows, cols)) band = inDataset.GetRasterBand(1) inimage[0] = band.ReadAsArray(x0, y0, cols, rows) band = inDataset.GetRasterBand(6) inimage[1] = band.ReadAsArray(x0, y0, cols, rows) band = inDataset.GetRasterBand(9) inimage[2] = band.ReadAsArray(x0, y0, cols, rows) elif bands == 4: outDataset = driver.Create(outfile, cols, rows, 2, GDT_Float32) inimage = np.zeros((2, rows, cols)) band = inDataset.GetRasterBand(1) inimage[0] = band.ReadAsArray(x0, y0, cols, rows) band = inDataset.GetRasterBand(4) inimage[1] = band.ReadAsArray(x0, y0, cols, rows) else: outDataset = driver.Create(outfile, cols, rows, 1, GDT_Float32) inimage = inDataset.GetRasterBand(1) outimage = np.copy(inimage) print '=========================' print ' GAMMA MAP FILTER' print '=========================' print time.asctime() print 'infile: %s' % infile print 'number of looks: %i' % m print 'number of iterations: %i' % niter start = time.time() itr = 0 while itr < niter: print 'iteration %i' % (itr + 1) if bands == 9: for k in range(3): outimage[k] = gamma_filter(k, inimage, outimage, rows, cols, m) elif bands == 4: for k in range(2): outimage[k] = gamma_filter(k, inimage, outimage, rows, cols, m) else: outimage = gamma_filter(0, inimage, outimage, rows, cols, m) itr += 1 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)) projection = inDataset.GetProjection() if projection is not None: outDataset.SetProjection(projection) if bands == 9: for k in range(3): outBand = outDataset.GetRasterBand(k + 1) outBand.WriteArray(outimage[k], 0, 0) outBand.FlushCache() elif bands == 4: for k in range(2): outBand = outDataset.GetRasterBand(k + 1) outBand.WriteArray(outimage[k], 0, 0) outBand.FlushCache() else: outBand = outDataset.GetRasterBand(1) outBand.WriteArray(outimage, 0, 0) outBand.FlushCache() outDataset = None print 'result written to: ' + outfile print 'elapsed time: ' + str(time.time() - start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) infile = auxil.select_infile(title='Select an image') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) if not pos: return bands = len(pos) dims = auxil.select_dims([0,0,cols,rows]) if dims: x0,y0,cols,rows = dims else: return class_image = np.zeros((rows,cols),dtype=np.byte) K = auxil.select_integer(6,'Number of clusters') max_scale = auxil.select_integer(2,'Maximum scaling factor') max_scale = min((max_scale,3)) min_scale = auxil.select_integer(0,'Minimum scaling factor') min_scale = min((max_scale,min_scale)) T0 = auxil.select_float(0.5,'Initial annealing temperature') beta = auxil.select_float(0.5,'Spatial mixing parameter') outfile, outfmt = auxil.select_outfilefmt('Select output classification file') if not outfile: return probfile, probfmt = auxil.select_outfilefmt('Select output probability file (optional)') print '=========================' print ' EM clustering' print '=========================' print 'infile: %s'%infile print 'clusters: %i'%K print 'T0: %f'%T0 print 'beta: %f'%beta start = time.time() # read in image and compress 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 = np.transpose(np.array([DWTbands[i].get_quadrant(0,float=True).ravel() for i in range(bands)])) # 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 = gdal.GetDriverByName(outfmt) 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 probfile: driver = gdal.GetDriverByName(probfmt) 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 if (outfmt == 'ENVI') and (K<19): # try to make an ENVI classification header file hdr = header.Header() headerfile = outfile+'.hdr' f = open(headerfile) line = f.readline() envihdr = '' while line: envihdr += line line = f.readline() f.close() hdr.read(envihdr) hdr['file type'] ='ENVI Classification' hdr['classes'] = str(K+1) classlookup = '{0' for i in range(1,3*(K+1)): classlookup += ', '+str(str(ctable[i])) classlookup +='}' hdr['class lookup'] = classlookup hdr['class names'] = ['class %i'%i for i in range(K+1)] f = open(headerfile,'w') f.write(str(hdr)) f.close() print 'classification written to: '+outfile print 'elapsed time: '+str(time.time()-start) print '--done------------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # get (spatial subset of) the C11 or C33 file first file1 = auxil.select_infile( title='Choose one componenst (C11, C22 or C33) ') if file1: inDataset1 = gdal.Open(file1, GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return inDataset = None # spatial subset x0, y0, cols, rows = auxil.select_dims([0, 0, cols, rows]) # output file outfile, fmt = auxil.select_outfilefmt() if not outfile: return # output image outim = np.zeros((9, rows, cols), dtype=np.float32) # get list of all files files = os.listdir(path) for afile in files: if re.search('hdr|sml', afile): continue # single polarimetry if re.search('pwr_geo', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[0, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None # dual and quad polarimetry elif re.search('hh_hh_geo|C11\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[0, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('re_hh_hv_geo|C12_real\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[1, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('im_hh_hv_geo|C12_imag\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[2, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('re_hh_vv_geo|C13_real\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[3, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('im_hh_vv_geo|C13_imag\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[4, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('hv_hv_geo|C22\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[5, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('re_hv_vv_geo|C23_real\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[6, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('im_hv_vv_geo|C23_imag\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[7, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None elif re.search('vv_vv_geo|C33\.tif', afile): inDataset = gdal.Open(afile, GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[8, :, :] = band.ReadAsArray(x0, y0, cols, rows) inDataset = None outim = np.nan_to_num(outim) idx = np.where(np.sum(np.abs(outim), axis=(1, 2)) > 0)[0] if idx == []: print 'no polarimetric bands found' return bands = len(idx) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, bands, GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.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) for k in range(bands): outBand = outDataset.GetRasterBand(k + 1) outBand.WriteArray(outim[idx[k], :, :], 0, 0) outBand.FlushCache() outDataset = None print '%i-band polarimetric image written to: %s' % (bands, outfile)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile,GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # number of looks m = auxil.select_integer(5,msg='Number of looks') if not m: return # number of iterations niter = auxil.select_integer(1,msg='Number of iterations') # output file outfile,fmt = auxil.select_outfilefmt() if not outfile: return # process diagonal bands only driver = gdal.GetDriverByName(fmt) if bands == 9: outDataset = driver.Create(outfile,cols,rows,3,GDT_Float32) inimage = np.zeros((3,rows,cols)) band = inDataset.GetRasterBand(1) inimage[0] = band.ReadAsArray(x0,y0,cols,rows) band = inDataset.GetRasterBand(6) inimage[1] = band.ReadAsArray(x0,y0,cols,rows) band = inDataset.GetRasterBand(9) inimage[2] = band.ReadAsArray(x0,y0,cols,rows) elif bands == 4: outDataset = driver.Create(outfile,cols,rows,2,GDT_Float32) inimage = np.zeros((2,rows,cols)) band = inDataset.GetRasterBand(1) inimage[0] = band.ReadAsArray(x0,y0,cols,rows) band = inDataset.GetRasterBand(4) inimage[1] = band.ReadAsArray(x0,y0,cols,rows) else: outDataset = driver.Create(outfile,cols,rows,1,GDT_Float32) inimage = inDataset.GetRasterBand(1) outimage = np.copy(inimage) print '=========================' print ' GAMMA MAP FILTER' print '=========================' print time.asctime() print 'infile: %s'%infile print 'number of looks: %i'%m print 'number of iterations: %i'%niter start = time.time() itr = 0 while itr < niter: print 'iteration %i'%(itr+1) if bands == 9: for k in range(3): outimage[k] = gamma_filter(k,inimage,outimage,rows,cols,m) elif bands == 4: for k in range(2): outimage[k] = gamma_filter(k,inimage,outimage,rows,cols,m) else: outimage = gamma_filter(0,inimage,outimage,rows,cols,m) itr += 1 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)) projection = inDataset.GetProjection() if projection is not None: outDataset.SetProjection(projection) if bands == 9: for k in range(3): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(outimage[k],0,0) outBand.FlushCache() elif bands == 4: for k in range(2): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(outimage[k],0,0) outBand.FlushCache() else: outBand = outDataset.GetRasterBand(1) outBand.WriteArray(outimage,0,0) outBand.FlushCache() outDataset = None print 'result written to: '+outfile print 'elapsed time: '+str(time.time()-start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # first image file1 = auxil.select_infile(title='Choose first image') if file1: inDataset1 = gdal.Open(file1,GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return dims = auxil.select_dims([0,0,cols,rows]) if dims: x10,y10,cols1,rows1 = dims else: return # second image file2 = auxil.select_infile(title='Choose second image') if file2: inDataset2 = gdal.Open(file2,GA_ReadOnly) cols = inDataset2.RasterXSize rows = inDataset2.RasterYSize bands = inDataset2.RasterCount else: return pos2 = auxil.select_pos(bands) if not pos2: return dims=auxil.select_dims([0,0,cols,rows]) if dims: x20,y20,cols,rows = dims else: return # penalization lam = auxil.select_penal(0.0) if lam is None: return # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # match dimensions bands = len(pos2) if (rows1 != rows) or (cols1 != cols) or (len(pos1) != bands): sys.stderr.write("Size mismatch") sys.exit(1) print '=========================' print ' iMAD' print '=========================' print time.asctime() print 'time1: '+file1 print 'time2: '+file2 print 'Delta [canonical correlations]' # iteration of MAD cpm = auxil.Cpm(2*bands) delta = 1.0 oldrho = np.zeros(bands) itr = 0 tile = np.zeros((cols,2*bands)) sigMADs = 0 means1 = 0 means2 = 0 A = 0 B = 0 rasterBands1 = [] rasterBands2 = [] for b in pos1: rasterBands1.append(inDataset1.GetRasterBand(b)) for b in pos2: rasterBands2.append(inDataset2.GetRasterBand(b)) while (delta > 0.001) and (itr < 100): # spectral tiling for statistics for row in range(rows): for k in range(bands): tile[:,k] = rasterBands1[k].ReadAsArray(x10,y10+row,cols,1) tile[:,bands+k] = rasterBands2[k].ReadAsArray(x20,y20+row,cols,1) # eliminate no-data pixels (assuming all zeroes) tst1 = np.sum(tile[:,0:bands],axis=1) tst2 = np.sum(tile[:,bands::],axis=1) idx1 = set(np.where( (tst1>0) )[0]) idx2 = set(np.where( (tst2>0) )[0]) idx = list(idx1.intersection(idx2)) if itr>0: mads = np.asarray((tile[:,0:bands]-means1)*A - (tile[:,bands::]-means2)*B) chisqr = np.sum((mads/sigMADs)**2,axis=1) wts = 1-stats.chi2.cdf(chisqr,[bands]) cpm.update(tile[idx,:],wts[idx]) else: cpm.update(tile[idx,:]) # weighted covariance matrices and means S = cpm.covariance() means = cpm.means() # reset prov means object cpm.__init__(2*bands) s11 = S[0:bands,0:bands] s11 = (1-lam)*s11 + lam*np.eye(bands) s22 = S[bands:,bands:] s22 = (1-lam)*s22 + lam*np.eye(bands) s12 = S[0:bands,bands:] s21 = S[bands:,0:bands] c1 = s12*linalg.inv(s22)*s21 b1 = s11 c2 = s21*linalg.inv(s11)*s12 b2 = s22 # solution of generalized eigenproblems if bands>1: mu2a,A = auxil.geneiv(c1,b1) mu2b,B = auxil.geneiv(c2,b2) # sort a idx = np.argsort(mu2a) A = A[:,idx] # sort b idx = np.argsort(mu2b) B = B[:,idx] mu2 = mu2b[idx] else: mu2 = c1/b1 A = 1/np.sqrt(b1) B = 1/np.sqrt(b2) # canonical correlations mu = np.sqrt(mu2) a2 = np.diag(A.T*A) b2 = np.diag(B.T*B) sigma = np.sqrt( (2-lam*(a2+b2))/(1-lam)-2*mu ) rho=mu*(1-lam)/np.sqrt( (1-lam*a2)*(1-lam*b2) ) # stopping criterion delta = max(abs(rho-oldrho)) print delta,rho oldrho = rho # tile the sigmas and means sigMADs = np.tile(sigma,(cols,1)) means1 = np.tile(means[0:bands],(cols,1)) means2 = np.tile(means[bands::],(cols,1)) # ensure sum of positive correlations between X and U is positive D = np.diag(1/np.sqrt(np.diag(s11))) s = np.ravel(np.sum(D*s11*A,axis=0)) A = A*np.diag(s/np.abs(s)) # ensure positive correlation between each pair of canonical variates cov = np.diag(A.T*s12*B) B = B*np.diag(cov/np.abs(cov)) itr += 1 # write results to disk driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile,cols,rows,bands+1,GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.GetGeoTransform() if geotransform is not None: gt = list(geotransform) gt[0] = gt[0] + x10*gt[1] gt[3] = gt[3] + y10*gt[5] outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) outBands = [] for k in range(bands+1): outBands.append(outDataset.GetRasterBand(k+1)) for row in range(rows): for k in range(bands): tile[:,k] = rasterBands1[k].ReadAsArray(x10,y10+row,cols,1) tile[:,bands+k] = rasterBands2[k].ReadAsArray(x20,y20+row,cols,1) mads = np.asarray((tile[:,0:bands]-means1)*A - (tile[:,bands::]-means2)*B) chisqr = np.sum((mads/sigMADs)**2,axis=1) for k in range(bands): outBands[k].WriteArray(np.reshape(mads[:,k],(1,cols)),0,row) outBands[bands].WriteArray(np.reshape(chisqr,(1,cols)),0,row) for outBand in outBands: outBand.FlushCache() outDataset = None inDataset1 = None inDataset2 = None print 'result written to: '+outfile print '--------done---------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # SAR image infile = auxil.select_infile(title='Choose SAR image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return # spatial subset x0, y0, rows, cols = auxil.select_dims([0, 0, rows, cols]) # number of looks m = auxil.select_integer(5, msg='Number of looks') if not m: return # output file outfile, fmt = auxil.select_outfilefmt() if not outfile: return # get filter weights from span image b = np.ones((rows, cols)) band = inDataset.GetRasterBand(1) span = band.ReadAsArray(x0, y0, cols, rows).ravel() if bands == 9: band = inDataset.GetRasterBand(6) span += band.ReadAsArray(x0, y0, cols, rows).ravel() band = inDataset.GetRasterBand(9) span += band.ReadAsArray(x0, y0, cols, rows).ravel() elif bands == 4: band = inDataset.GetRasterBand(4) span += band.ReadAsArray(x0, y0, cols, rows).ravel() edge_idx = np.zeros((rows, cols), dtype=int) print '=========================' print ' MMSE_FILTER' print '=========================' print time.asctime() print 'infile: %s' % infile print 'number of looks: %i' % m print 'Determining filter weights from span image' start = time.time() print 'row: ', sys.stdout.flush() for j in range(3, rows - 3): if j % 50 == 0: print '%i ' % j, sys.stdout.flush() windex = get_windex(j, cols) for i in range(3, cols - 3): wind = np.reshape(span[windex], (7, 7)) # 3x3 compression w = congrid.congrid(wind, (3, 3), method='spline', centre=True) # get appropriate edge mask es = [np.sum(edges[p] * w) for p in range(4)] idx = np.argmax(es) if idx == 0: if np.abs(w[1, 1] - w[1, 0]) < np.abs(w[1, 1] - w[1, 2]): edge_idx[j, i] = 0 else: edge_idx[j, i] = 4 elif idx == 1: if np.abs(w[1, 1] - w[2, 0]) < np.abs(w[1, 1] - w[0, 2]): edge_idx[j, i] = 1 else: edge_idx[j, i] = 5 elif idx == 2: if np.abs(w[1, 1] - w[0, 1]) < np.abs(w[1, 1] - w[2, 1]): edge_idx[j, i] = 6 else: edge_idx[j, i] = 2 elif idx == 3: if np.abs(w[1, 1] - w[0, 0]) < np.abs(w[1, 1] - w[2, 2]): edge_idx[j, i] = 7 else: edge_idx[j, i] = 3 edge = templates[edge_idx[j, i]] wind = wind.ravel()[edge] gbar = np.mean(wind) varg = np.var(wind) if varg > 0: b[j, i] = np.max( ((1.0 - gbar**2 / (varg * m)) / (1.0 + 1.0 / m), 0.0)) windex += 1 print ' done' # filter the image outim = np.zeros((rows, cols), dtype=np.float32) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, bands, GDT_Float32) 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)) projection = inDataset.GetProjection() if projection is not None: outDataset.SetProjection(projection) print 'Filtering covariance matrix elememnts' for k in range(1, bands + 1): print 'band: %i' % (k) band = inDataset.GetRasterBand(k) band = band.ReadAsArray(0, 0, cols, rows) gbar = band * 0.0 # get window means for j in range(3, rows - 3): windex = get_windex(j, cols) for i in range(3, cols - 3): wind = band.ravel()[windex] edge = templates[edge_idx[j, i]] wind = wind[edge] gbar[j, i] = np.mean(wind) windex += 1 # apply adaptive filter and write to disk outim = np.reshape(gbar + b * (band - gbar), (rows, cols)) outBand = outDataset.GetRasterBand(k) outBand.WriteArray(outim, 0, 0) outBand.FlushCache() outDataset = None print 'result written to: ' + outfile print 'elapsed time: ' + str(time.time() - start)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # reference image file1 = auxil.select_infile(title='Choose reference image') if file1: inDataset1 = gdal.Open(file1,GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return dims = auxil.select_dims([0,0,cols,rows]) if dims: x10,y10,cols1,rows1 = dims else: return # target image file2 = auxil.select_infile(title='Choose target image') if file2: inDataset2 = gdal.Open(file2,GA_ReadOnly) cols = inDataset2.RasterXSize rows = inDataset2.RasterYSize bands = inDataset2.RasterCount else: return pos2 = auxil.select_pos(bands) if not pos2: return dims=auxil.select_dims([0,0,cols,rows]) if dims: x20,y20,cols2,rows2 = dims else: return # match dimensions bands = len(pos2) if (rows1 != rows2) or (cols1 != cols2) or (len(pos1) != bands): sys.stderr.write("Size mismatch") sys.exit(1) # iMAD image file3 = auxil.select_infile(title='Choose iMAD image') if file3: inDataset3 = gdal.Open(file3,GA_ReadOnly) cols = inDataset3.RasterXSize rows = inDataset3.RasterYSize imadbands = inDataset3.RasterCount else: return dims=auxil.select_dims([0,0,cols,rows]) if dims: x30,y30,cols,rows = dims else: return if (rows1 != rows) or (cols1 != cols): sys.stderr.write("Size mismatch") sys.exit(1) # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # full scene fsfile = auxil.select_infile(title='Choose full target scene if desired') # no-change threshold ncpThresh = auxil.select_ncp(0.95) if ncpThresh is None: return chisqr = inDataset3.GetRasterBand(imadbands).ReadAsArray(x30,y30,cols,rows).ravel() ncp = 1 - stats.chi2.cdf(chisqr,[imadbands-1]) idx = np.where(ncp>ncpThresh)[0] # split train/test in ratio 2:1 tmp = np.asarray(range(len(idx))) tst = idx[np.where(np.mod(tmp,3) == 0)] trn = idx[np.where(np.mod(tmp,3) > 0)] print '=========================================' print ' RADCAL' print '=========================================' print time.asctime() print 'reference: '+file1 print 'target : '+file2 print 'no-change probability threshold: '+str(ncpThresh) print 'no-change pixels (train): '+str(len(trn)) print 'no-change pixels (test): '+str(len(tst)) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile,cols,rows,bands,GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.GetGeoTransform() if geotransform is not None: gt = list(geotransform) gt[0] = gt[0] + x10*gt[1] gt[3] = gt[3] + y10*gt[5] outDataset.SetGeoTransform(tuple(gt)) if projection is not None: outDataset.SetProjection(projection) aa = [] bb = [] i = 1 for k in pos1: x = inDataset1.GetRasterBand(k).ReadAsArray(x10,y10,cols,rows).astype(float).ravel() y = inDataset2.GetRasterBand(k).ReadAsArray(x20,y20,cols,rows).astype(float).ravel() b,a,R = auxil.orthoregress(y[trn],x[trn]) print '--------------------' print 'spectral band: ', k print 'slope: ', b print 'intercept: ', a print 'correlation: ', R print 'means(tgt,ref,nrm): ', np.mean(y[tst]),np.mean(x[tst]),np.mean(a+b*y[tst]) print 't-test, p-value: ', stats.ttest_rel(x[tst], a+b*y[tst]) print 'vars(tgt,ref,nrm) ', np.var(y[tst]),np.var(x[tst]),np.var(a+b*y[tst]) print 'F-test, p-value: ', auxil.fv_test(x[tst], a+b*y[tst]) aa.append(a) bb.append(b) outBand = outDataset.GetRasterBand(i) outBand.WriteArray(np.resize(a+b*y,(rows,cols)),0,0) outBand.FlushCache() if i <= 10: plt.figure(i) ymax = max(y[idx]) xmax = max(x[idx]) plt.plot(y[idx],x[idx],'k.',[0,ymax],[a,a+b*ymax],'k-') plt.axis([0,ymax,0,xmax]) plt.title('Band '+str(k)) plt.xlabel('Target') plt.ylabel('Reference') i += 1 outDataset = None print 'result written to: '+outfile if fsfile is not None: path = os.path.dirname(fsfile) basename = os.path.basename(fsfile) root, ext = os.path.splitext(basename) fsoutfile = path+'/'+root+'_norm'+ext print 'normalizing '+fsfile+'...' fsDataset = gdal.Open(fsfile,GA_ReadOnly) cols = fsDataset.RasterXSize rows = fsDataset.RasterYSize driver = fsDataset.GetDriver() outDataset = driver.Create(fsoutfile,cols,rows,bands,GDT_Float32) projection = fsDataset.GetProjection() geotransform = fsDataset.GetGeoTransform() if geotransform is not None: outDataset.SetGeoTransform(geotransform) if projection is not None: outDataset.SetProjection(projection) j = 0 for k in pos2: inBand = fsDataset.GetRasterBand(k) outBand = outDataset.GetRasterBand(j+1) for i in range(rows): y = inBand.ReadAsArray(0,i,cols,1) outBand.WriteArray(aa[j]+bb[j]*y,0,i) outBand.FlushCache() j += 1 outDataset = None print 'result written to: '+fsoutfile plt.show() print '-------done-----------------------------'
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # MS image file1 = auxil.select_infile(title='Choose MS image') if file1: inDataset1 = gdal.Open(file1, GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return pos1 = auxil.select_pos(bands) if not pos1: return num_bands = len(pos1) dims = auxil.select_dims([0, 0, cols, rows]) if dims: x10, y10, cols1, rows1 = dims else: return # PAN image file2 = auxil.select_infile(title='Choose PAN image') if file2: inDataset2 = gdal.Open(file2, GA_ReadOnly) bands = inDataset2.RasterCount else: return if bands > 1: print 'Must be a single band (panchromatic) image' return geotransform1 = inDataset1.GetGeoTransform() geotransform2 = inDataset2.GetGeoTransform() # outfile outfile, fmt = auxil.select_outfilefmt() if not outfile: return # resolution ratio ratio = auxil.select_integer(4, 'Resolution ratio (2 or 4)') if not ratio: return # MS registration band k1 = auxil.select_integer(1, 'MS band for registration') if not k1: return 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 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)) # 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 = gdal.GetDriverByName(fmt) 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(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) # get (spatial subset of) the C11 or C33 file first file1 = auxil.select_infile(title='Choose one componenst (C11, C22 or C33) ') if file1: inDataset1 = gdal.Open(file1,GA_ReadOnly) cols = inDataset1.RasterXSize rows = inDataset1.RasterYSize bands = inDataset1.RasterCount else: return inDataset = None # spatial subset x0,y0,rows,cols=auxil.select_dims([0,0,rows,cols]) # output file outfile,fmt = auxil.select_outfilefmt() if not outfile: return # output image outim = np.zeros((9,rows,cols), dtype=np.float32) # get list of all files files = os.listdir(path) for afile in files: if re.search('hdr|sml',afile): continue # single polarimetry if re.search('pwr_geo',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[0,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None # dual and quad polarimetry elif re.search('hh_hh_geo|C11\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[0,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('re_hh_hv_geo|C12_real\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[1,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('im_hh_hv_geo|C12_imag\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[2,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('re_hh_vv_geo|C13_real\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[3,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('im_hh_vv_geo|C13_imag\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[4,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('hv_hv_geo|C22\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[5,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('re_hv_vv_geo|C23_real\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[6,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('im_hv_vv_geo|C23_imag\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[7,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None elif re.search('vv_vv_geo|C33\.tif',afile): inDataset = gdal.Open(afile,GA_ReadOnly) band = inDataset.GetRasterBand(1) outim[8,:,:] = band.ReadAsArray(x0,y0,cols,rows) inDataset = None outim = np.nan_to_num(outim) idx = np.where(np.sum(np.abs(outim),axis=(1,2))>0)[0] if idx == []: print 'no polarimetric bands found' return bands = len(idx) driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile,cols,rows,bands,GDT_Float32) projection = inDataset1.GetProjection() geotransform = inDataset1.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) for k in range(bands): outBand = outDataset.GetRasterBand(k+1) outBand.WriteArray(outim[idx[k],:,:],0,0) outBand.FlushCache() outDataset = None print '%i-band polarimetric image written to: %s'%(bands,outfile)
def main(): gdal.AllRegister() path = auxil.select_directory('Choose working directory') if path: os.chdir(path) infile = auxil.select_infile(title='Select an image') if infile: inDataset = gdal.Open(infile, GA_ReadOnly) cols = inDataset.RasterXSize rows = inDataset.RasterYSize bands = inDataset.RasterCount else: return pos = auxil.select_pos(bands) if not pos: return dims = auxil.select_dims([0, 0, cols, rows]) if dims: x0, y0, cols, rows = dims else: return m = auxil.select_integer(2000, 'Select sample size (0 for k-means)') n = auxil.select_integer(10, 'Select number of eigenvalues') outfile, fmt = auxil.select_outfilefmt() if not outfile: return kernel = auxil.select_integer(1, 'Select kernel: 0=linear, 1=Gaussian') print '=========================' print ' kPCA' print '=========================' print 'infile: ' + infile print 'samples: ' + str(m) if kernel == 0: print 'kernel: ' + 'linear' else: print 'kernel: ' + 'Gaussian' start = time.time() if kernel == 0: n = min(bands, n) # construct data design matrices XX = zeros((cols * rows, bands)) k = 0 for b in pos: band = inDataset.GetRasterBand(b) band = band.ReadAsArray(x0, y0, cols, rows).astype(float) XX[:, k] = ravel(band) k += 1 if m > 0: idx = fix(random.random(m) * (cols * rows)).astype(integer) X = XX[idx, :] else: print 'running k-means on 100 cluster centers...' X, _ = kmeans(XX, 100, iter=1) m = 100 print 'centered kernel matrix...' # centered kernel matrix K, gma = auxil.kernelMatrix(X, kernel=kernel) meanK = sum(K) / (m * m) rowmeans = mat(sum(K, axis=0) / m) if gma is not None: print 'gamma: ' + str(round(gma, 6)) K = auxil.center(K) print 'diagonalizing...' # diagonalize try: w, v = linalg.eigh(K, eigvals=(m - n, m - 1)) idx = range(n) idx.reverse() w = w[idx] v = v[:, idx] # variance of PCs var = w / m except linalg.LinAlgError: print 'eigenvalue computation failed' sys.exit() # dual variables (normalized eigenvectors) alpha = mat(v) * mat(diag(1 / sqrt(w))) print 'projecting...' # projecting image = zeros((rows, cols, n)) for i in range(rows): XXi = XX[i * cols:(i + 1) * cols, :] KK, gma = auxil.kernelMatrix(X, XXi, kernel=kernel, gma=gma) # centering on training data: # subtract column means colmeans = mat(sum(KK, axis=0) / m) onesm = mat(ones(m)) KK = KK - onesm.T * colmeans # subtract row means onesc = mat(ones(cols)) KK = KK - rowmeans.T * onesc # add overall mean KK = KK + meanK # project image[i, :, :] = KK.T * alpha # write to disk driver = gdal.GetDriverByName(fmt) outDataset = driver.Create(outfile, cols, rows, n, GDT_Float32) 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) for k in range(n): outBand = outDataset.GetRasterBand(k + 1) outBand.WriteArray(image[:, :, k], 0, 0) outBand.FlushCache() outDataset = None inDataset = None print 'result written to: ' + outfile print 'elapsed time: ' + str(time.time() - start) plt.plot(range(1, n + 1), var, 'k-') plt.title('kernel PCA') plt.xlabel('principal component') plt.ylabel('Variance') plt.show() print '--done------------------------'