color_map.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  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. import numpy as np
  15. def get_colormap(rgb=False):
  16. """
  17. Get colormap
  18. """
  19. color_list = np.array(
  20. [
  21. 0xFF,
  22. 0x00,
  23. 0x00,
  24. 0xCC,
  25. 0xFF,
  26. 0x00,
  27. 0x00,
  28. 0xFF,
  29. 0x66,
  30. 0x00,
  31. 0x66,
  32. 0xFF,
  33. 0xCC,
  34. 0x00,
  35. 0xFF,
  36. 0xFF,
  37. 0x4D,
  38. 0x00,
  39. 0x80,
  40. 0xFF,
  41. 0x00,
  42. 0x00,
  43. 0xFF,
  44. 0xB2,
  45. 0x00,
  46. 0x1A,
  47. 0xFF,
  48. 0xFF,
  49. 0x00,
  50. 0xE5,
  51. 0xFF,
  52. 0x99,
  53. 0x00,
  54. 0x33,
  55. 0xFF,
  56. 0x00,
  57. 0x00,
  58. 0xFF,
  59. 0xFF,
  60. 0x33,
  61. 0x00,
  62. 0xFF,
  63. 0xFF,
  64. 0x00,
  65. 0x99,
  66. 0xFF,
  67. 0xE5,
  68. 0x00,
  69. 0x00,
  70. 0xFF,
  71. 0x1A,
  72. 0x00,
  73. 0xB2,
  74. 0xFF,
  75. 0x80,
  76. 0x00,
  77. 0xFF,
  78. 0xFF,
  79. 0x00,
  80. 0x4D,
  81. ]
  82. ).astype(np.float32)
  83. color_list = color_list.reshape((-1, 3))
  84. if not rgb:
  85. color_list = color_list[:, ::-1]
  86. return color_list.astype("int32")
  87. def get_color_map_list(num_classes):
  88. """
  89. Args:
  90. num_classes (int): number of class
  91. Returns:
  92. color_map (list): RGB color list
  93. """
  94. color_map = num_classes * [0, 0, 0]
  95. for i in range(0, num_classes):
  96. j = 0
  97. lab = i
  98. while lab:
  99. color_map[i * 3] |= ((lab >> 0) & 1) << (7 - j)
  100. color_map[i * 3 + 1] |= ((lab >> 1) & 1) << (7 - j)
  101. color_map[i * 3 + 2] |= ((lab >> 2) & 1) << (7 - j)
  102. j += 1
  103. lab >>= 3
  104. color_map = [color_map[i : i + 3] for i in range(0, len(color_map), 3)]
  105. return color_map
  106. def font_colormap(color_index):
  107. """
  108. Get font color according to the index of colormap
  109. """
  110. dark = np.array([0x14, 0x0E, 0x35])
  111. light = np.array([0xFF, 0xFF, 0xFF])
  112. light_indexs = [0, 3, 4, 8, 9, 13, 14, 18, 19]
  113. if color_index in light_indexs:
  114. return light.astype("int32")
  115. else:
  116. return dark.astype("int32")