gpt4 book ai didi

python - 在 OpenCV 中使用 createTrackbar() 方法制作绘画应用程序时,EVENT_MOUSEMOVE 不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 22:44:32 24 4
gpt4 key购买 nike

我一直在尝试使用 OpenCV 和 Python 制作一个简单的 Paint 应用程序。

这里我使用了 trackbar 方法创建了五个 adjustable fields(三个 (R, G, B) fields,一个 On/Off switch 来启用或不启用绘图,一个 field 来绘制一圈可调半径)。

目前,正在读取所有字段数据并将其提供给适当的函数。我希望我的画图应用程序具有可调节的背景颜色,并在鼠标悬停的任何地方制作圆圈,半径和颜色均可调节。

但是当鼠标悬停在图像上时,我的代码没有画一个圆圈。

import cv2
import numpy as np
def nothing(x):
pass

colo = (0,0,255)
radius = 5

def draw_circle(event,x,y,flags,param):
global colo,radius
if event == cv2.EVENT_MOUSEMOVE:
# print(str(colo) + " " + str(radius))
cv2.circle(img,(x,y),radius,colo,-1)

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

# create trackbars for color and radius change
cv2.createTrackbar('Red','image',0,255,nothing)
cv2.createTrackbar('Green','image',0,255,nothing)
cv2.createTrackbar('Blue','image',0,255,nothing)
cv2.createTrackbar('CirRadius','image',5,50,nothing)

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break

# get current positions of five trackbars
r = cv2.getTrackbarPos('Red','image')
g = cv2.getTrackbarPos('Green','image')
b = cv2.getTrackbarPos('Blue','image')
s = cv2.getTrackbarPos(switch,'image')
radius = cv2.getTrackbarPos('CirRadius','image')
colo = (b,g,r)

if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]
cv2.destroyAllWindows()

enter image description here

最佳答案

如果圆圈和背景的颜色保持不变,我们无法区分或定位圆圈。由于您使用的是 global colo,因此它不可见。

import cv2
import numpy as np
def nothing(x):
pass

colo = (0,0,255)
radius = 5

def draw_circle(event,x,y,flags,param):
global colo,radius
if event == cv2.EVENT_MOUSEMOVE:
# print(str(colo) + " " + str(radius))
cv2.circle(img,(x,y),radius,(0,0,255),-1)

# Create a black image, a window
img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')
cv2.setMouseCallback('image',draw_circle)

# create trackbars for color and radius change
cv2.createTrackbar('Red','image',0,255,nothing)
cv2.createTrackbar('Green','image',0,255,nothing)
cv2.createTrackbar('Blue','image',0,255,nothing)
cv2.createTrackbar('CirRadius','image',5,50,nothing)

# create switch for ON/OFF functionality
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)

while(1):
cv2.imshow('image',img)
# get current positions of five trackbars
r = cv2.getTrackbarPos('Red','image')
g = cv2.getTrackbarPos('Green','image')
b = cv2.getTrackbarPos('Blue','image')
s = cv2.getTrackbarPos(switch,'image')
radius = cv2.getTrackbarPos('CirRadius','image')
colo = (b,g,r)

if s == 0:
img[:] = 0
else:
img[:] = [b,g,r]

k = cv2.waitKey(1) & 0xFF
if k == 27:
break


cv2.destroyAllWindows()

关于python - 在 OpenCV 中使用 createTrackbar() 方法制作绘画应用程序时,EVENT_MOUSEMOVE 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com