transforms.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633
  1. """Transform classes for applying various blur operations to images.
  2. This module contains transform classes that implement different blur effects including
  3. standard blur, motion blur, median blur, Gaussian blur, glass blur, advanced blur, defocus,
  4. and zoom blur. These transforms are designed to work within the albumentations pipeline
  5. and support parameters for controlling the intensity and properties of the blur effects.
  6. """
  7. from __future__ import annotations
  8. from typing import Annotated, Any, Literal, cast
  9. import numpy as np
  10. from pydantic import (
  11. AfterValidator,
  12. Field,
  13. ValidationInfo,
  14. field_validator,
  15. model_validator,
  16. )
  17. from typing_extensions import Self
  18. from albumentations.augmentations.pixel import functional as fpixel
  19. from albumentations.core.pydantic import (
  20. NonNegativeFloatRangeType,
  21. OnePlusFloatRangeType,
  22. OnePlusIntRangeType,
  23. SymmetricRangeType,
  24. check_range_bounds,
  25. convert_to_0plus_range,
  26. nondecreasing,
  27. process_non_negative_range,
  28. )
  29. from albumentations.core.transforms_interface import (
  30. BaseTransformInitSchema,
  31. ImageOnlyTransform,
  32. )
  33. from albumentations.core.utils import to_tuple
  34. from . import functional as fblur
  35. __all__ = [
  36. "AdvancedBlur",
  37. "Blur",
  38. "Defocus",
  39. "GaussianBlur",
  40. "GlassBlur",
  41. "MedianBlur",
  42. "MotionBlur",
  43. "ZoomBlur",
  44. ]
  45. HALF = 0.5
  46. TWO = 2
  47. class BlurInitSchema(BaseTransformInitSchema):
  48. blur_limit: tuple[int, int] | int
  49. @field_validator("blur_limit")
  50. @classmethod
  51. def process_blur(cls, value: tuple[int, int] | int, info: ValidationInfo) -> tuple[int, int]:
  52. return fblur.process_blur_limit(value, info, min_value=3)
  53. class Blur(ImageOnlyTransform):
  54. """Apply uniform box blur to the input image using a randomly sized square kernel.
  55. This transform uses OpenCV's cv2.blur function, which performs a simple box filter blur.
  56. The size of the blur kernel is randomly selected for each application, allowing for
  57. varying degrees of blur intensity.
  58. Args:
  59. blur_limit (tuple[int, int] | int): Controls the range of the blur kernel size.
  60. - If a single int is provided, the kernel size will be randomly chosen
  61. between 3 and that value.
  62. - If a tuple of two ints is provided, it defines the inclusive range
  63. of possible kernel sizes.
  64. The kernel size must be odd and greater than or equal to 3.
  65. Larger kernel sizes produce stronger blur effects.
  66. Default: (3, 7)
  67. p (float): Probability of applying the transform. Default: 0.5
  68. Notes:
  69. - The blur kernel is always square (same width and height).
  70. - Only odd kernel sizes are used to ensure the blur has a clear center pixel.
  71. - Box blur is faster than Gaussian blur but may produce less natural results.
  72. - This blur method averages all pixels under the kernel area, which can
  73. reduce noise but also reduce image detail.
  74. Targets:
  75. image
  76. Image types:
  77. uint8, float32
  78. Examples:
  79. >>> import numpy as np
  80. >>> import albumentations as A
  81. >>> import cv2
  82. >>>
  83. >>> # Create a sample image for demonstration
  84. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  85. >>> # Add some shapes to visualize blur effects
  86. >>> cv2.rectangle(image, (50, 50), (250, 250), (255, 0, 0), -1) # Red square
  87. >>> cv2.circle(image, (150, 150), 60, (0, 255, 0), -1) # Green circle
  88. >>> cv2.line(image, (50, 150), (250, 150), (0, 0, 255), 5) # Blue line
  89. >>>
  90. >>> # Example 1: Basic usage with default parameters
  91. >>> transform = A.Compose([
  92. ... A.Blur(p=1.0) # Always apply with default blur_limit=(3, 7)
  93. ... ])
  94. >>>
  95. >>> result = transform(image=image)
  96. >>> blurred_image = result["image"]
  97. >>> # The image will have a random blur with kernel size between 3 and 7
  98. >>>
  99. >>> # Example 2: Using a fixed blur kernel size
  100. >>> fixed_transform = A.Compose([
  101. ... A.Blur(blur_limit=5, p=1.0) # Always use kernel size 5x5
  102. ... ])
  103. >>>
  104. >>> fixed_result = fixed_transform(image=image)
  105. >>> fixed_blurred_image = fixed_result["image"]
  106. >>> # The image will have a consistent 5x5 kernel blur
  107. >>>
  108. >>> # Example 3: Using a custom range for blur kernel sizes
  109. >>> strong_transform = A.Compose([
  110. ... A.Blur(blur_limit=(7, 13), p=1.0) # Use larger kernel for stronger blur
  111. ... ])
  112. >>>
  113. >>> strong_result = strong_transform(image=image)
  114. >>> strong_blurred = strong_result["image"]
  115. >>> # The image will have a stronger blur with kernel size between 7 and 13
  116. >>>
  117. >>> # Example 4: As part of a pipeline with other transforms
  118. >>> pipeline = A.Compose([
  119. ... A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7),
  120. ... A.Blur(blur_limit=(3, 5), p=0.5), # 50% chance of applying blur
  121. ... A.HorizontalFlip(p=0.5)
  122. ... ])
  123. >>>
  124. >>> pipeline_result = pipeline(image=image)
  125. >>> transformed_image = pipeline_result["image"]
  126. >>> # The image may or may not be blurred depending on the random probability
  127. """
  128. class InitSchema(BlurInitSchema):
  129. pass
  130. def __init__(
  131. self,
  132. blur_limit: tuple[int, int] | int = (3, 7),
  133. p: float = 0.5,
  134. ):
  135. super().__init__(p=p)
  136. self.blur_limit = cast("tuple[int, int]", blur_limit)
  137. def apply(self, img: np.ndarray, kernel: int, **params: Any) -> np.ndarray:
  138. """Apply blur to the input image.
  139. Args:
  140. img (np.ndarray): Image to blur.
  141. kernel (int): Size of the kernel for blur.
  142. **params (Any): Additional parameters.
  143. Returns:
  144. np.ndarray: Blurred image.
  145. """
  146. return fblur.box_blur(img, kernel)
  147. def get_params(self) -> dict[str, Any]:
  148. """Get parameters for the transform.
  149. Returns:
  150. dict[str, Any]: Dictionary with parameters.
  151. """
  152. kernel = fblur.sample_odd_from_range(
  153. self.py_random,
  154. self.blur_limit[0],
  155. self.blur_limit[1],
  156. )
  157. return {"kernel": kernel}
  158. class MotionBlur(Blur):
  159. """Apply motion blur to the input image using a directional kernel.
  160. This transform simulates motion blur effects that occur during image capture,
  161. such as camera shake or object movement. It creates a directional blur using
  162. a line-shaped kernel with controllable angle, direction, and position.
  163. Args:
  164. blur_limit (int | tuple[int, int]): Maximum kernel size for blurring.
  165. Should be in range [3, inf).
  166. - If int: kernel size will be randomly chosen from [3, blur_limit]
  167. - If tuple: kernel size will be randomly chosen from [min, max]
  168. Larger values create stronger blur effects.
  169. Default: (3, 7)
  170. angle_range (tuple[float, float]): Range of possible angles in degrees.
  171. Controls the rotation of the motion blur line:
  172. - 0°: Horizontal motion blur →
  173. - 45°: Diagonal motion blur ↗
  174. - 90°: Vertical motion blur ↑
  175. - 135°: Diagonal motion blur ↖
  176. Default: (0, 360)
  177. direction_range (tuple[float, float]): Range for motion bias.
  178. Controls how the blur extends from the center:
  179. - -1.0: Blur extends only backward (←)
  180. - 0.0: Blur extends equally in both directions (←→)
  181. - 1.0: Blur extends only forward (→)
  182. For example, with angle=0:
  183. - direction=-1.0: ←•
  184. - direction=0.0: ←•→
  185. - direction=1.0: •→
  186. Default: (-1.0, 1.0)
  187. allow_shifted (bool): Allow random kernel position shifts.
  188. - If True: Kernel can be randomly offset from center
  189. - If False: Kernel will always be centered
  190. Default: True
  191. p (float): Probability of applying the transform. Default: 0.5
  192. Examples of angle vs direction:
  193. 1. Horizontal motion (angle=0°):
  194. - direction=0.0: ←•→ (symmetric blur)
  195. - direction=1.0: •→ (forward blur)
  196. - direction=-1.0: ←• (backward blur)
  197. 2. Vertical motion (angle=90°):
  198. - direction=0.0: ↑•↓ (symmetric blur)
  199. - direction=1.0: •↑ (upward blur)
  200. - direction=-1.0: ↓• (downward blur)
  201. 3. Diagonal motion (angle=45°):
  202. - direction=0.0: ↙•↗ (symmetric blur)
  203. - direction=1.0: •↗ (forward diagonal blur)
  204. - direction=-1.0: ↙• (backward diagonal blur)
  205. Note:
  206. - angle controls the orientation of the motion line
  207. - direction controls the distribution of the blur along that line
  208. - Together they can simulate various motion effects:
  209. * Camera shake: Small angle range + direction near 0
  210. * Object motion: Specific angle + direction=1.0
  211. * Complex motion: Random angle + random direction
  212. Examples:
  213. >>> import numpy as np
  214. >>> import albumentations as A
  215. >>> import cv2
  216. >>>
  217. >>> # Create a sample image for demonstration
  218. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  219. >>> # Add some shapes to visualize motion blur effects
  220. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  221. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  222. >>> cv2.putText(image, "Motion Blur", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  223. >>>
  224. >>> # Example 1: Horizontal camera shake (symmetric)
  225. >>> horizontal_shake = A.Compose([
  226. ... A.MotionBlur(
  227. ... blur_limit=(10, 12), # Strong blur
  228. ... angle_range=(-5, 5), # Near-horizontal motion (±5°)
  229. ... direction_range=(0, 0), # Symmetric blur (equally in both directions)
  230. ... p=1.0 # Always apply
  231. ... )
  232. ... ])
  233. >>>
  234. >>> horizontal_result = horizontal_shake(image=image)
  235. >>> horizontal_blur = horizontal_result["image"]
  236. >>> # The image will have a horizontal camera shake effect, blurring equally in both directions
  237. >>>
  238. >>> # Example 2: Object moving right (directional motion)
  239. >>> rightward_motion = A.Compose([
  240. ... A.MotionBlur(
  241. ... blur_limit=(7, 9), # Medium blur
  242. ... angle_range=(0, 0), # Exactly horizontal motion (0°)
  243. ... direction_range=(0.8, 1.0), # Strong forward bias (mostly rightward)
  244. ... p=1.0
  245. ... )
  246. ... ])
  247. >>>
  248. >>> rightward_result = rightward_motion(image=image)
  249. >>> rightward_blur = rightward_result["image"]
  250. >>> # The image will simulate an object moving rightward, with blur mostly to the right
  251. >>>
  252. >>> # Example 3: Object moving diagonally down-right
  253. >>> diagonal_motion = A.Compose([
  254. ... A.MotionBlur(
  255. ... blur_limit=(9, 11), # Stronger blur
  256. ... angle_range=(135, 135), # 135° motion (down-right diagonal)
  257. ... direction_range=(0.7, 0.9), # Forward bias
  258. ... p=1.0
  259. ... )
  260. ... ])
  261. >>>
  262. >>> diagonal_result = diagonal_motion(image=image)
  263. >>> diagonal_blur = diagonal_result["image"]
  264. >>> # The image will simulate diagonal motion down and to the right
  265. >>>
  266. >>> # Example 4: Vertical motion (up-down)
  267. >>> vertical_motion = A.Compose([
  268. ... A.MotionBlur(
  269. ... blur_limit=9, # Fixed kernel size
  270. ... angle_range=(90, 90), # Vertical motion (90°)
  271. ... direction_range=(-0.2, 0.2), # Near-symmetric (slight bias)
  272. ... p=1.0
  273. ... )
  274. ... ])
  275. >>>
  276. >>> vertical_result = vertical_motion(image=image)
  277. >>> vertical_blur = vertical_result["image"]
  278. >>> # The image will simulate vertical motion blur
  279. >>>
  280. >>> # Example 5: Random motion blur (can be in any direction)
  281. >>> random_motion = A.Compose([
  282. ... A.MotionBlur(
  283. ... blur_limit=(5, 12), # Variable strength
  284. ... angle_range=(0, 360), # Any angle
  285. ... direction_range=(-1.0, 1.0), # Any direction bias
  286. ... allow_shifted=True, # Allow kernel to be shifted from center
  287. ... p=1.0
  288. ... )
  289. ... ])
  290. >>>
  291. >>> random_result = random_motion(image=image)
  292. >>> random_blur = random_result["image"]
  293. >>> # The image will have a random motion blur in any direction
  294. >>>
  295. >>> # Example 6: Multiple random parameters with kernel centered (not shifted)
  296. >>> centered_motion = A.Compose([
  297. ... A.MotionBlur(
  298. ... blur_limit=(5, 9),
  299. ... angle_range=(0, 360),
  300. ... direction_range=(-1.0, 1.0),
  301. ... allow_shifted=False, # Kernel will always be centered
  302. ... p=1.0
  303. ... )
  304. ... ])
  305. >>>
  306. >>> centered_result = centered_motion(image=image)
  307. >>> centered_blur = centered_result["image"]
  308. >>> # The image will have motion blur with the kernel centered (not shifted)
  309. >>>
  310. >>> # Example 7: In a composition with other transforms
  311. >>> pipeline = A.Compose([
  312. ... A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.5),
  313. ... A.MotionBlur( # 30% chance of applying motion blur
  314. ... blur_limit=(3, 7),
  315. ... angle_range=(0, 180), # Only horizontal to vertical
  316. ... direction_range=(-0.5, 0.5), # Moderate direction bias
  317. ... p=0.3
  318. ... ),
  319. ... A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=15, val_shift_limit=10, p=0.3)
  320. ... ])
  321. >>>
  322. >>> pipeline_result = pipeline(image=image)
  323. >>> transformed_image = pipeline_result["image"]
  324. >>> # The image may have motion blur applied with 30% probability along with other effects
  325. References:
  326. - Motion blur fundamentals:
  327. https://en.wikipedia.org/wiki/Motion_blur
  328. - Directional blur kernels:
  329. https://www.sciencedirect.com/topics/computer-science/directional-blur
  330. - OpenCV filter2D (used for convolution):
  331. https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga27c049795ce870216ddfb366086b5a04
  332. - Research on motion blur simulation:
  333. "Understanding and Evaluating Blind Deconvolution Algorithms" (CVPR 2009)
  334. https://doi.org/10.1109/CVPR.2009.5206815
  335. - Motion blur in photography:
  336. "The Manual of Photography", Chapter 7: Motion in Photography
  337. ISBN: 978-0240520377
  338. - Kornia's implementation (similar approach):
  339. https://kornia.readthedocs.io/en/latest/augmentation.html#kornia.augmentation.RandomMotionBlur
  340. See Also:
  341. - GaussianBlur: For uniform blur effects
  342. - MedianBlur: For noise reduction while preserving edges
  343. - RandomRain: Another motion-based effect
  344. - Perspective: For geometric motion-like distortions
  345. """
  346. class InitSchema(BlurInitSchema):
  347. allow_shifted: bool
  348. angle_range: Annotated[
  349. tuple[float, float],
  350. AfterValidator(nondecreasing),
  351. AfterValidator(check_range_bounds(0, 360)),
  352. ]
  353. direction_range: Annotated[
  354. tuple[float, float],
  355. AfterValidator(nondecreasing),
  356. AfterValidator(check_range_bounds(min_val=-1.0, max_val=1.0)),
  357. ]
  358. def __init__(
  359. self,
  360. blur_limit: tuple[int, int] | int = (3, 7),
  361. allow_shifted: bool = True,
  362. angle_range: tuple[float, float] = (0, 360),
  363. direction_range: tuple[float, float] = (-1.0, 1.0),
  364. p: float = 0.5,
  365. ):
  366. super().__init__(blur_limit=blur_limit, p=p)
  367. self.allow_shifted = allow_shifted
  368. self.blur_limit = cast("tuple[int, int]", blur_limit)
  369. self.angle_range = angle_range
  370. self.direction_range = direction_range
  371. def apply(self, img: np.ndarray, kernel: np.ndarray, **params: Any) -> np.ndarray:
  372. """Apply motion blur to the input image.
  373. Args:
  374. img (np.ndarray): Image to blur.
  375. kernel (np.ndarray): Kernel for motion blur.
  376. **params (Any): Additional parameters.
  377. Returns:
  378. np.ndarray: Motion blurred image.
  379. """
  380. return fpixel.convolve(img, kernel=kernel)
  381. def get_params(self) -> dict[str, Any]:
  382. """Get parameters for the transform.
  383. Returns:
  384. dict[str, Any]: Dictionary with parameters.
  385. """
  386. ksize = fblur.sample_odd_from_range(
  387. self.py_random,
  388. self.blur_limit[0],
  389. self.blur_limit[1],
  390. )
  391. angle = self.py_random.uniform(*self.angle_range)
  392. direction = self.py_random.uniform(*self.direction_range)
  393. # Create motion blur kernel
  394. kernel = fblur.create_motion_kernel(
  395. ksize,
  396. angle,
  397. direction,
  398. allow_shifted=self.allow_shifted,
  399. random_state=self.py_random,
  400. )
  401. return {"kernel": kernel.astype(np.float32) / np.sum(kernel)}
  402. class MedianBlur(Blur):
  403. """Apply median blur to the input image.
  404. This transform uses a median filter to blur the input image. Median filtering is particularly
  405. effective at removing salt-and-pepper noise while preserving edges, making it a popular choice
  406. for noise reduction in image processing.
  407. Args:
  408. blur_limit (int | tuple[int, int]): Maximum aperture linear size for blurring the input image.
  409. Must be odd and in the range [3, inf).
  410. - If a single int is provided, the kernel size will be randomly chosen
  411. between 3 and that value.
  412. - If a tuple of two ints is provided, it defines the inclusive range
  413. of possible kernel sizes.
  414. Default: (3, 7)
  415. p (float): Probability of applying the transform. Default: 0.5
  416. Targets:
  417. image
  418. Image types:
  419. uint8, float32
  420. Number of channels:
  421. Any
  422. Note:
  423. - The kernel size (aperture linear size) must always be odd and greater than 1.
  424. - Unlike mean blur or Gaussian blur, median blur uses the median of all pixels under
  425. the kernel area, making it more robust to outliers.
  426. - This transform is particularly useful for:
  427. * Removing salt-and-pepper noise
  428. * Preserving edges while smoothing images
  429. * Pre-processing images for edge detection algorithms
  430. - For color images, the median is calculated independently for each channel.
  431. - Larger kernel sizes result in stronger blurring effects but may also remove
  432. fine details from the image.
  433. Examples:
  434. >>> import numpy as np
  435. >>> import albumentations as A
  436. >>> import cv2
  437. >>>
  438. >>> # Create a sample image for demonstration
  439. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  440. >>> # Add some shapes to visualize blur effects
  441. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  442. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  443. >>> cv2.putText(image, "Sample Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  444. >>>
  445. >>> # Add salt and pepper noise to demonstrate median blur's noise removal capability
  446. >>> noise = np.zeros((300, 300, 3), dtype=np.uint8)
  447. >>> noise_points = np.random.random((300, 300)) > 0.95 # 5% of pixels as noise
  448. >>> image[noise_points] = 255 # White noise (salt)
  449. >>> noise_points = np.random.random((300, 300)) > 0.95 # Another 5% of pixels
  450. >>> image[noise_points] = 0 # Black noise (pepper)
  451. >>>
  452. >>> # Example 1: Minimal median blur (3x3 kernel)
  453. >>> minimal_blur = A.Compose([
  454. ... A.MedianBlur(
  455. ... blur_limit=3, # Fixed 3x3 kernel
  456. ... p=1.0 # Always apply
  457. ... )
  458. ... ])
  459. >>>
  460. >>> minimal_result = minimal_blur(image=image)
  461. >>> minimal_blurred = minimal_result["image"]
  462. >>> # The image will have minimal median blur, removing most salt and pepper noise
  463. >>> # while preserving edges and details
  464. >>>
  465. >>> # Example 2: Medium median blur
  466. >>> medium_blur = A.Compose([
  467. ... A.MedianBlur(
  468. ... blur_limit=5, # Fixed 5x5 kernel
  469. ... p=1.0
  470. ... )
  471. ... ])
  472. >>>
  473. >>> medium_result = medium_blur(image=image)
  474. >>> medium_blurred = medium_result["image"]
  475. >>> # The image will have a medium median blur, removing noise and small details
  476. >>> # while still preserving major edges
  477. >>>
  478. >>> # Example 3: Strong median blur
  479. >>> strong_blur = A.Compose([
  480. ... A.MedianBlur(
  481. ... blur_limit=9, # Fixed 9x9 kernel
  482. ... p=1.0
  483. ... )
  484. ... ])
  485. >>>
  486. >>> strong_result = strong_blur(image=image)
  487. >>> strong_blurred = strong_result["image"]
  488. >>> # The image will have a strong median blur, potentially removing smaller
  489. >>> # features while still preserving major edges better than other blur types
  490. >>>
  491. >>> # Example 4: Random kernel size range
  492. >>> random_kernel = A.Compose([
  493. ... A.MedianBlur(
  494. ... blur_limit=(3, 9), # Kernel size between 3x3 and 9x9
  495. ... p=1.0
  496. ... )
  497. ... ])
  498. >>>
  499. >>> random_result = random_kernel(image=image)
  500. >>> random_blurred = random_result["image"]
  501. >>> # The image will have a random median blur strength
  502. >>>
  503. >>> # Example 5: In a pipeline for noise reduction
  504. >>> pipeline = A.Compose([
  505. ... A.GaussNoise(var_limit=(10, 50), p=0.5), # Possibly add some noise
  506. ... A.MedianBlur(blur_limit=(3, 5), p=0.7), # 70% chance of applying median blur
  507. ... A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.3)
  508. ... ])
  509. >>>
  510. >>> pipeline_result = pipeline(image=image)
  511. >>> processed_image = pipeline_result["image"]
  512. >>> # The image may have been denoised with the median blur (70% probability)
  513. References:
  514. - Median filter: https://en.wikipedia.org/wiki/Median_filter
  515. - OpenCV medianBlur: https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9
  516. """
  517. def __init__(
  518. self,
  519. blur_limit: tuple[int, int] | int = (3, 7),
  520. p: float = 0.5,
  521. ):
  522. super().__init__(blur_limit=blur_limit, p=p)
  523. def apply(self, img: np.ndarray, kernel: int, **params: Any) -> np.ndarray:
  524. """Apply median blur to the input image.
  525. Args:
  526. img (np.ndarray): Image to blur.
  527. kernel (int): Size of the kernel for blur.
  528. **params (Any): Additional parameters.
  529. Returns:
  530. np.ndarray: Median blurred image.
  531. """
  532. return fblur.median_blur(img, kernel)
  533. class GaussianBlur(ImageOnlyTransform):
  534. """Apply Gaussian blur to the input image using a randomly sized kernel.
  535. This transform blurs the input image using a Gaussian filter with a random kernel size
  536. and sigma value. Gaussian blur is a widely used image processing technique that reduces
  537. image noise and detail, creating a smoothing effect.
  538. Args:
  539. sigma_limit (tuple[float, float] | float): Range for the Gaussian kernel standard
  540. deviation (sigma). Must be more or equal than 0.
  541. - If a single float is provided, sigma will be randomly chosen
  542. between 0 and that value.
  543. - If a tuple of two floats is provided, it defines the inclusive range
  544. of possible sigma values.
  545. Default: (0.5, 3.0)
  546. blur_limit (tuple[int, int] | int): Controls the range of the Gaussian kernel size.
  547. - If a single int is provided, the kernel size will be randomly chosen
  548. between 0 and that value.
  549. - If a tuple of two ints is provided, it defines the inclusive range
  550. of possible kernel sizes.
  551. Must be zero or odd and in range [0, inf). If set to 0 (default), the kernel size
  552. will be computed from sigma as `int(sigma * 3.5) * 2 + 1` to exactly match PIL's
  553. implementation.
  554. Default: 0
  555. p (float): Probability of applying the transform. Default: 0.5
  556. Targets:
  557. image
  558. Image types:
  559. uint8, float32
  560. Number of channels:
  561. Any
  562. Note:
  563. - When blur_limit=0 (default), this implementation exactly matches PIL's
  564. GaussianBlur behavior:
  565. * Kernel size is computed as int(sigma * 3.5) * 2 + 1
  566. * Gaussian values are computed using the standard formula
  567. * Kernel is normalized to preserve image luminance
  568. - When blur_limit is specified, the kernel size is randomly sampled from that range
  569. regardless of sigma, which might result in inconsistent blur effects.
  570. - The default sigma range (0.5, 3.0) provides a good balance between subtle
  571. and strong blur effects:
  572. * sigma=0.5 results in a subtle blur
  573. * sigma=3.0 results in a stronger blur
  574. Examples:
  575. >>> import numpy as np
  576. >>> import albumentations as A
  577. >>> import cv2
  578. >>>
  579. >>> # Create a sample image for demonstration
  580. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  581. >>> # Add some shapes to visualize blur effects
  582. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  583. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  584. >>> cv2.putText(image, "Sample Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  585. >>>
  586. >>> # Example 1: Default Gaussian blur (automatic kernel size)
  587. >>> default_blur = A.Compose([
  588. ... A.GaussianBlur(p=1.0) # Using default parameters
  589. ... ])
  590. >>>
  591. >>> default_result = default_blur(image=image)
  592. >>> default_blurred = default_result["image"]
  593. >>> # The image will have a medium Gaussian blur with sigma between 0.5 and 3.0
  594. >>>
  595. >>> # Example 2: Light Gaussian blur
  596. >>> light_blur = A.Compose([
  597. ... A.GaussianBlur(
  598. ... sigma_limit=(0.2, 0.5), # Small sigma for subtle blur
  599. ... blur_limit=0, # Auto-compute kernel size
  600. ... p=1.0
  601. ... )
  602. ... ])
  603. >>>
  604. >>> light_result = light_blur(image=image)
  605. >>> light_blurred = light_result["image"]
  606. >>> # The image will have a subtle Gaussian blur effect
  607. >>>
  608. >>> # Example 3: Strong Gaussian blur
  609. >>> strong_blur = A.Compose([
  610. ... A.GaussianBlur(
  611. ... sigma_limit=(3.0, 7.0), # Larger sigma for stronger blur
  612. ... blur_limit=0, # Auto-compute kernel size
  613. ... p=1.0
  614. ... )
  615. ... ])
  616. >>>
  617. >>> strong_result = strong_blur(image=image)
  618. >>> strong_blurred = strong_result["image"]
  619. >>> # The image will have a strong Gaussian blur effect
  620. >>>
  621. >>> # Example 4: Fixed kernel size
  622. >>> fixed_kernel = A.Compose([
  623. ... A.GaussianBlur(
  624. ... sigma_limit=(0.5, 2.0),
  625. ... blur_limit=(9, 9), # Fixed 9x9 kernel size
  626. ... p=1.0
  627. ... )
  628. ... ])
  629. >>>
  630. >>> fixed_result = fixed_kernel(image=image)
  631. >>> fixed_kernel_blur = fixed_result["image"]
  632. >>> # The image will have Gaussian blur with a fixed 9x9 kernel
  633. >>>
  634. >>> # Example 5: Random kernel size range
  635. >>> random_kernel = A.Compose([
  636. ... A.GaussianBlur(
  637. ... sigma_limit=(1.0, 2.0),
  638. ... blur_limit=(5, 9), # Kernel size between 5x5 and 9x9
  639. ... p=1.0
  640. ... )
  641. ... ])
  642. >>>
  643. >>> random_result = random_kernel(image=image)
  644. >>> random_kernel_blur = random_result["image"]
  645. >>> # The image will have Gaussian blur with a kernel size between 5x5 and 9x9
  646. >>>
  647. >>> # Example 6: In an augmentation pipeline
  648. >>> pipeline = A.Compose([
  649. ... A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.5),
  650. ... A.GaussianBlur(sigma_limit=(0.5, 1.5), p=0.3), # 30% chance of applying
  651. ... A.RGBShift(r_shift_limit=10, g_shift_limit=10, b_shift_limit=10, p=0.3)
  652. ... ])
  653. >>>
  654. >>> pipeline_result = pipeline(image=image)
  655. >>> transformed_image = pipeline_result["image"]
  656. >>> # The image may have Gaussian blur applied with 30% probability along with other effects
  657. References:
  658. - OpenCV Gaussian Blur: https://docs.opencv.org/master/d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1
  659. - PIL GaussianBlur: https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html#PIL.ImageFilter.GaussianBlur
  660. """
  661. class InitSchema(BaseTransformInitSchema):
  662. sigma_limit: Annotated[
  663. tuple[float, float] | float,
  664. AfterValidator(process_non_negative_range),
  665. AfterValidator(nondecreasing),
  666. ]
  667. blur_limit: Annotated[
  668. tuple[int, int] | int,
  669. AfterValidator(convert_to_0plus_range),
  670. AfterValidator(nondecreasing),
  671. ]
  672. def __init__(
  673. self,
  674. blur_limit: tuple[int, int] | int = 0,
  675. sigma_limit: tuple[float, float] | float = (0.5, 3.0),
  676. p: float = 0.5,
  677. ):
  678. super().__init__(p=p)
  679. self.blur_limit = cast("tuple[int, int]", blur_limit)
  680. self.sigma_limit = cast("tuple[float, float]", sigma_limit)
  681. def apply(
  682. self,
  683. img: np.ndarray,
  684. kernel: np.ndarray,
  685. **params: Any,
  686. ) -> np.ndarray:
  687. """Apply Gaussian blur to the input image.
  688. Args:
  689. img (np.ndarray): Image to blur.
  690. kernel (np.ndarray): Kernel for Gaussian blur.
  691. **params (Any): Additional parameters.
  692. Returns:
  693. np.ndarray: Gaussian blurred image.
  694. """
  695. return fpixel.separable_convolve(img, kernel=kernel)
  696. def get_params_dependent_on_data(self, params: dict[str, Any], data: dict[str, Any]) -> dict[str, float]:
  697. """Get parameters that depend on input data.
  698. Args:
  699. params (dict[str, Any]): Parameters.
  700. data (dict[str, Any]): Input data.
  701. Returns:
  702. dict[str, float]: Dictionary with parameters.
  703. """
  704. sigma = self.py_random.uniform(*self.sigma_limit)
  705. ksize = self.py_random.randint(*self.blur_limit)
  706. return {"kernel": fblur.create_gaussian_kernel_1d(sigma, ksize)}
  707. class GlassBlur(ImageOnlyTransform):
  708. """Apply a glass blur effect to the input image.
  709. This transform simulates the effect of looking through textured glass by locally
  710. shuffling pixels in the image. It creates a distorted, frosted glass-like appearance.
  711. Args:
  712. sigma (float): Standard deviation for the Gaussian kernel used in the process.
  713. Higher values increase the blur effect. Must be non-negative.
  714. Default: 0.7
  715. max_delta (int): Maximum distance in pixels for shuffling.
  716. Determines how far pixels can be moved. Larger values create more distortion.
  717. Must be a positive integer.
  718. Default: 4
  719. iterations (int): Number of times to apply the glass blur effect.
  720. More iterations create a stronger effect but increase computation time.
  721. Must be a positive integer.
  722. Default: 2
  723. mode (Literal["fast", "exact"]): Mode of computation. Options are:
  724. - "fast": Uses a faster but potentially less accurate method.
  725. - "exact": Uses a slower but more precise method.
  726. Default: "fast"
  727. p (float): Probability of applying the transform. Should be in the range [0, 1].
  728. Default: 0.5
  729. Targets:
  730. image
  731. Image types:
  732. uint8, float32
  733. Number of channels:
  734. Any
  735. Note:
  736. - This transform is particularly effective for creating a 'looking through
  737. glass' effect or simulating the view through a frosted window.
  738. - The 'fast' mode is recommended for most use cases as it provides a good
  739. balance between effect quality and computation speed.
  740. - Increasing 'iterations' will strengthen the effect but also increase the
  741. processing time linearly.
  742. Examples:
  743. >>> import numpy as np
  744. >>> import albumentations as A
  745. >>> import cv2
  746. >>>
  747. >>> # Create a sample image for demonstration
  748. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  749. >>> # Add some shapes to visualize glass blur effects
  750. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  751. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  752. >>> cv2.putText(image, "Text Sample", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  753. >>>
  754. >>> # Example 1: Subtle glass effect (light frosting)
  755. >>> subtle_transform = A.Compose([
  756. ... A.GlassBlur(
  757. ... sigma=0.4, # Lower sigma for gentler blur
  758. ... max_delta=2, # Small displacement
  759. ... iterations=1, # Single iteration
  760. ... mode="fast",
  761. ... p=1.0 # Always apply
  762. ... )
  763. ... ])
  764. >>>
  765. >>> subtle_result = subtle_transform(image=image)
  766. >>> subtle_glass = subtle_result["image"]
  767. >>> # The image will have a subtle glass-like distortion, like light frosting
  768. >>>
  769. >>> # Example 2: Medium glass effect (typical frosted glass)
  770. >>> medium_transform = A.Compose([
  771. ... A.GlassBlur(
  772. ... sigma=0.7, # Default sigma
  773. ... max_delta=4, # Default displacement
  774. ... iterations=2, # Default iterations
  775. ... mode="fast",
  776. ... p=1.0
  777. ... )
  778. ... ])
  779. >>>
  780. >>> medium_result = medium_transform(image=image)
  781. >>> medium_glass = medium_result["image"]
  782. >>> # The image will have a moderate glass-like effect, similar to standard frosted glass
  783. >>>
  784. >>> # Example 3: Strong glass effect (heavy distortion)
  785. >>> strong_transform = A.Compose([
  786. ... A.GlassBlur(
  787. ... sigma=1.0, # Higher sigma for stronger blur
  788. ... max_delta=6, # Larger displacement
  789. ... iterations=3, # More iterations
  790. ... mode="fast",
  791. ... p=1.0
  792. ... )
  793. ... ])
  794. >>>
  795. >>> strong_result = strong_transform(image=image)
  796. >>> strong_glass = strong_result["image"]
  797. >>> # The image will have a strong glass-like distortion, heavily obscuring details
  798. >>>
  799. >>> # Example 4: Using exact mode for higher quality
  800. >>> exact_transform = A.Compose([
  801. ... A.GlassBlur(
  802. ... sigma=0.7,
  803. ... max_delta=4,
  804. ... iterations=2,
  805. ... mode="exact", # More precise but slower
  806. ... p=1.0
  807. ... )
  808. ... ])
  809. >>>
  810. >>> exact_result = exact_transform(image=image)
  811. >>> exact_glass = exact_result["image"]
  812. >>> # The image will have a similar effect to medium, but with potentially better quality
  813. >>>
  814. >>> # Example 5: In a pipeline with other transforms
  815. >>> pipeline = A.Compose([
  816. ... A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.7),
  817. ... A.GlassBlur(sigma=0.7, max_delta=4, iterations=2, p=0.5), # 50% chance of applying
  818. ... A.HueSaturationValue(hue_shift_limit=10, sat_shift_limit=15, val_shift_limit=10, p=0.3)
  819. ... ])
  820. >>>
  821. >>> pipeline_result = pipeline(image=image)
  822. >>> transformed_image = pipeline_result["image"]
  823. >>> # The image may have glass blur applied with 50% probability along with other effects
  824. References:
  825. - This implementation is based on the technique described in:
  826. "ImageNet-trained CNNs are biased towards texture; increasing shape bias improves accuracy and robustness"
  827. https://arxiv.org/abs/1903.12261
  828. - Original implementation:
  829. https://github.com/hendrycks/robustness/blob/master/ImageNet-C/create_c/make_imagenet_c.py
  830. """
  831. class InitSchema(BaseTransformInitSchema):
  832. sigma: float = Field(ge=0)
  833. max_delta: int = Field(ge=1)
  834. iterations: int = Field(ge=1)
  835. mode: Literal["fast", "exact"]
  836. def __init__(
  837. self,
  838. sigma: float = 0.7,
  839. max_delta: int = 4,
  840. iterations: int = 2,
  841. mode: Literal["fast", "exact"] = "fast",
  842. p: float = 0.5,
  843. ):
  844. super().__init__(p=p)
  845. self.sigma = sigma
  846. self.max_delta = max_delta
  847. self.iterations = iterations
  848. self.mode = mode
  849. def apply(
  850. self,
  851. img: np.ndarray,
  852. *args: Any,
  853. dxy: np.ndarray,
  854. **params: Any,
  855. ) -> np.ndarray:
  856. """Apply glass blur effect to the input image.
  857. Args:
  858. img (np.ndarray): Image to blur.
  859. *args (Any): Additional positional arguments.
  860. dxy (np.ndarray): Displacement map.
  861. **params (Any): Additional parameters.
  862. Returns:
  863. np.ndarray: Image with glass blur effect.
  864. """
  865. return fblur.glass_blur(
  866. img,
  867. self.sigma,
  868. self.max_delta,
  869. self.iterations,
  870. dxy,
  871. self.mode,
  872. )
  873. def get_params_dependent_on_data(
  874. self,
  875. params: dict[str, Any],
  876. data: dict[str, Any],
  877. ) -> dict[str, np.ndarray]:
  878. """Get parameters that depend on input data.
  879. Args:
  880. params (dict[str, Any]): Parameters.
  881. data (dict[str, Any]): Input data.
  882. Returns:
  883. dict[str, np.ndarray]: Dictionary with parameters.
  884. """
  885. height, width = params["shape"][:2]
  886. # generate array containing all necessary values for transformations
  887. width_pixels = height - self.max_delta * 2
  888. height_pixels = width - self.max_delta * 2
  889. total_pixels = int(width_pixels * height_pixels)
  890. dxy = self.random_generator.integers(
  891. -self.max_delta,
  892. self.max_delta,
  893. size=(total_pixels, self.iterations, 2),
  894. )
  895. return {"dxy": dxy}
  896. class AdvancedBlur(ImageOnlyTransform):
  897. """Applies a Generalized Gaussian blur to the input image with randomized parameters for advanced data augmentation.
  898. This transform creates a custom blur kernel based on the Generalized Gaussian distribution,
  899. which allows for a wide range of blur effects beyond standard Gaussian blur. It then applies
  900. this kernel to the input image through convolution. The transform also incorporates noise
  901. into the kernel, resulting in a unique combination of blurring and noise injection.
  902. Key features of this augmentation:
  903. 1. Generalized Gaussian Kernel: Uses a generalized normal distribution to create kernels
  904. that can range from box-like blurs to very peaked blurs, controlled by the beta parameter.
  905. 2. Anisotropic Blurring: Allows for different blur strengths in horizontal and vertical
  906. directions (controlled by sigma_x and sigma_y), and rotation of the kernel.
  907. 3. Kernel Noise: Adds multiplicative noise to the kernel before applying it to the image,
  908. creating more diverse and realistic blur effects.
  909. Implementation Details:
  910. The kernel is generated using a 2D Generalized Gaussian function. The process involves:
  911. 1. Creating a 2D grid based on the kernel size
  912. 2. Applying rotation to this grid
  913. 3. Calculating the kernel values using the Generalized Gaussian formula
  914. 4. Adding multiplicative noise to the kernel
  915. 5. Normalizing the kernel
  916. The resulting kernel is then applied to the image using convolution.
  917. Args:
  918. blur_limit (tuple[int, int] | int, optional): Controls the size of the blur kernel. If a single int
  919. is provided, the kernel size will be randomly chosen between 3 and that value.
  920. Must be odd and ≥ 3. Larger values create stronger blur effects.
  921. Default: (3, 7)
  922. sigma_x_limit (tuple[float, float] | float): Controls the spread of the blur in the x direction.
  923. Higher values increase blur strength.
  924. If a single float is provided, the range will be (0, limit).
  925. Default: (0.2, 1.0)
  926. sigma_y_limit (tuple[float, float] | float): Controls the spread of the blur in the y direction.
  927. Higher values increase blur strength.
  928. If a single float is provided, the range will be (0, limit).
  929. Default: (0.2, 1.0)
  930. rotate_limit (tuple[int, int] | int): Range of angles (in degrees) for rotating the kernel.
  931. This rotation allows for diagonal blur directions. If limit is a single int, an angle is picked
  932. from (-rotate_limit, rotate_limit).
  933. Default: (-90, 90)
  934. beta_limit (tuple[float, float] | float): Shape parameter of the Generalized Gaussian distribution.
  935. - beta = 1 gives a standard Gaussian distribution
  936. - beta < 1 creates heavier tails, resulting in more uniform, box-like blur
  937. - beta > 1 creates lighter tails, resulting in more peaked, focused blur
  938. Default: (0.5, 8.0)
  939. noise_limit (tuple[float, float] | float): Controls the strength of multiplicative noise
  940. applied to the kernel. Values around 1.0 keep the original kernel mostly intact,
  941. while values further from 1.0 introduce more variation.
  942. Default: (0.75, 1.25)
  943. p (float): Probability of applying the transform. Default: 0.5
  944. Notes:
  945. - This transform is particularly useful for simulating complex, real-world blur effects
  946. that go beyond simple Gaussian blur.
  947. - The combination of blur and noise can help in creating more robust models by simulating
  948. a wider range of image degradations.
  949. - Extreme values, especially for beta and noise, may result in unrealistic effects and
  950. should be used cautiously.
  951. Examples:
  952. >>> import numpy as np
  953. >>> import albumentations as A
  954. >>> import cv2
  955. >>>
  956. >>> # Create a sample image for demonstration
  957. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  958. >>> # Add some shapes to visualize blur effects
  959. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  960. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  961. >>> cv2.putText(image, "Text Example", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  962. >>> cv2.line(image, (50, 250), (250, 250), (0, 0, 255), 3) # Blue line
  963. >>>
  964. >>> # Example 1: Gaussian-like blur (beta = 1)
  965. >>> gaussian_like = A.Compose([
  966. ... A.AdvancedBlur(
  967. ... blur_limit=5,
  968. ... sigma_x_limit=(0.5, 0.5),
  969. ... sigma_y_limit=(0.5, 0.5),
  970. ... rotate_limit=0,
  971. ... beta_limit=(1.0, 1.0), # Standard Gaussian (beta = 1)
  972. ... noise_limit=(1.0, 1.0), # No noise
  973. ... p=1.0
  974. ... )
  975. ... ])
  976. >>>
  977. >>> gaussian_result = gaussian_like(image=image)
  978. >>> gaussian_image = gaussian_result["image"]
  979. >>> # The image will have a standard Gaussian blur applied
  980. >>>
  981. >>> # Example 2: Box-like blur (beta < 1)
  982. >>> box_like = A.Compose([
  983. ... A.AdvancedBlur(
  984. ... blur_limit=(7, 9),
  985. ... sigma_x_limit=(0.6, 0.8),
  986. ... sigma_y_limit=(0.6, 0.8),
  987. ... rotate_limit=0,
  988. ... beta_limit=(0.5, 0.7), # Box-like blur (beta < 1)
  989. ... noise_limit=(0.9, 1.1), # Slight noise
  990. ... p=1.0
  991. ... )
  992. ... ])
  993. >>>
  994. >>> box_result = box_like(image=image)
  995. >>> box_image = box_result["image"]
  996. >>> # The image will have a more box-like blur with heavier tails
  997. >>>
  998. >>> # Example 3: Peaked blur (beta > 1)
  999. >>> peaked = A.Compose([
  1000. ... A.AdvancedBlur(
  1001. ... blur_limit=(7, 9),
  1002. ... sigma_x_limit=(0.6, 0.8),
  1003. ... sigma_y_limit=(0.6, 0.8),
  1004. ... rotate_limit=0,
  1005. ... beta_limit=(3.0, 6.0), # Peaked blur (beta > 1)
  1006. ... noise_limit=(0.9, 1.1), # Slight noise
  1007. ... p=1.0
  1008. ... )
  1009. ... ])
  1010. >>>
  1011. >>> peaked_result = peaked(image=image)
  1012. >>> peaked_image = peaked_result["image"]
  1013. >>> # The image will have a more focused, peaked blur with lighter tails
  1014. >>>
  1015. >>> # Example 4: Anisotropic blur (directional)
  1016. >>> directional = A.Compose([
  1017. ... A.AdvancedBlur(
  1018. ... blur_limit=(9, 11),
  1019. ... sigma_x_limit=(0.8, 1.0), # Stronger x blur
  1020. ... sigma_y_limit=(0.2, 0.3), # Weaker y blur
  1021. ... rotate_limit=(0, 0), # No rotation
  1022. ... beta_limit=(1.0, 2.0),
  1023. ... noise_limit=(0.9, 1.1),
  1024. ... p=1.0
  1025. ... )
  1026. ... ])
  1027. >>>
  1028. >>> directional_result = directional(image=image)
  1029. >>> directional_image = directional_result["image"]
  1030. >>> # The image will have a horizontal directional blur
  1031. >>>
  1032. >>> # Example 5: Rotated directional blur
  1033. >>> rotated = A.Compose([
  1034. ... A.AdvancedBlur(
  1035. ... blur_limit=(9, 11),
  1036. ... sigma_x_limit=(0.8, 1.0), # Stronger x blur
  1037. ... sigma_y_limit=(0.2, 0.3), # Weaker y blur
  1038. ... rotate_limit=(45, 45), # 45 degree rotation
  1039. ... beta_limit=(1.0, 2.0),
  1040. ... noise_limit=(0.9, 1.1),
  1041. ... p=1.0
  1042. ... )
  1043. ... ])
  1044. >>>
  1045. >>> rotated_result = rotated(image=image)
  1046. >>> rotated_image = rotated_result["image"]
  1047. >>> # The image will have a diagonal directional blur
  1048. >>>
  1049. >>> # Example 6: Noisy blur
  1050. >>> noisy = A.Compose([
  1051. ... A.AdvancedBlur(
  1052. ... blur_limit=(5, 7),
  1053. ... sigma_x_limit=(0.4, 0.6),
  1054. ... sigma_y_limit=(0.4, 0.6),
  1055. ... rotate_limit=(-30, 30),
  1056. ... beta_limit=(0.8, 1.2),
  1057. ... noise_limit=(0.7, 1.3), # Strong noise variation
  1058. ... p=1.0
  1059. ... )
  1060. ... ])
  1061. >>>
  1062. >>> noisy_result = noisy(image=image)
  1063. >>> noisy_image = noisy_result["image"]
  1064. >>> # The image will have blur with significant noise in the kernel
  1065. >>>
  1066. >>> # Example 7: Random parameters (for general augmentation)
  1067. >>> random_blur = A.Compose([
  1068. ... A.AdvancedBlur(p=0.5) # Using default parameter ranges
  1069. ... ])
  1070. >>>
  1071. >>> random_result = random_blur(image=image)
  1072. >>> random_image = random_result["image"]
  1073. >>> # The image may have a random advanced blur applied with 50% probability
  1074. Reference:
  1075. This transform is inspired by techniques described in:
  1076. "Real-ESRGAN: Training Real-World Blind Super-Resolution with Pure Synthetic Data"
  1077. https://arxiv.org/abs/2107.10833
  1078. Targets:
  1079. image
  1080. Image types:
  1081. uint8, float32
  1082. """
  1083. class InitSchema(BlurInitSchema):
  1084. sigma_x_limit: NonNegativeFloatRangeType
  1085. sigma_y_limit: NonNegativeFloatRangeType
  1086. beta_limit: NonNegativeFloatRangeType
  1087. noise_limit: NonNegativeFloatRangeType
  1088. rotate_limit: SymmetricRangeType
  1089. @field_validator("beta_limit")
  1090. @classmethod
  1091. def _check_beta_limit(cls, value: tuple[float, float] | float) -> tuple[float, float]:
  1092. result = to_tuple(value, low=0)
  1093. if not (result[0] < 1.0 < result[1]):
  1094. raise ValueError(
  1095. f"Beta limit should include 1.0, got {result}",
  1096. )
  1097. return result
  1098. @model_validator(mode="after")
  1099. def _validate_limits(self) -> Self:
  1100. if (
  1101. isinstance(self.sigma_x_limit, (tuple, list))
  1102. and self.sigma_x_limit[0] == 0
  1103. and isinstance(self.sigma_y_limit, (tuple, list))
  1104. and self.sigma_y_limit[0] == 0
  1105. ):
  1106. msg = "sigma_x_limit and sigma_y_limit minimum value cannot be both equal to 0."
  1107. raise ValueError(msg)
  1108. return self
  1109. def __init__(
  1110. self,
  1111. blur_limit: tuple[int, int] | int = (3, 7),
  1112. sigma_x_limit: tuple[float, float] | float = (0.2, 1.0),
  1113. sigma_y_limit: tuple[float, float] | float = (0.2, 1.0),
  1114. rotate_limit: tuple[int, int] | int = (-90, 90),
  1115. beta_limit: tuple[float, float] | float = (0.5, 8.0),
  1116. noise_limit: tuple[float, float] | float = (0.9, 1.1),
  1117. p: float = 0.5,
  1118. ):
  1119. super().__init__(p=p)
  1120. self.blur_limit = cast("tuple[int, int]", blur_limit)
  1121. self.sigma_x_limit = cast("tuple[float, float]", sigma_x_limit)
  1122. self.sigma_y_limit = cast("tuple[float, float]", sigma_y_limit)
  1123. self.rotate_limit = cast("tuple[int, int]", rotate_limit)
  1124. self.beta_limit = cast("tuple[float, float]", beta_limit)
  1125. self.noise_limit = cast("tuple[float, float]", noise_limit)
  1126. def apply(self, img: np.ndarray, kernel: np.ndarray, **params: Any) -> np.ndarray:
  1127. """Apply advanced blur to the input image.
  1128. Args:
  1129. img (np.ndarray): Image to blur.
  1130. kernel (np.ndarray): Kernel for blur.
  1131. **params (Any): Additional parameters.
  1132. Returns:
  1133. np.ndarray: Blurred image.
  1134. """
  1135. return fpixel.convolve(img, kernel=kernel)
  1136. def get_params(self) -> dict[str, np.ndarray]:
  1137. """Get parameters for the transform.
  1138. Returns:
  1139. dict[str, np.ndarray]: Dictionary with parameters.
  1140. """
  1141. ksize = fblur.sample_odd_from_range(
  1142. self.py_random,
  1143. self.blur_limit[0],
  1144. self.blur_limit[1],
  1145. )
  1146. sigma_x = self.py_random.uniform(*self.sigma_x_limit)
  1147. sigma_y = self.py_random.uniform(*self.sigma_y_limit)
  1148. angle = np.deg2rad(self.py_random.uniform(*self.rotate_limit))
  1149. # Split into 2 cases to avoid selection of narrow kernels (beta > 1) too often.
  1150. beta = (
  1151. self.py_random.uniform(self.beta_limit[0], 1)
  1152. if self.py_random.random() < HALF
  1153. else self.py_random.uniform(1, self.beta_limit[1])
  1154. )
  1155. noise_matrix = self.random_generator.uniform(
  1156. *self.noise_limit,
  1157. size=(ksize, ksize),
  1158. )
  1159. # Generate mesh grid centered at zero.
  1160. ax = np.arange(-ksize // 2 + 1.0, ksize // 2 + 1.0)
  1161. # > Shape (ksize, ksize, 2)
  1162. grid = np.stack(np.meshgrid(ax, ax), axis=-1)
  1163. # Calculate rotated sigma matrix
  1164. d_matrix = np.array([[sigma_x**2, 0], [0, sigma_y**2]])
  1165. u_matrix = np.array(
  1166. [[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]],
  1167. )
  1168. sigma_matrix = np.dot(u_matrix, np.dot(d_matrix, u_matrix.T))
  1169. inverse_sigma = np.linalg.inv(sigma_matrix)
  1170. # Described in "Parameter Estimation For Multivariate Generalized Gaussian Distributions"
  1171. kernel = np.exp(
  1172. -0.5 * np.power(np.sum(np.dot(grid, inverse_sigma) * grid, 2), beta),
  1173. )
  1174. # Add noise
  1175. kernel *= noise_matrix
  1176. # Normalize kernel
  1177. kernel = kernel.astype(np.float32) / np.sum(kernel)
  1178. return {"kernel": kernel}
  1179. class Defocus(ImageOnlyTransform):
  1180. """Apply defocus blur to the input image.
  1181. This transform simulates the effect of an out-of-focus camera by applying a defocus blur
  1182. to the image. It uses a combination of disc kernels and Gaussian blur to create a realistic
  1183. defocus effect.
  1184. Args:
  1185. radius (tuple[int, int] | int): Range for the radius of the defocus blur.
  1186. If a single int is provided, the range will be [1, radius].
  1187. Larger values create a stronger blur effect.
  1188. Default: (3, 10)
  1189. alias_blur (tuple[float, float] | float): Range for the standard deviation of the Gaussian blur
  1190. applied after the main defocus blur. This helps to reduce aliasing artifacts.
  1191. If a single float is provided, the range will be (0, alias_blur).
  1192. Larger values create a smoother, more aliased effect.
  1193. Default: (0.1, 0.5)
  1194. p (float): Probability of applying the transform. Should be in the range [0, 1].
  1195. Default: 0.5
  1196. Targets:
  1197. image
  1198. Image types:
  1199. uint8, float32
  1200. Note:
  1201. - The defocus effect is created using a disc kernel, which simulates the shape of a camera's aperture.
  1202. - The additional Gaussian blur (alias_blur) helps to soften the edges of the disc kernel, creating a
  1203. more natural-looking defocus effect.
  1204. - Larger radius values will create a stronger, more noticeable defocus effect.
  1205. - The alias_blur parameter can be used to fine-tune the appearance of the defocus, with larger values
  1206. creating a smoother, potentially more realistic effect.
  1207. Examples:
  1208. >>> import numpy as np
  1209. >>> import albumentations as A
  1210. >>> import cv2
  1211. >>>
  1212. >>> # Create a sample image for demonstration
  1213. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  1214. >>> # Add some shapes to visualize defocus effects
  1215. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  1216. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  1217. >>> cv2.putText(image, "Sharp Text", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
  1218. >>>
  1219. >>> # Example 1: Subtle defocus effect (small aperture)
  1220. >>> subtle_transform = A.Compose([
  1221. ... A.Defocus(
  1222. ... radius=(2, 3), # Small defocus radius
  1223. ... alias_blur=(0.1, 0.2), # Minimal aliasing
  1224. ... p=1.0 # Always apply
  1225. ... )
  1226. ... ])
  1227. >>>
  1228. >>> subtle_result = subtle_transform(image=image)
  1229. >>> subtle_defocus = subtle_result["image"]
  1230. >>> # The image will have a subtle defocus effect, with just slight blurring
  1231. >>>
  1232. >>> # Example 2: Moderate defocus effect (medium aperture)
  1233. >>> moderate_transform = A.Compose([
  1234. ... A.Defocus(
  1235. ... radius=(4, 6), # Medium defocus radius
  1236. ... alias_blur=(0.2, 0.3), # Moderate aliasing
  1237. ... p=1.0
  1238. ... )
  1239. ... ])
  1240. >>>
  1241. >>> moderate_result = moderate_transform(image=image)
  1242. >>> moderate_defocus = moderate_result["image"]
  1243. >>> # The image will have a noticeable defocus effect, similar to a poorly focused camera
  1244. >>>
  1245. >>> # Example 3: Strong defocus effect (large aperture)
  1246. >>> strong_transform = A.Compose([
  1247. ... A.Defocus(
  1248. ... radius=(8, 12), # Large defocus radius
  1249. ... alias_blur=(0.4, 0.6), # Strong aliasing
  1250. ... p=1.0
  1251. ... )
  1252. ... ])
  1253. >>>
  1254. >>> strong_result = strong_transform(image=image)
  1255. >>> strong_defocus = strong_result["image"]
  1256. >>> # The image will have a strong defocus effect, heavily blurring the details
  1257. >>>
  1258. >>> # Example 4: Using in a pipeline with other transforms
  1259. >>> pipeline = A.Compose([
  1260. ... A.RandomBrightnessContrast(brightness_limit=0.1, contrast_limit=0.1, p=0.7),
  1261. ... A.Defocus(radius=(3, 8), alias_blur=0.3, p=0.5), # 50% chance of applying defocus
  1262. ... A.GaussNoise(var_limit=(10, 30), p=0.3) # Possible noise after defocus
  1263. ... ])
  1264. >>>
  1265. >>> pipeline_result = pipeline(image=image)
  1266. >>> transformed_image = pipeline_result["image"]
  1267. >>> # The image may have defocus blur applied with 50% probability
  1268. References:
  1269. Defocus aberration: https://en.wikipedia.org/wiki/Defocus_aberration
  1270. """
  1271. class InitSchema(BaseTransformInitSchema):
  1272. radius: OnePlusIntRangeType
  1273. alias_blur: NonNegativeFloatRangeType
  1274. def __init__(
  1275. self,
  1276. radius: tuple[int, int] | int = (3, 10),
  1277. alias_blur: tuple[float, float] | float = (0.1, 0.5),
  1278. p: float = 0.5,
  1279. ):
  1280. super().__init__(p=p)
  1281. self.radius = cast("tuple[int, int]", radius)
  1282. self.alias_blur = cast("tuple[float, float]", alias_blur)
  1283. def apply(
  1284. self,
  1285. img: np.ndarray,
  1286. radius: int,
  1287. alias_blur: float,
  1288. **params: Any,
  1289. ) -> np.ndarray:
  1290. """Apply defocus blur to the input image.
  1291. Args:
  1292. img (np.ndarray): Image to blur.
  1293. radius (int): Radius of the defocus blur.
  1294. alias_blur (float): Standard deviation of the Gaussian blur.
  1295. **params (Any): Additional parameters.
  1296. Returns:
  1297. np.ndarray: Defocused image.
  1298. """
  1299. return fblur.defocus(img, radius, alias_blur)
  1300. def get_params(self) -> dict[str, Any]:
  1301. """Get parameters for the transform.
  1302. Returns:
  1303. dict[str, Any]: Dictionary with parameters.
  1304. """
  1305. return {
  1306. "radius": self.py_random.randint(*self.radius),
  1307. "alias_blur": self.py_random.uniform(*self.alias_blur),
  1308. }
  1309. class ZoomBlur(ImageOnlyTransform):
  1310. """Apply zoom blur transform.
  1311. This transform simulates the effect of zooming during exposure, creating a dynamic radial blur.
  1312. It works by averaging multiple versions of the image at different zoom levels, creating
  1313. a smooth transition from the center outward.
  1314. Args:
  1315. max_factor ((float, float) or float): range for max factor for blurring.
  1316. If max_factor is a single float, the range will be (1, limit). Default: (1, 1.31).
  1317. All max_factor values should be larger than 1.
  1318. step_factor ((float, float) or float): If single float will be used as step parameter for np.arange.
  1319. If tuple of float step_factor will be in range `[step_factor[0], step_factor[1])`. Default: (0.01, 0.03).
  1320. All step_factor values should be positive.
  1321. p (float): probability of applying the transform. Default: 0.5.
  1322. Targets:
  1323. image
  1324. Image types:
  1325. uint8, float32
  1326. Examples:
  1327. >>> import numpy as np
  1328. >>> import albumentations as A
  1329. >>> import cv2
  1330. >>>
  1331. >>> # Create a sample image for demonstration
  1332. >>> image = np.zeros((300, 300, 3), dtype=np.uint8)
  1333. >>> # Add some shapes to visualize zoom blur effects
  1334. >>> cv2.rectangle(image, (100, 100), (200, 200), (255, 0, 0), -1) # Red square
  1335. >>> cv2.circle(image, (150, 150), 30, (0, 255, 0), -1) # Green circle
  1336. >>> cv2.line(image, (50, 150), (250, 150), (0, 0, 255), 5) # Blue line
  1337. >>>
  1338. >>> # Example 1: Subtle zoom blur
  1339. >>> subtle_transform = A.Compose([
  1340. ... A.ZoomBlur(
  1341. ... max_factor=(1.05, 1.10), # Small zoom range
  1342. ... step_factor=0.01, # Fine steps
  1343. ... p=1.0 # Always apply
  1344. ... )
  1345. ... ])
  1346. >>>
  1347. >>> subtle_result = subtle_transform(image=image)
  1348. >>> subtle_blur = subtle_result["image"]
  1349. >>> # The image will have a subtle zoom blur effect, simulating a slight zoom during exposure
  1350. >>>
  1351. >>> # Example 2: Moderate zoom blur
  1352. >>> moderate_transform = A.Compose([
  1353. ... A.ZoomBlur(
  1354. ... max_factor=(1.15, 1.25), # Medium zoom range
  1355. ... step_factor=0.02, # Medium steps
  1356. ... p=1.0
  1357. ... )
  1358. ... ])
  1359. >>>
  1360. >>> moderate_result = moderate_transform(image=image)
  1361. >>> moderate_blur = moderate_result["image"]
  1362. >>> # The image will have a more noticeable zoom blur effect
  1363. >>>
  1364. >>> # Example 3: Strong zoom blur
  1365. >>> strong_transform = A.Compose([
  1366. ... A.ZoomBlur(
  1367. ... max_factor=(1.3, 1.5), # Large zoom range
  1368. ... step_factor=(0.03, 0.05), # Larger steps (randomly chosen)
  1369. ... p=1.0
  1370. ... )
  1371. ... ])
  1372. >>>
  1373. >>> strong_result = strong_transform(image=image)
  1374. >>> strong_blur = strong_result["image"]
  1375. >>> # The image will have a strong zoom blur effect, simulating fast zooming
  1376. >>>
  1377. >>> # Example 4: In a pipeline with other transforms
  1378. >>> pipeline = A.Compose([
  1379. ... A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.7),
  1380. ... A.ZoomBlur(max_factor=(1.1, 1.3), step_factor=0.02, p=0.5),
  1381. ... A.HorizontalFlip(p=0.5)
  1382. ... ])
  1383. >>>
  1384. >>> pipeline_result = pipeline(image=image)
  1385. >>> transformed_image = pipeline_result["image"]
  1386. >>> # The image may have zoom blur applied with 50% probability
  1387. Reference:
  1388. Zoom Blur: https://arxiv.org/abs/1903.12261
  1389. """
  1390. class InitSchema(BaseTransformInitSchema):
  1391. max_factor: OnePlusFloatRangeType
  1392. step_factor: NonNegativeFloatRangeType
  1393. def __init__(
  1394. self,
  1395. max_factor: tuple[float, float] | float = (1, 1.31),
  1396. step_factor: tuple[float, float] | float = (0.01, 0.03),
  1397. p: float = 0.5,
  1398. ):
  1399. super().__init__(p=p)
  1400. self.max_factor = cast("tuple[float, float]", max_factor)
  1401. self.step_factor = cast("tuple[float, float]", step_factor)
  1402. def apply(
  1403. self,
  1404. img: np.ndarray,
  1405. zoom_factors: np.ndarray,
  1406. **params: Any,
  1407. ) -> np.ndarray:
  1408. """Apply zoom blur to the input image.
  1409. Args:
  1410. img (np.ndarray): Image to blur.
  1411. zoom_factors (np.ndarray): Array of zoom factors.
  1412. **params (Any): Additional parameters.
  1413. Returns:
  1414. np.ndarray: Zoom blurred image.
  1415. """
  1416. return fblur.zoom_blur(img, zoom_factors)
  1417. def get_params(self) -> dict[str, Any]:
  1418. """Get parameters for the transform.
  1419. Returns:
  1420. dict[str, Any]: Dictionary with parameters.
  1421. """
  1422. step_factor = self.py_random.uniform(*self.step_factor)
  1423. max_factor = max(1 + step_factor, self.py_random.uniform(*self.max_factor))
  1424. return {"zoom_factors": np.arange(1.0, max_factor, step_factor)}