仅更正数组Python上的一个(细节)位置

2024-09-25 00:25:19 发布

您现在位置: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)))


Tags: inforbanddsresrgb像素rows