-1

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)

3
  • 2
    Please first fix indentation and syntax errors in your code before sharing it here! Commented Jan 20, 2023 at 8:16
  • 1
    I fixed it, sorry. Commented Jan 20, 2023 at 11:44
  • calling waitKey in side of a mouse event handler is an error. Commented Jan 20, 2023 at 13:56

2 Answers 2

0

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.