自己写的一个回调函数无法实现rgb颜色调整,希望附上过程,必给采纳

在函数nothing的开头定义一下全局变量,加上:
global brush_color, brush_color, drawing
改成这样:
import cv2
import numpy as np
def nothing(x):
global brush_color, brush_color, drawing
cv2.imshow('painting', img)
r = cv2.getTrackbarPos('r', 'painting')
g = cv2.getTrackbarPos('g', 'painting')
b = cv2.getTrackbarPos('b', 'painting')
brush_color = (b, g, r)
brush_size = cv2.getTrackbarPos('brush size', 'painting')
def mouse_event(event, x, y, flags, param):
'''
### 鼠标回调函数
'''
global brush_size, brush_color, drawing
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
cv2.circle(img, (x, y), brush_size, brush_color, -1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
cv2.circle(img, (x, y), brush_size, brush_color, -1)
img = np.zeros((300, 512, 3), np.uint8)
img[:] = (255, 255, 255) # 定义画板为白色
cv2.namedWindow('painting')
# 定义默认的笔刷尺寸和颜色
brush_size, brush_color = 8, (0, 0, 0)
drawing = False
# 创建rgb三个滑动条
cv2.createTrackbar('r', 'painting', 0, 255, nothing)
cv2.createTrackbar('g', 'painting', 0, 255, nothing)
cv2.createTrackbar('b', 'painting', 0, 255, nothing)
# 创建笔刷大小滑动条
cv2.createTrackbar('brush size', 'painting', 8, 15, nothing)
# 定义鼠标回调函数
cv2.setMouseCallback('painting', mouse_event)
cv2.waitKey()
cv2.destroyAllWindows()

如有帮助,请点采纳。