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 3.0 kB

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

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