我发现this tutorial 对于创建调色板和了解彩虹通常是如何生成的非常有用。我建议通读一遍,了解如何创建任意长度的重复和非重复颜色循环。
javascript 很容易翻译成 OpenCV Python,如下所示。 makeColorGradient 生成指定count 的RGB 颜色列表。其余代码只是对其进行测试并很好地显示渐变。再次参考教程,了解酷梯度类型和参数值。 =)
import math
import cv2
import numpy as np
def makeColorGradient(freq1, freq2, freq3,
phase1, phase2, phase3,
center=128, width=127, count=50):
colors = []
for i in range(count):
red = int(math.sin(freq1*i + phase1) * width + center);
grn = int(math.sin(freq2*i + phase2) * width + center);
blu = int(math.sin(freq3*i + phase3) * width + center);
#document.write( '<font color="' + RGB2Color(red,grn,blu) + '">█</font>');
colors.append((red,grn,blu))
return colors
def main():
freq = 2.4 #non repeating color set
#freq = 0.3 #nice happy rainbow =)
phases = [0,2,4]
count = 67 #number of colors to generate
colors = makeColorGradient(freq,freq,freq,
phases[0],phases[1],phases[2],
count=count)
winname = 'Color Gradient count='+str(count)
cv2.namedWindow(winname)
w = 800
h = 100
canvas = np.zeros((h,w,3),np.uint8)
linspace = np.linspace(0,w,count,endpoint=True)
linspace = map(int, linspace)
for i in range(count-1):
r1 = (linspace[i], 0)
r2 = (linspace[i+1], h)
color = colors[i]
cv2.rectangle(canvas, r1, r2, color, thickness=cv2.cv.CV_FILLED)
cv2.imshow(winname, canvas)
#keep window open till escape key pressed
while(1):
if(cv2.waitKey(15) == 27):
break
if __name__ == '__main__':
main()
print 'done'