def check_input_image_and_label(image_path, label_path):
    """
    check the input image and label, they should have same width, height, and projection
    :param image_path: the path of image
    :param label_path: the path of label
    :return: (width, height) of image if successful, Otherwise (None, None).
    """

    img_obj = RSImageclass()
    if img_obj.open(image_path) is False:
        assert False
    label_obj = RSImageclass()
    if label_obj.open(label_path) is False:
        assert False
    # check width and height
    width = img_obj.GetWidth()
    height = img_obj.GetHeight()
    if width != label_obj.GetWidth() or height != label_obj.GetHeight():
        basic.outputlogMessage("Error, not the same width and height of image (%s) and label (%s)"%(image_path,label_path))
        assert False

    # check resolution
    if img_obj.GetXresolution() != label_obj.GetXresolution() or img_obj.GetYresolution() != label_obj.GetYresolution():
        basic.outputlogMessage(
            "Error, not the same resolution of image (%s) and label (%s)" % (image_path, label_path))
        assert False

    # check projection
    if img_obj.GetProjection() != label_obj.GetProjection():
        basic.outputlogMessage(
            "warning, not the same projection of image (%s) and label (%s)" % (image_path, label_path))
    #     assert False

    return (width, height)
def main(options, args):

    msi_files = args  # all images in this file should have the same width and height
    # output = options.output
    name_index = options.name_index

    if len(msi_files) < 1:
        raise IOError('NO input images')

    # test_TheilSen()

    annual_based = options.annual_based

    # sort the file to make
    msi_files = sorted(msi_files)

    # for file in msi_files:
    #     print(file)

    # test
    aoi = (300, 250, 600, 300)  # (xoff, yoff ,xsize, ysize) in pixels
    # aoi = (300, 250, 10, 20)
    # band_index = [1,2,3]    # for test

    valid_month = [7, 8]
    confidence_inter = 0.95

    # split the image and label
    img_obj = RSImageclass()
    if img_obj.open(msi_files[0]) is False:
        raise IOError('Open %s failed' % msi_files[0])
    width = img_obj.GetWidth()
    height = img_obj.GetHeight()
    patch_w = 200
    patch_h = 200
    patch_boundary = split_image.sliding_window(
        width, height, patch_w, patch_h, 0,
        0)  # boundary of patch (xoff,yoff ,xsize, ysize)

    # use multiple thread
    num_cores = multiprocessing.cpu_count()
    print('number of thread %d' % num_cores)
    # theadPool = mp.Pool(num_cores)  # multi threads, can not utilize all the CPUs? not sure hlc 2018-4-19
    theadPool = Pool(num_cores)  # multi processes

    # for idx, aoi in enumerate(patch_boundary):
    #     print(idx, aoi)

    tmp_dir = '%s_trend_patches' % name_index
    parameters_list = [(msi_files, aoi, name_index,
                        valid_month, confidence_inter,
                        os.path.join(tmp_dir, '%d.tif' % idx), annual_based)
                       for idx, aoi in enumerate(patch_boundary)]
    results = theadPool.map(cal_trend_for_one_index_parallel, parameters_list)
Beispiel #3
0
def cal_the_mean_of_bands(input_path):

    # if multi files in one line, then only consider the first file
    image_path = get_first_path_in_line(input_path)
    if image_path is False:
        return False

    img_obj = RSImageclass()
    if img_obj.open(image_path):
        width = img_obj.GetWidth()
        height = img_obj.GetHeight()
        mean_of_bands = RSImage.get_image_mean_value(image_path)

        if mean_of_bands is False:
            return (False, False, False)
        return (width, height, mean_of_bands)
    else:
        basic.outputlogMessage('error, Open image %s failed' % image_path)
        return (False, False, False)
def main(options, args):

    mod_snow_file = args[0]
    myd_snow_file = args[1]

    # x = 10
    # y = 100

    # at least four decimal
    # x = 92.9123
    # y = 34.8485
    # x = 92.76071
    # y = 35.08546

    # x = 92.80871
    # y = 34.79564
    # xy_srs = 'lon_lat_wgs84'  # pixel
    # snow_series = one_point_snowcover_series(x,y,xy_srs,mod_snow_file,myd_snow_file)
    # save_year_monthly_days([snow_series], 1, 1)

    # calculate for the whole image
    rs_obj = RSImageclass()
    if rs_obj.open(mod_snow_file) is False:
        return False
    img_width = rs_obj.GetWidth()  #3 #
    img_height = rs_obj.GetHeight()  #3 #

    xy_srs = 'pixel'  # pixel lon_lat_wgs84
    snow_series_wholeArea = []
    # print(img_width,img_height)
    for img_row in range(img_height):
        for img_col in range(img_width):
            snow_series = one_point_snowcover_series(img_col, img_row, xy_srs,
                                                     mod_snow_file,
                                                     myd_snow_file)
            snow_series_wholeArea.append(snow_series)

    save_yearly_days(mod_snow_file, snow_series_wholeArea, img_width,
                     img_height)

    save_year_monthly_days(mod_snow_file, snow_series_wholeArea, img_width,
                           img_height)
