使用Python语言的OpenCV库,在按住鼠标左键的情况下,移动鼠标,绘制任意形状的图形
event判断鼠标是否是移动状态(event == cv.EVENT_MOUSEMOVE),flags判断鼠标是否处于左键按下并拖动的状态(flags == cv.EVENT_FLAG_LBUTTON),当满足这两个条件之后,按住左键并移动鼠标可以实现绘制功能文章来源:https://www.toymoban.com/news/detail-558109.html
双击鼠标左键可以清除画板上的所有图形(event == cv.EVENT_LBUTTONDBLCLK)文章来源地址https://www.toymoban.com/news/detail-558109.html
def drawing(event, x, y, flags, param):
global img
if event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON:
cv.circle(img, (x, y), 5, (0, 0, 255), -1)
elif event == cv.EVENT_LBUTTONDBLCLK:
img = np.zeros(img_size, np.uint8) + 255
cv.imshow('image', img)
完整代码如下:
import numpy as np
import cv2 as cv
def drawing(event, x, y, flags, param):
global img
if event == cv.EVENT_MOUSEMOVE and flags == cv.EVENT_FLAG_LBUTTON:
cv.circle(img, (x, y), 5, (0, 0, 255), -1)
elif event == cv.EVENT_LBUTTONDBLCLK:
img = np.zeros(img_size, np.uint8) + 255
cv.imshow('image', img)
img_size = [400, 500, 3]
img = np.zeros(img_size, np.uint8) + 255
cv.namedWindow('image')
cv.setMouseCallback('image', drawing)
cv.imshow('image', img)
cv.waitKey(0)
cv.destroyAllWindows()
到了这里,关于Python-OpenCV按住鼠标左键绘制图形的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!