Python OpenCV 用滑动条做调色板
Posted wbyixx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python OpenCV 用滑动条做调色板相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*- import cv2 import numpy as np def 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) switch = ‘0:0FF 1:0N‘ cv2.createTrackbar(switch, ‘image‘, 0, 1, nothing) while True: cv2.imshow(‘image‘, img) k = cv2.waitKey(1) & 0xff if k == 27: break r = cv2.getTrackbarPos(‘R‘, ‘image‘) g = cv2.getTrackbarPos(‘G‘, ‘image‘) b = cv2.getTrackbarPos(‘B‘, ‘image‘) s = cv2.getTrackbarPos(switch, ‘image‘) if s == 0: img[:] = 0 else: img[:] = [b, g, r] cv2.destroyAllWindows()
利用调色板的颜色绘制
# -*- coding: utf-8 -*- import cv2 import numpy as np #当鼠标按下时变为 True drawing = False #如果mode为True绘制矩形, 按下m变成绘制曲线 mode = True ix, iy = -1, -1 def nothing(x): pass #创建回调函数 def draw(event, x, y, flags, param): r = cv2.getTrackbarPos(‘R‘, ‘image‘) g = cv2.getTrackbarPos(‘G‘, ‘image‘) b = cv2.getTrackbarPos(‘B‘, ‘image‘) color = (b, g, r) global ix, iy, drawing, mode #当按下左键时返回起始点坐标 if event == cv2.EVENT_LBUTTONDOWN: drawing = True ix, iy = x, y #当鼠标按下并移动时绘制图形,可以查看移动,flag是否按下 elif event == cv2.EVENT_MOUSEMOVE and flags == cv2.EVENT_FLAG_LBUTTON: if drawing == True: if mode == True: cv2.rectangle(img, (ix, iy), (x, y), color, -1) else: cv2.circle(img, (x, y), 3, color, -1) #当鼠标松开时停止绘画 elif event == cv2.EVENT_LBUTTONUP: drawing = False img = np.zeros((512, 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.setMouseCallback(‘image‘, draw) while True: cv2.imshow(‘image‘, img) k = cv2.waitKey(1) & 0xff if k == ord(‘m‘): mode = not mode elif k == 27: break
以上是关于Python OpenCV 用滑动条做调色板的主要内容,如果未能解决你的问题,请参考以下文章