使用Python,Opencv绘制调色板及圆形来模拟霓虹的渐变效果
1. 效果图2. 源码参考这篇博客将使用python,opencv绘制调色板及圆形来模拟霓虹的渐变效果。
1. 效果图
可以构造霓虹的渐变效果呦,选择不同的圆心,视觉上的震撼感也不一样~
2. 源码
# 构建霓虹渐变效果# USAGE# python trackbar_as_color_palette.pyimport cv2import numpy as npdef nothing(x):pass# 构建一个黑色背景图,一个窗口img = np.zeros((300, 512, 3), np.uint8)cv2.namedWindow('image')# 构建颜色渐变轨迹栏cv2.createTrackbar('R', 'image', 0, 255, nothing)cv2.createTrackbar('G', 'image', 0, 255, nothing)cv2.createTrackbar('B', 'image', 0, 255, nothing)# 构建半径大小渐变栏cv2.createTrackbar('radius', 'image', 0, 100, nothing)# 构建切换开关,1开始绘制switch = '0 : OFF \n1 : ON'cv2.createTrackbar(switch, 'image', 0, 1, nothing)while (1):cv2.imshow('image', img)k = cv2.waitKey(1) & 0xFF# 按下ESC键退出if k == 27:break# 获取当前5个轨迹栏的值r = cv2.getTrackbarPos('R', 'image')g = cv2.getTrackbarPos('G', 'image')b = cv2.getTrackbarPos('B', 'image')radius = cv2.getTrackbarPos("radius", 'image')s = cv2.getTrackbarPos(switch, 'image')# 随机圆心pts = tuple(np.random.randint(0, high=512, size=(2,)))# print(pts, type(pts))if s == 0:img[:] = 0else:cv2.circle(img, pts, radius, (b, g, r), -1)cv2.destroyAllWindows()