This script is a simple example of how to create a basic drawing program using the OpenCV library. Such drawing programs can be used in various real-world applications, such as computer-aided design (CAD) software, image annotation and labeling tools, and whiteboard applications for online education and collaboration.
This Python script uses the OpenCV library to create a simple drawing program that allows the user to draw lines on an image using the mouse. The script sets up a mouse event handler, which listens for mouse clicks, mouse movement, and mouse button releases.:
- OpenCV library is a popular computer vision library that provides various image and video processing functions.
- NumPy is a numerical computing library that supports working with large, multi-dimensional arrays and matrices.
The draw_shape function is responsible for drawing the lines on the image based on the user's mouse actions, such as mouse clicks, mouse movement, and mouse button releases. The function updates the global variables based on the mouse events and draws lines on the image using OpenCV's cv2.line function.
The function takes five arguments:
- event: An integer value representing the mouse event type, such as cv2.EVENT_LBUTTONDOWN for a left mouse button click event.
- x: An integer value that represents the x-coordinate of the mouse position relative to the image.
- y: An integer value that represents the y-coordinate of the mouse position relative to the image.
- flag: An integer value that provides additional information about the mouse event, such as whether the mouse buttons were pressed or released.
- param: An optional parameter that can pass additional user-defined data to the event handler.
- cv2.setMouseCallback is a function in the OpenCV library that allows the user to associate a mouse event handler function with a particular window.
This script provides a starting point for building more complex drawing tools with more advanced features, such as the ability to draw different shapes, fill areas with color, use different colors and line styles, and save the drawing to a file.
Here is an example of how to draw a line using the mouse in Opencv:
Preview of the output that you will get on running this code from your IDE
In this solution we use the Imread function of the OpenCV.
#!/usr/bin/python3 # 2018/05/14 19:17:13 import cv2 import numpy as np drawing = False x2,y2 = -1,-1 def draw_shape(event,x,y,flag,parm): global x2,y2,drawing, img, img2 if event == cv2.EVENT_LBUTTONDOWN: print('Cliked: ', (x,y)) drawing = True img2 = img.copy() x2,y2 = x,y cv2.line(img,(x2,y2),(x,y),(0,0,255),1, cv2.LINE_AA) elif event == cv2.EVENT_MOUSEMOVE: if drawing == True: print('Moving: ',(x,y)) a, b = x, y if a != x & b != y: img = img2.copy() cv2.line(img,(x2,y2),(x,y),(0,255,0),1, cv2.LINE_AA) elif event == cv2.EVENT_LBUTTONUP: drawing = False print('Released: ',(x,y)) img = img2.copy() cv2.line(img,(x2,y2),(x,y),(0,0,255),1, cv2.LINE_AA) img = np.zeros((512,512,3), np.uint8) img2 = img.copy() cv2.namedWindow('Draw') cv2.setMouseCallback('Draw',draw_shape) while(True): cv2.imshow('Draw',img) k = cv2.waitKey(1) & 0xFF if k == 27: break cv2.destroyAllWindows()
- Copy the code using the "Copy" button above, and paste it in a Python file in your IDE.
- Import open Cv library and Numpy library.
- Modify the name, location of the image in the code.
- Run the file to draw a line.
I hope you found this useful. I have added the link to dependent libraries, version information in the following sections.
i found this code snippet by searching for "how to draw pefect line in python using open Cv" in kandi. You can try any such use case!
I tested this solution in the following versions. Be mindful of changes when working with other versions
- The solution is created and executed in python version 3.7.15 .
- The solution is tested on OpenCV 4.6.0
- The solution is tested on numpy 1.21.6
Using this solution, we are going to draw a perfect line using the OpenCv library in Python with simple steps. This process also facilities an easy to use, hassle free method to create a hands-on working version of code which would help us draw a image in Python