Connect new point to the previous point on a image with a straight line in Opencv-Python
Last Updated :
03 Jan, 2023
OpenCV-Python is a library of Python bindings designed to solve computer vision problems. Let’s see how to Connect a new point to the previous point on an image with a straight line.
Step 1: Draw points on image:
On a image we can mark points using cv2.circle( ) method. This method is used to draw a circle on any image.
Syntax: cv2.circle(image, center_coordinates, radius, color, thickness)
Parameters: This method will take the following parameters:
- image: It is the image on which circle is to be drawn.
- center_coordinates: It is the center coordinates of the circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
- radius: It is the radius of the circle.
- color: It is the color of the borderline of the circle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
- thickness: It is the thickness of the circle borderline in px. The thickness of -1 px will fill the rectangle shape by the specified color.
Return: It returns an image with a circle drawn on it.
Step 2: Joining two points through a straight line:
We can join two points on an image using the cv2.line() method. This method is used to draw a line on any image.
Syntax: cv2.circle(image, center_coordinates, radius, color, thickness)
Parameters: This method will take the following parameters:
- image: It is the image on which circle is to be drawn.
- center_coordinates: It is the center coordinates of the circle. The coordinates are represented as tuples of two values i.e. (X coordinate value, Y coordinate value).
- radius: It is the radius of the circle.
- color: It is the color of the borderline of the circle to be drawn. For BGR, we pass a tuple. eg: (255, 0, 0) for blue color.
- thickness: It is the thickness of the circle borderline in px. The thickness of -1 px will fill the rectangle shape by the specified color.
Return Value: It returns an image with a line drawn on it.
Below is the implementation:
Python3
import cv2
import numpy as np
def click_event(event, x, y,
flags, params):
if event = = cv2.EVENT_LBUTTONDOWN:
points.append((x,y))
cv2.circle(img,(x,y), 4 ,
( 0 , 255 , 0 ), - 1 )
if len (points) > = 2 :
cv2.line(img, points[ - 1 ], points[ - 2 ],
( 0 , 255 , 255 ), 5 )
cv2.imshow( 'image' , img)
img = np.zeros(( 512 , 512 , 3 ),
np.uint8)
points = []
cv2.imshow( 'image' ,img)
cv2.setMouseCallback( 'image' ,
click_event)
cv2.waitKey( 0 )
cv2.destroyAllWindows()
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...