示例#1
0
    def do(self, input_obj: ImageEntity, random_state_obj: RandomState) -> ImageEntity:
        """
        Compresses 3-channel image input image as a specified filetype and stores in memory,
        passes to into wand and applies filter, stores filtered image as specified filetype again in memory,
        which is then decompressed back into 3-channel image
        :param input_obj: entity to be transformed
        :param random_state_obj: object to hold random state and enable reproducibility
        :return:new entity with transform applied
        """
        data = input_obj.get_data()
        original_n_chan = data.shape[2]
        rgb_data, alpha_ch = normalization_to_rgb(data, self.pre_normalize, self.__repr__())
        logger.debug("%s is treating input as %s!" % (self.__repr__(), self.channel_order))
        if self.channel_order == 'RGB':
            rgb_data = cv2.cvtColor(rgb_data, cv2.COLOR_RGB2BGR)

        form = '.bmp'
        success, buffer = cv2.imencode(form, rgb_data)
        # faster than numpy tobytes method
        input_stream = BytesIO(buffer)
        with wand.image.Image(blob=input_stream.getvalue()) as wand_image:
            filtered_wand_image = self.filter(wand_image)
            output_stream = BytesIO()
            filtered_wand_image.save(output_stream)
            rgb_filtered_data = cv2.imdecode(np.frombuffer(output_stream.getbuffer(), np.uint8), 1)

            if self.channel_order == 'RGB':
                rgb_filtered_data = cv2.cvtColor(rgb_filtered_data, cv2.COLOR_BGR2RGB)
            filtered_data = normalization_from_rgb(rgb_filtered_data, alpha_ch, self.post_normalize,
                                                   original_n_chan, self.__repr__())
            return GenericImageEntity(filtered_data, input_obj.get_mask())
示例#2
0
    def do(self, input_obj: ImageEntity,
           random_state_obj: RandomState) -> ImageEntity:
        """
        Perform the actual defined transformation
        :param input_obj: the input Entity to be transformed
        :param random_state_obj: ignored
        :return: the transformed Entity
        """
        img = input_obj.get_data()
        original_n_chan = img.shape[2]
        rgb_img, alpha_ch = normalization_to_rgb(img, self.pre_normalize,
                                                 "AddRainXForm")

        logger.debug(
            "Applying albumentations.RandomRain with slant=%0.02f, drop_length=%0.02f, drop_width=%0.02f,"
            "drop_color=%s, rain_type=%s, pre_normalization=%s, post_normalization=%s"
            % (self.slant, self.drop_length, self.drop_width,
               str(self.drop_color), self.rain_type, self.pre_normalize,
               self.post_normalize), )

        rgb_img_xformed = self.rain_object(random_state=random_state_obj,
                                           image=rgb_img)['image']
        img_xformed = normalization_from_rgb(rgb_img_xformed, alpha_ch,
                                             self.post_normalize,
                                             original_n_chan, "AddRainXForm")

        return GenericImageEntity(img_xformed, input_obj.get_mask())
示例#3
0
    def do(self, input_obj: ImageEntity,
           random_state_obj: RandomState) -> ImageEntity:
        """
        Perform the actual defined transformation
        :param input_obj: the input Entity to be transformed
        :param random_state_obj: ignored
        :return: the transformed Entity
        """
        img = input_obj.get_data()
        original_n_chan = img.shape[2]
        rgb_img, alpha_ch = normalization_to_rgb(img, self.pre_normalize,
                                                 "AddSnowXForm")

        logger.debug(
            "Applying albumentations.RandomSnow with coeff=%0.02f, pre_normalize=%s, post_normalize=%s"
            % (
                self.snow_coeff,
                self.pre_normalize,
                self.post_normalize,
            ))

        rgb_img_xformed = self.snow_object(random_state=random_state_obj,
                                           image=rgb_img)['image']
        img_xformed = normalization_from_rgb(rgb_img_xformed, alpha_ch,
                                             self.post_normalize,
                                             original_n_chan, "AddSnowXForm")
        return GenericImageEntity(img_xformed, input_obj.get_mask())
示例#4
0
    def do(self, input_obj: ImageEntity,
           random_state_obj: RandomState) -> ImageEntity:
        """
        Perform the actual defined transformation
        :param input_obj: the input Entity to be transformed
        :param random_state_obj: ignored
        :return: the transformed Entity
        """
        img = input_obj.get_data()
        original_n_chan = img.shape[2]
        rgb_img, alpha_ch = normalization_to_rgb(img, self.pre_normalize,
                                                 "AddSunFlareXForm")

        # RandomSunFlare requires center and angle of flare to be normalized on [0.0, 1.0],
        if self.sunflare_object is None:
            roi = ()
            if self.flare_center == (-1, -1):
                roi = (0.0, 0.0, 1.0, 0.5)
            else:
                roi_x = self.flare_center[0] / rgb_img.shape[1]
                roi_y = self.flare_center[1] / rgb_img.shape[0]
                roi = (roi_x, roi_y, roi_x, roi_y)
            angle_lower = self.angle / (2 * math.pi)
            angle_upper = self.angle / (2 * math.pi)
            if self.angle == -1:
                angle_lower, angle_upper = 0.0, 1.0
            self.sunflare_object = albu.RandomSunFlare(
                roi,
                angle_lower,
                angle_upper,
                self.no_of_flare_circles - 1,
                self.no_of_flare_circles + 1,
                self.src_radius,
                self.src_color,
                always_apply=True)
        logger.debug(
            "Applying albumentations.RandomSunFlare with center=%s, angle=%0.02f, # flare-circles=%d,"
            "flare-radius=%d, color=%s, pre_normalize=%s, post_normalize=%s" %
            (str(self.flare_center), self.angle, self.no_of_flare_circles,
             self.src_radius, str(
                 self.src_color), self.pre_normalize, self.post_normalize))

        rgb_img_xformed = self.sunflare_object(random_state=random_state_obj,
                                               image=rgb_img)['image']
        img_xformed = normalization_from_rgb(rgb_img_xformed, alpha_ch,
                                             self.post_normalize,
                                             original_n_chan,
                                             "AddSunFlareXForm")
        return GenericImageEntity(img_xformed, input_obj.get_mask())
示例#5
0
    def do(self, input_obj: ImageEntity,
           random_state_obj: RandomState) -> ImageEntity:
        """
        Perform the actual defined transformation
        :param input_obj: the input Entity to be transformed
        :param random_state_obj: ignored
        :return: the transformed Entity
        """
        img = input_obj.get_data()
        original_n_chan = img.shape[2]
        rgb_img, alpha_ch = normalization_to_rgb(img, self.pre_normalize,
                                                 "AddShadowXForm")

        if self.shadow_object is None:
            no_of_shadows_lower, no_of_shadows_upper = self.no_of_shadows, self.no_of_shadows
            # RandomShadow requires roi to be normalized on [0.0, 1.0]
            roi = ()
            if self.rectangular_roi == (-1, -1, -1, -1):
                roi = (0.0, 0.5, 1.0, 1.0)
            else:
                roi_x1 = self.rectangular_roi[0] / rgb_img.shape[1]
                roi_y1 = self.rectangular_roi[1] / rgb_img.shape[0]
                roi_x2 = self.rectangular_roi[2] / rgb_img.shape[1]
                roi_y2 = self.rectangular_roi[3] / rgb_img.shape[0]
                roi = (roi_x1, roi_y1, roi_x2, roi_y2)
            self.shadow_object = albu.RandomShadow(roi,
                                                   no_of_shadows_lower,
                                                   no_of_shadows_upper,
                                                   self.shadow_dimension,
                                                   always_apply=True)

        logger.debug(
            "Applying albumentations.RandomShadow with shadows=%d, ROI=%s, dimension=%d, pre_normalization=%s,"
            "post_normalization=%s" % (
                self.no_of_shadows,
                str(self.rectangular_roi),
                self.shadow_dimension,
                self.pre_normalize,
                self.post_normalize,
            ))

        rgb_img_xformed = self.shadow_object(random_state=random_state_obj,
                                             image=rgb_img)['image']
        img_xformed = normalization_from_rgb(rgb_img_xformed, alpha_ch,
                                             self.post_normalize,
                                             original_n_chan, "AddShadowXForm")
        return GenericImageEntity(img_xformed, input_obj.get_mask())