渐变色可用于画热度图、分布图等。渐变色示例:
import cv2
import numpy as np
def gradient_ramp(num):
def arithmetic_progression(vec_1, vec_2, num):
series = []
for beg, end in zip(vec_1, vec_2):
series.append(np.linspace(beg, end, num))
return np.stack(series, axis=1)
# red -> yellow
part_1 = arithmetic_progression([0, 0, 255], [0, 255, 255], 256)
# yellow -> green
part_2 = arithmetic_progression([0, 255, 255], [0, 255, 0], 256)
# green -> cyan
part_3 = arithmetic_progression([0, 255, 0], [255, 255, 0], 256)
# cyan -> blue
part_4 = arithmetic_progression([255, 255, 0], [255, 0, 0], 256)
ribbon = np.vstack([part_1, part_2[1:], part_3[1:], part_4[1:]])
ribbon = ribbon.astype(np.uint8)
total = ribbon.shape[0]
indices = np.linspace(0, total - 1, num, dtype=np.int)
return ribbon[indices]
if __name__ == '__main__':
num = 512
colors = gradient_ramp(num) # color bar
canvas = np.zeros((64, num, 3), dtype=np.uint8)
for idx, color in enumerate(colors):
canvas[:, idx, :] = color
cv2.namedWindow('debug')
cv2.imshow('debug', canvas)
cv2.waitKey()