Draw Geometric Shapes using OpenCV

Introduction

In the previous tutorial, we have seen the OpenCV polygons drawing and writing of the text. Similar to that in this tutorial, we shall look to draw geometric shapes using opencv by the mouse pointer in the work area.

Let us perform our code writing and execution using the Jupyter notebook. You can write the code as the python script or in the single cell of the Jupyter notebook and execute it. In this tutorial, we shall look into the drawing of the circle using the mouse pointer. To perform this operation the concept of call back is used. The different functions are being called by the open CV to display the images accordingly.

Initially you need to import the required libraries both CV2 and Numpy.

import cv2
import numpy as np

Now, you need to mention the function with function definition and the arguments.

Arguments

  • Event – It tells the activity of the mouse
  • X and Y – These are the coordinates considered as the center of the circle.
  • Flags and Params are the certain parameters required to perform the action that are received from the CV2.
  • Inside the function different events are considered with the else and if conditions.

 The events when the left button and the right button are clicked will draw the circle with a radius of 100 units and the coordinates as x and y on the plane with the green and blue colours respectively. The name of the window is my_drawing and the call back to draw the circle is declared.

def draw_circle(event,x,y,flags,param):
    if event ==cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img,(x,y),100,(0,255,0),-1)
    elif event==cv2.EVENT_RBUTTONDOWN:
        cv2.circle(img,(x,y),100,(255,0,0),-1)
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing',draw_circle)

In the main function, the image is declared with the units and it is given the colour coding of black by storing with the entire array with zeros.

img=np.zeros((512,512,3),np.int8)

The image is displayed using the cv2.imshow command and the waitkey is declared to close the window when the escape key is clicked.

img=np.zeros((512,512,3),np.int8)
while True:
        cv2.imshow('my_drawing',img)
        
        if cv2.waitKey(20) & 0xFF == 27:
            break
            
cv2.destroyAllWindows()

Code

import cv2
import numpy as np

#Function

def draw_circle(event,x,y,flags,param):
    if event ==cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img,(x,y),100,(0,255,0),-1)
    elif event==cv2.EVENT_RBUTTONDOWN:
        cv2.circle(img,(x,y),100,(255,0,0),-1)
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing',draw_circle)

#Show image using open cv

img=np.zeros((512,512,3),np.int8)
while True:
        cv2.imshow('my_drawing',img)
        
        if cv2.waitKey(20) & 0xFF == 27:
            break
            
cv2.destroyAllWindows()

Output

After the successful execution of the code you will be obtained with the window, where you can draw the circles. To close the window click the escape key.

opencv

Similar to this you can perform the below code with inclusion of few more events. You can draw the rectangles continuously on the plane with this code.

Code

import cv2
import numpy as np
#variables

drawing = False
ix=-1
iy=-1

#function
def draw_rectangle(event,x,y,flags,params):
    global ix,iy,drawing
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing=True
        ix,iy = x,y
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing ==True:
            cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
    elif event==cv2.EVENT_LBUTTONUP:
        drawing=False
        cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
        
#section showing image
#image
img = np.zeros((512,512,3))
cv2.namedWindow(winname='my_drawing')
cv2.setMouseCallback('my_drawing',draw_rectangle)

while True:
    cv2.imshow('my_drawing',img)
    if cv2.waitKey(1) & 0xFF ==27:
        break
cv2.destroyAllWindpws()
    

Output

The output of the execution of code will result in the similar image below, when the rectangle is drawn.

image processing

So, this is all about the simple way of performing the drawing of the circles, using the mouse pointer and for any sort of doubts and the questions in the execution of this code you can reach us through the comment box.

Related Tutorials

OpenCV Image Basics

Drawing Polygons and Writing text on the images

Spread knowledge

Leave a Comment

Your email address will not be published.