chisqr = np.asarray(outBands[bands]).ravel()
ncp = 1 - stats.chi2.cdf(chisqr, [bands - 1])
idx = np.where(ncp > ncpThresh)

aa = []
bb = []
j = 1
bands = len(pos)
# Cáculo ortoregresión con imagen referencia e imagen objetivo obteniendo
# valores de pendiente, intercepción y correlación entre las mismas.
outBand = []

for k in pos:
    x = np.asarray(rasterBands2[k - 1]).ravel()
    y = np.asarray(rasterBands1[k - 1]).ravel()
    b, a, R = orthoregress(y[idx], x[idx])
    print('band: {} slope: {} intercept: {} correlation: {}'.format(
        k, b, a, R))
    my = max(y[idx])
    aa.append(a)
    bb.append(b)
    outBand.append(np.zeros([rows, cols]))
    # Imagen normalizada resultante
    outBand[k - 1] = np.resize(a + b * y, (rows, cols))
    # outBand.FlushCache()
    j += 1

print('outband_f')
print(outBand)

ncoords = []
Exemple #2
0
def main(img_imad, ncpThresh=0.95, pos=None, dims=None, img_target=None, graphics=False):

    if img_target is not None:
        path = os.path.dirname(img_target)
        basename = os.path.basename(img_target)
        root, ext = os.path.splitext(basename)
        fsoutfn = path + '/' + root + '_norm_all' + ext

    path = os.path.dirname(img_imad)
    basename = os.path.basename(img_imad)
    root, ext = os.path.splitext(basename)
    b = root.find('(')
    err = root.find(')')
    referenceroot, targetbasename = root[b + 1:err].split('&')
    referencefn = path + '/' + referenceroot + ext
    targetfn = path + '/' + targetbasename
    targetroot, targetext = os.path.splitext(targetbasename)
    outfn = path + '/' + targetroot + '_norm' + targetext
    imadDataset = gdal.Open(img_imad, GA_ReadOnly)
    try:
        imadbands = imadDataset.RasterCount
        cols = imadDataset.RasterXSize
        rows = imadDataset.RasterYSize
    except Exception as err:
        print('Error: {}  --Image could not be read'.format(err))
        sys.exit(1)
    referenceDataset = gdal.Open(referencefn, GA_ReadOnly)
    targetDataset = gdal.Open(targetfn, GA_ReadOnly)
    if pos is None:
        pos = list(range(1, referenceDataset.RasterCount + 1))
    if dims is None:
        x0 = 0
        y0 = 0
    else:
        x0, y0, cols, rows = dims
    chisqr = imadDataset.GetRasterBand(imadbands).ReadAsArray(0, 0, cols, rows).ravel()
    ncp = 1 - stats.chi2.cdf(chisqr, [imadbands - 1])
    idx = where(ncp > ncpThresh)
    print(time.asctime())
    print('reference: ' + referencefn)
    print('target   : ' + targetfn)
    print('no-change probability threshold: ' + str(ncpThresh))
    print('no-change pixels: ' + str(len(idx[0])))
    start = time.time()
    driver = targetDataset.GetDriver()
    outDataset = driver.Create(outfn, cols, rows, len(pos), GDT_UInt16)
    projection = imadDataset.GetProjection()
    geotransform = imadDataset.GetGeoTransform()
    if geotransform is not None:
        outDataset.SetGeoTransform(geotransform)
    if projection is not None:
        outDataset.SetProjection(projection)
    aa = []
    bb = []
    if graphics:
        plt.figure(1, (9, 6))
    j = 1
    bands = len(pos)
    for k in pos:
        x = referenceDataset.GetRasterBand(k).ReadAsArray(x0, y0, cols, rows).astype(float).ravel()
        y = targetDataset.GetRasterBand(k).ReadAsArray(x0, y0, cols, rows).astype(float).ravel()
        b, a, R = orthoregress(y[idx], x[idx])
        print('band: {}  slope: {} intercept: {}  correlation: {}'.format(k, b, a, R))
        my = max(y[idx])
        if (j < 7) and graphics:
            plt.subplot(2, 3, j)
            plt.plot(y[idx], x[idx], '.')
            plt.plot([0, my], [a, a + b * my])
            plt.title('Band {}'.format(k))
            if ((j < 4) and (bands < 4)) or j > 3:
                plt.xlabel('Target')
            if (j == 1) or (j == 4):
                plt.ylabel('Reference')
        aa.append(a)
        bb.append(b)
        outBand = outDataset.GetRasterBand(j)
        outBand.WriteArray(resize(a + b * y, (rows, cols)), 0, 0)
        outBand.FlushCache()
        j += 1
    if graphics:
        plt.show()
        plt.close()
    referenceDataset = None
    targetDataset = None
    outDataset = None
    print('result written to: ' + outfn)
    if img_target is not None:
        print('normalizing ' + img_target + '...')
        fsDataset = gdal.Open(img_target, GA_ReadOnly)
        try:
            cols = fsDataset.RasterXSize
            rows = fsDataset.RasterYSize
        except Exception as err:
            print('Error %s  -- Image could not be read in')
            sys.exit(1)
        driver = fsDataset.GetDriver()
        outDataset = driver.Create(fsoutfn, cols, rows, len(pos), GDT_UInt16)
        projection = fsDataset.GetProjection()
        geotransform = fsDataset.GetGeoTransform()
        if geotransform is not None:
            outDataset.SetGeoTransform(geotransform)
        if projection is not None:
            outDataset.SetProjection(projection)
        j = 1
        for k in pos:
            inBand = fsDataset.GetRasterBand(k)
            outBand = outDataset.GetRasterBand(j)
            for i in range(rows):
                y = inBand.ReadAsArray(0, i, cols, 1)
                outBand.WriteArray(aa[j - 1] + bb[j - 1] * y, 0, i)
            outBand.FlushCache()
            j += 1
        outDataset = None
        fsDataset = None
        print('full result written to: ' + fsoutfn)
        return fsoutfn

    print('elapsed time: {}'.format(time.time() - start))
    return outfn