简体   繁体   中英

Drawing circle with event buttons in python with cv2

I dont know how to make circle, which decreasing/increasing radius when '+' or '-' is pushing.

I have to:

  • if i push left mouse click - i have to draw a circle / done
  • if i push right click - i have to change color of circle / done
  • if i push '+' --- i have to increase my circle radius by i = 10
  • if i push '-' --- i have to decrease my circle radius by i = 10

Now, i have this code.

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() 

I have to make a program, which increases or decreasing radius on button. I saw this, but its not possible for me to transform it into my program ( mouse events on opencv )

Try this:

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)

Is this what you are looking for?

import cv2
import numpy as np
import random

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


def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img=image, center=(x, y), radius=radius, 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=radius, color=(c1, c2, c3), thickness=2)
        print('x = {}, y = {}, c = {} {} {}'.format(x, y, c1, c2, c3))


cv2.namedWindow('test window')
cv2.setMouseCallback('test window', draw_circle)

while True:
    cv2.imshow('test window', image)

    k = cv2.waitKey(20)

    if k == 27:
        break
    elif k == ord('+'):
        radius += 10
        print('r = {}'.format(radius))
    elif k == ord('-'):
        radius -= 10
        print('r = {}'.format(radius))

cv2.destroyAllWindows()

Screenshot:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM