Esempio n. 1
0
def ndfi_post_process(im: str, mean_it: str, min_gt: str, output: str) -> None:
    """
    对的得到的ndfi经阈值划分后的二值图像进行后处理
    通过得到的掩膜图像去除后向散射系数均值小于0.015的和最小值大于0.03的
    再进行开运算
    :param im:
    :param mean_it:
    :param min_gt:
    :param output
    :return:
    """
    ndfi, im_width, im_height, im_bands = readImage(im, 1)

    mean_it, mean_width, mean_height, mean_bands = readImage(mean_it, 1)

    min_gt, min_width, min_height, min_bands = readImage(min_gt, 1)

    # 去除后向散射均值小于0.015(可认定为永久水体的部分)
    # 去除后向散射最小值大于0.3(在洪涝中发生了什么,但没有达到受灾的水平)
    temp = np.zeros((im_height, im_width), np.uint8)
    for i in range(0, im_height):
        for j in range(0, im_width):
            if ndfi[i][j] == 1 and mean_it[i][j] == 1 and min_gt[i][j] == 1:
                temp[i][j] = 1
            else:
                temp[i][j] = 0

    array2Raster(temp, output, refImg=im)
Esempio n. 2
0
def ndfi(pre_images: Union[str, list],
         post_images: Union[str, list]) -> Union[np.ndarray, list]:
    """
    有灾前灾后图像计算NDFI,要求所有数据在空间上是对齐的
    :param pre_images:
    :param post_images:
    :return:
    """

    # 存储灾前灾后影像的像素值
    pre_array = []
    post_array = []

    for img in pre_images:
        if isinstance(img, str):
            im, im_width, im_height, im_bands = readImage(img, 2)
        pre_array.append(im)

    for img in post_images:
        if isinstance(img, str):
            im, im_width, im_height, im_bands = readImage(img, 2)
        post_array.append(im)

    min_ref_flood = np.amin(pre_array + post_array, 0)
    mean_ref = np.mean(pre_array, 0)

    # 分析NDFI结果
    # 找出后向散射系数均值小于0.015
    # 最小值大于0.03的
    mean_it = np.ones((im_height, im_width), np.uint8)
    min_gt = np.ones((im_height, im_width), np.uint8)
    for i in range(0, im_height):
        for j in range(0, im_width):
            if mean_ref[i][j] <= 15e-3:
                mean_it[i][j] = 0
            else:
                mean_it[i][j] = 1

            if min_ref_flood[i][j] >= 3e-2:
                min_gt[i][j] = 0
            else:
                min_gt[i][j] = 1

    # 保存后向散射系数均值小于0.015和最小值大于0.03的为二值图像之后用作掩膜
    array2Raster(
        mean_it,
        '/home/Tuotianyu/数据/paper_based/mean_it_0.015_0608_0620_0714_0720.tif',
        refImg=
        'S1B_IW_GRDH_1SDV_20200608T101815_20200608T101851_021941_029A3A_7867.zip.tif'
    )
    array2Raster(
        min_gt,
        '/home/Tuotianyu/数据/paper_based/min_gt_0.015_0608_0620_0714_0720.tif',
        refImg=
        'S1B_IW_GRDH_1SDV_20200608T101815_20200608T101851_021941_029A3A_7867.zip.tif'
    )

    return (mean_ref - min_ref_flood) / (mean_ref + min_ref_flood)
Esempio n. 3
0
def erase_small_area(img: str, threshold: int, output: str) -> None:
    """
    消除NDFI提取洪涝区域中像素个数较小的像素族
    :param img: 洪涝提取结果
    :param threshold: 个数小于阈值的像素族将会被清楚
    :param output: 输出路径
    :return:
    """

    data_set = gdal.Open(img)
    im = data_set.GetRasterBand(1).ReadAsArray()
    print('height:', len(im), 'width:', len(im[0]))
    ret, thresh = cv2.threshold(im, 0, 255, cv2.THRESH_BINARY)

    cv2.namedWindow('im', cv2.WINDOW_NORMAL)
    cv2.imshow('im', thresh)
    cv2.waitKey(0)

    # 寻找二值图像中的轮廓
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)

    n = len(contours)
    print("'Contours' number:", n)

    for i in range(0, n):
        if cv2.contourArea(contours[i]) < threshold:
            # 初始化,记录消除区域的范围
            r_min, r_max, c_min, c_max = sys.maxsize, -1, sys.maxsize, -1
            for j in range(0, len(contours[i])):
                if contours[i][j][0][1] < r_min:
                    r_min = contours[i][j][0][1]
                if contours[i][j][0][1] > r_max:
                    r_max = contours[i][j][0][1]
                if contours[i][j][0][0] < c_min:
                    c_min = contours[i][j][0][0]
                if contours[i][j][0][0] > c_max:
                    c_max = contours[i][j][0][0]
            for j in range(r_min, r_max + 1):
                for k in range(c_min, c_max + 1):
                    im[j][k] = 0

    array2Raster(im, output, refImg=img)
    # 绘制轮廓
    cv2.drawContours(im, contours, -1, (255, 255, 255), 2)

    cv2.namedWindow('contours', cv2.WINDOW_NORMAL)
    cv2.imshow('contours', im)
    cv2.waitKey(0)
Esempio n. 4
0
def ndfi_post_process3(im: str, water: str, output: str) -> None:
    """
    使用阈值分割提取的水体对NDFI提取的洪涝结果进行掩膜
    :param im:
    :param water:
    :param output
    :return:
    """

    ndfi, im_width, im_height, im_bands = readImage(im, 1)

    water, im_width2, im_height2, im_bands2 = readImage(water, 1)

    temp = np.zeros((im_height, im_width), np.uint8)
    for i in range(0, im_height):
        for j in range(0, im_width):
            if ndfi[i][j] == 1 and water[i][j] == 1:
                temp[i][j] = 1

    array2Raster(temp, output, refImg=im)
def threshold_segmentation(sar_im: str, threshold: float, output: str) -> None:
    """
    阈值法提取SAR图像水体
    :param sar_im:
    :param threshold:
    :param output
    :return:
    """
    if isinstance(sar_im, str):
        im, im_width, im_height, im_bands = readImage(sar_im, 2)

        temp = np.zeros((im_height, im_width), np.uint8)

        for i in range(0, im_height):
            for j in range(0, im_width):
                if im[i][j] < threshold:
                    temp[i][j] = 0
                else:
                    temp[i][j] = 1

        array2Raster(temp, output, refImg=sar_im)
Esempio n. 6
0
def ndfi_post_process2(im: str, output: str, it=1) -> None:
    """
    使用形态学处理
    :param im:
    :param it: 迭代的次数
    :param output:
    :return:
    """
    ndfi, im_width, im_height, im_bands = readImage(im, 1)

    # 使用3×3的滤波器
    kernel = np.ones((3, 3), np.uint8)
    # 迭代次数
    count = it
    # 先膨胀
    dilation = cv2.dilate(ndfi, kernel)
    # 闭运算
    closing_im = cv2.morphologyEx(dilation,
                                  cv2.MORPH_CLOSE,
                                  kernel,
                                  iterations=count)

    array2Raster(closing_im, output, refImg=im)