Beispiel #5
0
def save_oneband_list(save_path, ndvi_list, band_name_list, org_img):
    '''
    save ndvi of multiple images to a file
    :param save_path:
    :param ndvi_list:
    :param band_name_list:
    :param org_img:
    :return:
    '''
    if len(ndvi_list) < 1 or len(band_name_list) < 1:
        raise ValueError('the input of ndvi or name list is empty')

    if len(ndvi_list) != len(band_name_list):
        raise ValueError(
            'the length of ndvi list and band name list is different')

    rsImg_org = RSImageclass()
    rsImg_org.open(org_img)
    org_width = rsImg_org.GetWidth()
    org_height = rsImg_org.GetHeight()
    datetype = rsImg_org.GetGDALDataType()
    prj = rsImg_org.GetProjection()
    geo_tran = rsImg_org.GetGeoTransform()

    save_bandcount = len(ndvi_list)

    rsImg_obj = RSImageclass()
    if rsImg_obj.New(save_path, org_width, org_height, save_bandcount,
                     6):  # 6 for float 32
        for idx in range(save_bandcount):
            height, width = ndvi_list[idx].shape

            ndvi_str = ndvi_list[idx].tobytes(
            )  # struct.pack('%f' % width * height, *templist)
            if rsImg_obj.WritebandData(idx + 1, 0, 0, width, height, ndvi_str,
                                       6):
                rsImg_obj.set_band_name(idx + 1, band_name_list[idx])

        # set projection and transform
        rsImg_obj.SetProjection(prj)
        rsImg_obj.SetGeoTransform(geo_tran)
Beispiel #6
0
def make_dataset(root,list_txt,patch_w,patch_h,adj_overlay_x,adj_overlay_y,train=True):
    """
    get the patches information of the remote sensing images. 
    :param root: data root
    :param list_txt: a list file contain images (one row contain one train image and one label 
    image with space in the center if the input is for training; one row contain one image if it is for inference)
    :param patch_w: the width of the expected patch
    :param patch_h: the height of the expected patch
    :param adj_overlay: the extended distance (in pixel) to adjacent patch, make each patch has overlay with adjacent patch
    :param train:  indicate training or inference
    :return: dataset (list)
    """
    dataset = []

    crop_height=patch_h+2*adj_overlay_y
    crop_width=patch_w+2*adj_overlay_x
    if os.path.isfile(list_txt) is False:
        basic.outputlogMessage("error, file %s not exist"%list_txt)
        assert False

    with open(list_txt) as file_obj:
        files_list = file_obj.readlines()
    if len(files_list) < 1:
        basic.outputlogMessage("error, no file name in the %s" % list_txt)
        assert False

    if train:
        abandon_number = 0
        count_for_pure_image = 0
        for line in files_list:
            names_list = line.split()
            if len(names_list) < 1: # empty line
                continue
            image_name = names_list[0]
            label_name = names_list[1].strip()

            # img_path = os.path.join(root,image_name)
            # label_path = os.path.join(root,label_name)

            img_path=image_name
            label_path=label_name
            #
            (width,height) = check_input_image_and_label(img_path,label_path)

            # split the image and label
            patch_boundary = split_image.sliding_window(width, height, patch_w, patch_h, adj_overlay_x,adj_overlay_y)

            for patch in patch_boundary:
               # remove the patch small than model input size
               #  if patch[2] < crop_width or patch[3] < crop_height:# xSize < 480 or ySize < 480
               #
               #    # print ('not in edge mode')
               #      continue
                img_patch = patchclass(img_path,patch)
                label_patch = patchclass(label_path,patch)

                gt_test = read_patch2(label_patch)
                max=np.amax(gt_test)
                min=np.amin(gt_test)

                if max==min:
                    count_for_pure_image = count_for_pure_image+1
                    #print ("this is %d image"%min)
                    a=random.randint(0,4)
                    if a == 1:
                        dataset.append([img_patch, label_patch])
                    else:
                        abandon_number=abandon_number+1
                        continue
                else:
                    dataset.append([img_patch, label_patch])
        print("%d images are abandoned"%abandon_number)
        print("%d images are 1 or 0 images"%count_for_pure_image)
        print("%d images in total"%len(dataset))

    else:
        for line in files_list:
            names_list = line.split()
            image_name = names_list[0]
            label_name = names_list[1].strip()


            img_path = image_name
            label_path = label_name
            (width, height) = check_input_image_and_label(img_path, label_path)
            #
            img_obj = RSImageclass()
            if img_obj.open(img_path) is False:
                assert False
            width = img_obj.GetWidth()
            height = img_obj.GetHeight()

            # split the image and label
            patch_boundary = split_image.sliding_window_test(width, height, patch_w, patch_h, adj_overlay_x,adj_overlay_y)

            for patch in patch_boundary:
                # need to handle the patch with smaller size
                # if patch[2] < crop_width or patch[3] < crop_height:   # xSize < 480 or ySize < 480
                #     continue
                img_patch = patchclass(img_path, patch)
                label_patch = patchclass(label_path, patch)

                dataset.append([img_patch, label_patch])

    return dataset
    # band_num  = 1   # only consider the first band
    # bucket_count, hist_min, hist_max, hist_buckets = RSImage.get_image_histogram_oneband(img_file, band_num)

    img_name = os.path.basename(img_file)
    if img_name in calculated_files:
        basic.outputlogMessage('image: %s already be calcuated, skip' %
                               img_name)
        continue

    try:
        valid_count = RSImage.get_valid_pixel_count(
            img_file)  # count the pixels without nodata pixel
    except Exception as e:  # can get all the exception, and the program will not exit
        print(str(e))
        basic.outputlogMessage("get grey histrogram of %s failed" % img_file)
        continue

    # get image width and height
    img_obj = RSImageclass()
    if img_obj.open(img_file):
        width = img_obj.GetWidth()
        height = img_obj.GetHeight()
        nodata_per = 100.0 * (width * height - valid_count) / (width * height)
        basic.outputlogMessage('Nodata percentage %.2lf' % nodata_per)

        f_obj.writelines("%d: %s Nodata percentage: %.2lf \n" %
                         (idx + 1, img_name, nodata_per))
        f_obj.flush()

