仅校正数组上的一个(特定)位置-Python

时间:2019-05-16 13:40:36

标签: python

我是一个新手,尝试用python做一个简单的代码来获取图像数据集,每个图像数据集包含4个波段(R,G,B,Nir)。 对于每一行(仅R,G,B),我只需要对某些特定像素(代表图像上水的像素)进行校正。 我的代码的第一步是使用使用原始图像生成的其他栅格文件来确定水上像素的位置(ii,jj)。用于识别水的栅格文件的大小与原始图像的大小相同。

由代表水的像素验证的条件是:

for ii in range(rows_ndwi):
            for jj in range (cols_ndwi):
                if ndwi[ii,jj] >= 0:

一旦发现每个波段需要校正的像素(II,JJ),我就将校正(每个波段)仅应用于水中的像素。

在此修复操作结束时,每个波段的数组将保留在水中没有确定位置的值,并且仅更改位于水中的像素。

我将分享我的代码中“尝试”应用这些更正的部分。

for i in range(size):
    i += 1
    os.chdir('D:/CESAR_PHD/10.Lyzenga/Test_30/raw_data_rgb_nir/')
    text_file = open('list.txt', 'r') 
    line = text_file.read().split() 
    size = len(line)
    nome = line[i-1]
    ds_res_rgb = gdal.Open(nome)
    rows = ds_res_rgb.RasterYSize
    cols = ds_res_rgb.RasterXSize 
    bands = ds_res_rgb.RasterCount
    print('Opening the red band of image ' + nome)
    banda_red = ds_res_rgb.GetRasterBand(1).ReadAsArray(0, 0, cols, rows)
    print('Opening the green band of image ' + nome)
    banda_green =ds_res_rgb.GetRasterBand(2).ReadAsArray(0, 0, cols, rows)
    print('Opening the blue band of image ' + nome)
    banda_blue =ds_res_rgb.GetRasterBand(3).ReadAsArray(0, 0, cols, rows)
       print('Opening the nir band of image ' + nome)
    nir_band = ds_res_rgb.GetRasterBand(3).ReadAsArray(0, 0, cols, rows)
    for j in range(size): 
        j += 1
        os.chdir('D:/CESAR_PHD/10.Lyzenga/Test_30/ndwi/')
        text_file_ndwi = open('list.txt', 'r') 
        line_ndwi = text_file_ndwi.read().split()
        size_ndwi = len(line_ndwi)      
        nome_ndwi = line_ndwi[j-1]
        ds_ndwi = gdal.Open(nome_ndwi)
        rows_ndwi = ds_ndwi.RasterYSize
        cols_ndwi = ds_ndwi.RasterXSize
        bands_ndwi = ds_ndwi.RasterCount
        print('Opening NDWI raster file from reservoir ' + nome_ndwi)
        ndwi = ds_ndwi.GetRasterBand(1).ReadAsArray(0, 0, cols_ndwi, rows_ndwi)
        print('Applyin the sun-glint correction for image ' + nome)
        for ii in range(rows_ndwi):
            for jj in range (cols_ndwi):
                if ndwi[ii,jj] >= 0: ## condition for found the positions for pixels that need to be correction
                    banda_red[ii,jj] = (banda_red[ii,jj] - (a[0] * (nir_band[ii,jj] - nir_dw)))
                    banda_green[ii,jj] = (banda_green[ii,jj] - (a[1] * (nir_band[ii,jj] - nir_dw)))
                    banda_blue[ii,jj] = (banda_blue[ii,jj] - (a[2] * (nir_band[ii,jj] - nir_dw)))

0 个答案:

没有答案