opencv 用cv2在python中用事件按钮画圆

rvpgvaaj  于 2023-01-21  发布在  Python
关注(0)|答案(1)|浏览(172)

我不知道如何画圆,当“+”或“-”在推的时候,半径会减小/增大。
我必须:
如果我按鼠标左键-我必须画一个圆/完成
如果我按下右键-我必须改变圆圈的颜色/完成
如果我按“+”---我必须将圆半径增加i = 10
如果我按“-”---我必须将圆半径减小i = 10
现在,我有这个代码。

import cv2
import numpy as np
import random


k = cv2.waitKey(10) & 0xff
def draw_circle(event, x, y, flags, param):
    k1 = cv2.waitKey(10) & 0xff 
    # global radius # just trying idk

    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img = image, center = (x,y), radius = 50 , 
        color = (255,0,0), thickness = 2)
        print('x = {}, y = {}'.format(x,y))

    elif event == cv2.EVENT_RBUTTONDOWN:
        c1 = random.randint(0, 255)
        c2 = random.randint(0, 255)
        c3 = random.randint(0, 255)

        cv2.circle(img = image, center = (x,y), radius = 100, 
        color = (c1, c2, c3), thickness = 2)





image = np.zeros((600,600,3), dtype = np.uint8)

cv2.namedWindow(winname = 'testwindow')
cv2.setMouseCallback('testwindow',draw_circle)


while True:
    cv2.imshow('testwindow',image)
    if k == 32:
         print('Something') 
    k = cv2.waitKey(1)
    if k == 27:
        break

cv2.destroyAllWindows()

我必须做一个程序,它增加或减少按钮的半径。我看到了这个,但它不可能为我把它转换成我的程序(mouse events on opencv

ni65a41a

ni65a41a1#

试试这个:

def mouse_callback(self, event, x, y, flags, params):
    if event == cv2.EVENT_LBUTTONDOWN:
        self.mouse_pressed = True

    # mouse pointer has moved over the window
    elif event == cv2.EVENT_MOUSEMOVE:
        if self.mouse_pressed:
            cv2.circle(img=self.img, center=(x, y), radius=20, color=self.char_color, thickness=-1)

    # left mouse button is released
    elif event == cv2.EVENT_LBUTTONUP:
        self.mouse_pressed = False
        cv2.circle(img=self.img, center=(x, y), radius=20, color=self.char_color, thickness=-1)

相关问题