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.5 kB

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

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