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.

ACE.py 2.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. """
  19. import cv2
  20. import numpy as np
  21. import math
  22. para = {}
  23. def ACE(img, ratio=4, radius=300):
  24. """The implementation of ACE"""
  25. global para
  26. para_mat = para.get(radius)
  27. if para_mat is not None:
  28. pass
  29. else:
  30. size = radius * 2 + 1
  31. para_mat = np.zeros((size, size))
  32. for h in range(-radius, radius + 1):
  33. for w in range(-radius, radius + 1):
  34. if not h and not w:
  35. continue
  36. para_mat[radius + h, radius + w] = 1.0 / \
  37. math.sqrt(h ** 2 + w ** 2)
  38. para_mat /= para_mat.sum()
  39. para[radius] = para_mat
  40. h, w = img.shape[:2]
  41. p_h, p_w = [0] * radius + list(range(h)) + [h - 1] * radius,\
  42. [0] * radius + list(range(w)) + [w - 1] * radius
  43. temp = img[np.ix_(p_h, p_w)]
  44. res = np.zeros(img.shape)
  45. for i in range(radius * 2 + 1):
  46. for j in range(radius * 2 + 1):
  47. if para_mat[i][j] == 0:
  48. continue
  49. res += (para_mat[i][j] *
  50. np.clip((img - temp[i:i + h, j:j + w]) * ratio, -1, 1))
  51. return res
  52. def ACE_channel(img, ratio, radius):
  53. """The implementation of ACE through individual channel"""
  54. h, w = img.shape[:2]
  55. if min(h, w) <= 2:
  56. return np.zeros(img.shape) + 0.5
  57. down_ori = cv2.pyrDown(img, ((w + 1) // 2, (h + 1) // 2))
  58. temp = ACE_channel(down_ori, ratio, radius)
  59. up_temp = cv2.resize(temp, (w, h))
  60. up_ori = cv2.resize(down_ori, (w, h))
  61. re = up_temp + ACE(img, ratio, radius) - ACE(up_ori, ratio, radius)
  62. return re
  63. def ACE_color(img, ratio=4, radius=3):
  64. """Enhance the image through RGB channels"""
  65. re = np.zeros(img.shape)
  66. for c in range(3):
  67. re[:, :, c] = reprocessImage(ACE_channel(img[:, :, c], ratio, radius))
  68. return re
  69. def reprocessImage(img):
  70. """Reprocess and map the image to [0,1]"""
  71. ht = np.histogram(img, 2000)
  72. d = np.cumsum(ht[0]) / float(img.size)
  73. try:
  74. left = next(x for x in range(len(d)) if d[x] >= 0.005)
  75. except:
  76. left = 1999
  77. try:
  78. right = next(y for y in range(len(d)-1,0,-1) if d[y] <= 0.995)
  79. except:
  80. right = 1
  81. return np.clip((img - ht[1][left]) / (ht[1][right] - ht[1][left]), 0, 1)

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

Contributors (1)