f_obj.close()
Beispiel #8
0
def make_dataset(root, list_txt, patch_w, patch_h, adj_overlay, train=True):
    """
    get the patches information of the remote sensing images. 
    :param root: data root
    :param list_txt: a list file contain images (one row contain one train image and one label 
    image with space in the center if the input is for training; one row contain one image if it is for inference)
    :param patch_w: the width of the expected patch
    :param patch_h: the height of the expected patch
    :param adj_overlay: the extended distance (in pixel) to adjacent patch, make each patch has overlay with adjacent patch
    :param train:  indicate training or inference
    :return: dataset (list)
    """
    dataset = []

    if os.path.isfile(list_txt) is False:
        basic.outputlogMessage("error, file %s not exist" % list_txt)
        assert False

    with open(list_txt) as file_obj:
        files_list = file_obj.readlines()
    if len(files_list) < 1:
        basic.outputlogMessage("error, no file name in the %s" % list_txt)
        assert False

    if train:
        for line in files_list:
            names_list = line.split()
            if len(names_list) < 1:  # empty line
                continue
            image_name = names_list[0]
            label_name = names_list[1].strip()

            img_path = os.path.join(root, image_name)
            label_path = os.path.join(root, label_name)
            #
            (width, height) = check_input_image_and_label(img_path, label_path)

            # split the image and label
            patch_boundary = split_image.sliding_window(
                width, height, patch_w, patch_h, adj_overlay)

            for patch in patch_boundary:
                # remove the patch small than model input size
                if patch[2] < crop_width or patch[
                        3] < crop_height:  # xSize < 480 or ySize < 480
                    continue
                img_patch = patchclass(img_path, patch)
                label_patch = patchclass(label_path, patch)
                dataset.append([img_patch, label_patch])

    else:
        for line in files_list:
            names_list = line.split()
            image_name = names_list[0].strip()

            img_path = os.path.join(root, image_name)
            #
            img_obj = RSImageclass()
            if img_obj.open(img_path) is False:
                assert False
            width = img_obj.GetWidth()
            height = img_obj.GetHeight()

            # split the image and label
            patch_boundary = split_image.sliding_window(
                width, height, patch_w, patch_h, adj_overlay)

            for patch in patch_boundary:
                # need to handle the patch with smaller size
                # if patch[2] < crop_width or patch[3] < crop_height:   # xSize < 480 or ySize < 480
                #     continue
                img_patch = patchclass(img_path, patch)

                dataset.append(img_patch)

    return dataset