コード例 #1
0
class HorizontalBlurrer(ImageModifierABC):
    max_kernel_size: int = 11

    def modify(self, image: Image, intensity: float) -> Image:
        kernel = self._make_zero_kernel(intensity)
        self._fill_middle_row_with_ones(kernel)
        self._normalize_kernel(kernel)
        return self._apply_kernel(image, kernel)

    def _make_zero_kernel(self, intensity: float) -> np.ndarray:
        kernel_size = int(self.max_kernel_size * intensity) + 1
        return np.zeros((kernel_size, kernel_size))

    @staticmethod
    def _fill_middle_row_with_ones(kernel: np.ndarray):
        kernel[len(kernel) // 2, :] = 1

    @staticmethod
    def _normalize_kernel(kernel: np.ndarray):
        kernel /= len(kernel)

    @staticmethod
    def _apply_kernel(image: Image, kernel: np.ndarray) -> Image:
        return cv2.filter2D(image, -1, kernel)


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(HorizontalBlurrer())
コード例 #2
0
ファイル: gaussian_blur.py プロジェクト: PolySTAR-mtl/cv
from dataclasses import dataclass

import cv2

from polystar.models.image import Image
from research.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC


@dataclass
class GaussianBlurrer(ImageModifierABC):
    max_factor: float = 0.015

    def modify(self, image: Image, intensity: float) -> Image:
        blur_factor = intensity * self.max_factor
        width, height, *_ = image.shape

        x, y = _to_odd_number(width * blur_factor), _to_odd_number(height * blur_factor)
        image = cv2.GaussianBlur(image, (x, y), cv2.BORDER_DEFAULT)
        return image


def _to_odd_number(number):
    return int(number // 2 * 2) - 1


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(GaussianBlurrer())
コード例 #3
0
from dataclasses import dataclass

import numpy as np

from polystar.models.image import Image
from research.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC


@dataclass
class BrightnessModifier(ImageModifierABC):
    max_offset: float = 10.0

    def modify(self, image: Image, intensity: float) -> Image:
        offset = self.max_offset * intensity
        perturbed_image = np.clip((image.astype(np.uint16) + offset), 0, 255).astype(np.uint8)
        return perturbed_image


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(BrightnessModifier())
コード例 #4
0
ファイル: contrast.py プロジェクト: PolySTAR-mtl/cv
from dataclasses import dataclass

import numpy as np

from polystar.models.image import Image
from research.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC


@dataclass
class ContrastModifier(ImageModifierABC):
    min_coef: float = 0.8
    max_coef: float = 1.5

    def modify(self, image: Image, intensity: float) -> Image:
        coef = self.min_coef + (self.max_coef - self.min_coef) * intensity
        perturbed_image = np.clip((image.astype(np.uint16) * coef), 0,
                                  255).astype(np.uint8)
        return perturbed_image


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(ContrastModifier())
コード例 #5
0
from dataclasses import dataclass

import cv2
import numpy as np

from polystar.models.image import Image
from research.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC


@dataclass
class SaturationModifier(ImageModifierABC):
    max_saturation: float = 0.6

    def modify(self, image: Image, intensity: float) -> Image:
        saturation_factor = 1 + self.max_saturation * intensity
        image_hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
        (h, s, v) = cv2.split(image_hsv)
        new_s = np.clip((s.astype(np.uint16) * saturation_factor), 0, 255).astype(np.uint8)
        new_image_hsv = cv2.merge([h, new_s, v])
        image_rgb = cv2.cvtColor(new_image_hsv, cv2.COLOR_HSV2RGB).astype(np.uint8)
        return image_rgb


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(SaturationModifier())
コード例 #6
0
ファイル: gaussian_noise.py プロジェクト: PolySTAR-mtl/cv
from dataclasses import dataclass

import numpy as np

from polystar.models.image import Image
from research.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC


@dataclass
class GaussianNoiser(ImageModifierABC):
    max_variance: float = 300.0

    def modify(self, image: Image, intensity: float) -> Image:
        variance = self.max_variance * intensity
        sigma = variance**0.5
        row, column, ch = image.shape
        gaussian = np.random.normal(0, sigma, (row, column, ch))
        perturbed_image = np.clip((image.astype(np.uint16) + gaussian), 0,
                                  255).astype(np.uint8)
        return perturbed_image


if __name__ == "__main__":
    from research.dataset.perturbations.utils import simple_modifier_demo

    simple_modifier_demo(GaussianNoiser())