def _BuildDataSet(self, sampling_factor,
                      normalize):

        estimation_set = self.img_est.sample(region=self.region,
                                             factor=sampling_factor,
                                             seed=self.seed)

        prediction_set = self.img_pred.sample(region=self.region,
                                              factor=sampling_factor,
                                              seed=self.seed)
        # Add weights
        estimation_set_size = ee.Number(estimation_set.size())
        prediction_set_size = ee.Number(prediction_set.size())
        peso_estimacion = ee.Number(self.beta).divide(estimation_set_size)
        peso_prediccion = ee.Number(1-self.beta).divide(prediction_set_size)

        bands_modeling_estimation_input_weight = list(self.bands_modeling_estimation_input)
        bmp = list(self.bands_modeling_prediction)
        bmp.append("weight")
        bme = list(self.bands_modeling_estimation)
        bme.append("weight")

        estimation_set = estimation_set.map(
            lambda ft: ee.Feature(ft).set("weight", peso_estimacion))
        prediction_set = prediction_set.map(
            lambda ft: ee.Feature(ft).set("weight", peso_prediccion))
        bands_modeling_estimation_input_weight.append("weight")

        self.estimation_set = estimation_set
        self.prediction_set = prediction_set
        if (self.beta > 0) and (self.cc_image < CC_IMAGE_TOP):
            self.datos = estimation_set.merge(prediction_set.select(bmp,
                                                                    bme))
        else:
            logger.info("Using only prediction")
            self.datos = prediction_set.select(bmp,
                                               bme)
        if normalize:
            self.datos, self.inputs_mean, self.inputs_std = normalization.ComputeNormalizationFeatureCollection(
                self.datos, self.bands_modeling_estimation_input, only_center_data=False, weight="weight")
            self.datos, self.outputs_mean, self.outputs_std = normalization.ComputeNormalizationFeatureCollection(
                self.datos, self.bands_modeling_estimation_output, only_center_data=True, weight="weight")

            self.inputs_mean = self.inputs_mean.toArray( self.bands_modeling_estimation_input)
            self.inputs_std = self.inputs_std.toArray(self.bands_modeling_estimation_input)
            self.outputs_mean = self.outputs_mean.toArray(self.bands_modeling_estimation_output)


            #if "B10" in self.bands_modeling_estimation_output:
            #    self.datos.select("B10").divide(100)
            #if "B11" in self.bands_modeling_estimation_output:
            #    output_dataset["B11"] /= 100

        self.inputs = self.datos.select(bands_modeling_estimation_input_weight)
        self.outputs = self.datos.select(self.bands_modeling_estimation_output)

        return
Example #2
0
def ClusterClouds(image,
                  background_prediction,
                  threshold_dif_cloud=.045,
                  do_clustering=True,
                  numPixels=1000,
                  threshold_reflectance=.175,
                  bands_thresholds=["B2", "B3", "B4"],
                  growing_ratio=2,
                  n_clusters=10,
                  region_of_interest=None):
    """
    Function that compute the cloud score given the differences between the real and predicted image.

    :param img_differences: image_real - image_pred
    :param threshold_dif_cloud: Threshold over the cloud score to be considered clouds
    :param threshold_dif_shadow:Threshold over the cloud score to be considered shadows
    :param n_clusters: number of clusters
    :param numPixels:  to be considered by the clustering algorithm
    :param region_of_interest:  region of interest within the image
    :return: ee.Image with 0 for clear pixels, 1 for shadow pixels and 2 for cloudy pixels
    """

    img_differences = image.subtract(background_prediction)

    training = img_differences.sample(region=region_of_interest,
                                      scale=30,
                                      numPixels=numPixels)

    training, media, std = normalization.ComputeNormalizationFeatureCollection(
        training, BANDS_MODEL)

    if do_clustering:
        clusterer = ee.Clusterer.wekaKMeans(n_clusters).train(training)
        img_differences_normalized = normalization.ApplyNormalizationImage(
            img_differences, BANDS_MODEL, media, std)
        result = img_differences_normalized.cluster(clusterer)

        multitemporal_score, reflectance_score = SelectClusters(
            image, background_prediction, result, n_clusters, bands_thresholds,
            region_of_interest)

    else:
        arrayImageDiff = img_differences.select(bands_thresholds).toArray()
        arrayImage = image.select(bands_thresholds).toArray()

        arrayImageDiffmean = arrayImageDiff.arrayReduce(ee.Reducer.mean(), axes=[0])\
                                           .gte(0).arrayGet([0])
        arrayImageDiffnorm = arrayImageDiff.multiply(arrayImageDiff)\
                                           .arrayReduce(ee.Reducer.mean(), axes=[0])\
                                           .sqrt().arrayGet([0])

        arrayImagenorm = arrayImage.multiply(arrayImage) \
                                   .arrayReduce(ee.Reducer.mean(), axes=[0])\
                                   .sqrt().arrayGet([0]) \

        reflectance_score = arrayImagenorm

        multitemporal_score = arrayImageDiffnorm.multiply(arrayImageDiffmean)

    # Apply thresholds
    if threshold_reflectance <= 0:
        cloud_score_threshold = multitemporal_score.gt(threshold_dif_cloud)
    else:
        cloud_score_threshold = multitemporal_score.gt(threshold_dif_cloud)\
                                                   .multiply(reflectance_score.gt(threshold_reflectance))

    # apply opening
    kernel = ee.Kernel.circle(radius=growing_ratio)
    cloud_score_threshold = cloud_score_threshold.focal_min(kernel=kernel).\
        focal_max(kernel= kernel)

    return cloud_score_threshold