You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dehaze.py 3.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """
  2. /**
  3. * Copyright 2020 Zhejiang Lab. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * =============================================================
  17. */
  18. Reference:
  19. - [Single Image Haze Removal Using Dark Channel Prior]
  20. (http://kaiminghe.com/publications/cvpr09.pdf) (CVPR 2009)
  21. """
  22. # !/usr/bin/env python
  23. # -*- coding:utf-8 -*-
  24. import cv2
  25. import numpy as np
  26. def guidedFilter(I, p, r, eps):
  27. """The implementation of guide filter
  28. Args:
  29. I: Guide image
  30. p: Input image
  31. r: The radius of filter window
  32. eps: Regularization parameter
  33. Returns:
  34. re: The result of guide filter
  35. """
  36. mean_I = cv2.boxFilter(I, -1, (r, r))
  37. mean_p = cv2.boxFilter(p, -1, (r, r))
  38. mean_Ip = cv2.boxFilter(I * p, -1, (r, r))
  39. cov_Ip = mean_Ip - mean_I * mean_p
  40. mean_II = cv2.boxFilter(I * I, -1, (r, r))
  41. var_I = mean_II - mean_I * mean_I
  42. a = cov_Ip / (var_I + eps)
  43. b = mean_p - a * mean_I
  44. mean_a = cv2.boxFilter(a, -1, (r, r))
  45. mean_b = cv2.boxFilter(b, -1, (r, r))
  46. re = mean_a * I + mean_b
  47. return re
  48. def AtmLight(img, TR, bins=2000):
  49. """Get the global atmospheric light of input image
  50. Args:
  51. img: Input image
  52. TR: The refined atmospheric mask image
  53. bins: The number of equal-width bins in the given range
  54. Returns:
  55. A: The global atmospheric light of input image
  56. """
  57. ht = np.histogram(TR, bins)
  58. d = np.cumsum(ht[0]) / float(TR.size)
  59. try:
  60. lmax = next(y for y in range(len(d) - 1, 0, -1) if d[y] <= 0.999)
  61. except:
  62. lmax = 1
  63. A = np.mean(img, 2)[TR >= ht[1][lmax]].max()
  64. return A
  65. def TransRefine(img, radius, eps, dehaze_ratio, maxTR):
  66. """Get the refined atmospheric mask image
  67. Args:
  68. img: Input image
  69. radius: The radius of filter window for guide filter
  70. eps: The radius of filter window for guide filter
  71. dehaze_ratio: the ratio of dehaze
  72. maxTR: The limitation of the output
  73. Returns:
  74. TR: The refined atmospheric mask image
  75. """
  76. h, w = img.shape[:2]
  77. img = cv2.pyrDown(img, (w // 4, h // 4))
  78. TR = np.min(img, 2)
  79. filter_TR = cv2.erode(TR, np.ones((2 * radius + 1, 2 * radius + 1)))
  80. TR = guidedFilter(TR, filter_TR, radius, eps)
  81. TR = cv2.resize(TR, (w, h))
  82. TR = np.minimum(TR * dehaze_ratio, maxTR)
  83. return TR
  84. def deHaze(
  85. img,
  86. radius=81,
  87. eps=0.001,
  88. dehaze_ratio=0.95,
  89. maxTR=0.80):
  90. re = np.zeros(img.shape)
  91. TR = TransRefine(img, radius, eps, dehaze_ratio, maxTR)
  92. A = AtmLight(img, TR, bins=2000)
  93. for k in range(3):
  94. re[:, :, k] = (img[:, :, k] - TR) / (1 - TR / A)
  95. re = np.clip(re, 0, 1)
  96. return re
  97. def addHaze(img, radius=81, eps=0.001, dehaze_ratio=0.95, maxTR=0.80):
  98. re = np.zeros(img.shape)
  99. TR = TransRefine(img, radius, eps, dehaze_ratio, maxTR)
  100. A = AtmLight(img, TR, bins=2000)
  101. for k in range(3):
  102. re[:, :, k] = (img[:, :, k] * 0.7) + A * 0.3
  103. re = np.clip(re, 0, 1)
  104. return re

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)