Esempio n. 7
0
    根据阈值将图像转换为二值影像
    :param image:
    :param threshold:
    :return:
    """
    if isinstance(image, str):
        im, im_width, im_height, im_bands = readImage(image, 1)

        temp = np.zeros((im_height, im_width), np.uint8)

        for i in range(0, im_height):
            for j in range(0, im_width):
                if im[i][j] >= threshold:
                    temp[i][j] = 1
                else:
                    temp[i][j] = 0

        return temp
    else:
        print('please provide the path of image!')
        return []


if __name__ == '__main__':
    import os
    os.chdir(r'F:\毕设\数据\第三次试验')
    binary_im = raster2binary('ndfi_0608_0620_0702_0714_0720.tif', 0.7)
    array2Raster(binary_im,
                 'binary_0.7_ndfi_0608_0620_0702_0714_0720.tif',
                 refImg='ndfi_0608_0620_0702_0714_0720.tif')
Esempio n. 8
0
if __name__ == '__main__':
    import os
    os.chdir(
        r'/home/Tuotianyu/S1_ARD/paper_based_method/home/Tuotianyu/未预处理S1数据')
    pre_image = [
        'S1B_IW_GRDH_1SDV_20200608T101815_20200608T101851_021941_029A3A_7867.zip.tif',
        'S1B_IW_GRDH_1SDV_20200620T101816_20200620T101851_022116_029F8B_298B.zip.tif'
    ]
    post_image = [
        'S1B_IW_GRDH_1SDV_20200714T101817_20200714T101852_022466_02AA36_A270.zip.tif',
        'S1B_IW_GRDH_1SDV_20200726T101818_20200726T101853_022641_02AF8A_BF3A.zip.tif'
    ]
    ndfi = ndfi(pre_image, post_image)
    array2Raster(
        ndfi,
        '/home/Tuotianyu/数据/paper_based/ndfi_0608_0620_0714_0726_VV.tif',
        refImg=
        'S1B_IW_GRDH_1SDV_20200608T101815_20200608T101851_021941_029A3A_7867.zip.tif'
    )
    # 以0.7位阈值将NDFI转换为二值图像
    binary_im = raster2binary(
        '/home/Tuotianyu/数据/paper_based/ndfi_0608_0620_0714_0726_VV.tif', 0.7)
    array2Raster(
        binary_im,
        '/home/Tuotianyu/数据/paper_based/binary_ndfi_0608_0620_0714_0726_VV.tif',
        refImg='/home/Tuotianyu/数据/paper_based/ndfi_0608_0620_0714_0726_VV.tif'
    )

    ndfi_post_process2(
        '/home/Tuotianyu/数据/paper_based/binary_ndfi_0608_0620_0714_0726_VV.tif',
        '/home/Tuotianyu/数据/paper_based/2_post_process_ndfi_0608_0620_0714_0726_VV.tif'
    )
Esempio n. 9
0
        im_height = data_set.RasterYSize
        im_bands = data_set.RasterCount
        print('im_width:', im_width, 'im_height:', im_height, 'bands_num:',
              im_bands)

        # 绿色波段
        g = data_set.GetRasterBand(3).ReadAsArray()
        # 近红外波段
        nir = data_set.GetRasterBand(8).ReadAsArray()

        return (g - nir) / (g + nir)
    else:
        print('please provide the path of image')

        return []


if __name__ == '__main__':
    import os
    os.chdir(r'/home/Tuotianyu/数据/水体提取/Sentinel-2/第一次试验')
    image = 'S2A_MSIL2A_20200715T024551_N0214_R132_T50RMT_20200715T084024_s2resampled.tif'
    ndwi = ndwi(image)
    array2Raster(
        ndwi,
        's2_water_0715',
        refImg=
        'S2A_MSIL2A_20200715T024551_N0214_R132_T50RMT_20200715T084024_s2resampled.tif'
    )
    # 根据阈值将其转换为二值图像
    raster2binary('s2_water_0715', 0.2)
        # 使用VV的极化方式
        r_im, w1, h1, b1 = readImage(reference_im, 2)
    else:
        print('Please provide the path of image!')
        return []

    if isinstance(flooded_img, str):
        f_im, w2, h2, b2 = readImage(flooded_img, 2)
    else:
        print('Please provide the path of image!')
        return []

    d_img = abs(f_im) - abs(r_im)

    return d_img


if __name__ == '__main__':
    import os
    os.chdir(r'/home/Tuotianyu/NDFI_NDFVI/第八次试验预处理数据/home/Tuotianyu/未预处理S1数据')
    difference_im = create_d(
        'S1B_IW_GRDH_1SDV_20200620T101816_20200620T101851_022116_029F8B_298B.zip.tif',
        'S1B_IW_GRDH_1SDV_20200714T101817_20200714T101852_022466_02AA36_A270.zip.tif'
    )
    array2Raster(
        difference_im,
        '0620_0714_difference_image.tif',
        refImg=
        'S1B_IW_GRDH_1SDV_20200714T101817_20200714T101852_022466_02AA36_A270.zip.tif'
    )
Esempio n. 11
0
def opening(im: Union[list, np.ndarray], it: int) -> Union[list, np.ndarray]:
    """
    开运算,先腐蚀后膨胀
    :param im:读入的影像
    :return:
    """
    binary_im, im_width, im_height, im_bands = readImage(im, 1)

    # 使用3×3的滤波器
    kernel = np.ones((3, 3), np.uint8)
    # 迭代次数
    count = it
    # 开运算
    opening_im = cv2.morphologyEx(binary_im,
                                  cv2.MORPH_OPEN,
                                  kernel,
                                  iterations=count)

    return opening_im


if __name__ == '__main__':
    import os
    os.chdir(r'F:\毕设\数据\Sentinel-2\第一次试验')

    closing_im = closing('binary_0.2_s2_water_0715.tif')
    array2Raster(closing_im,
                 'closing_binary_0.2_s2_water_0715.tif',
                 refImg='binary_0.2_s2_water_0715.tif')