Hello Guys,

In this tutorial, I will show you how to draw a line in OpenCV.

OpenCV comes with lots of built-in functions that make our life easier especially when we are working on image processing

And there is a function called line in OpenCV which is used to draw a line.

This function takes the following parameters:

1. Image: Takes image object
2. Point 1: Point 1 X and Y  Coordinates
3. Point 2: Point 2 X and Y  Coordinates
4. Color: Takes color in BGR format
5. Thickness: By default set to 1 (optional)
6. Line Type: By default set to 8-connected.It can also be LINE_AA or 4-connected (Optional)
7. Shift: Shifts fractional bits in the point coordinates (Optional)

Well it is recommended to use line LINE_AA (
Anti Aliased) as line type.

line without AA

Line Without LINE_AA  (Anti Aliased)

You can observe the right and left ends of the line . You will notice curves are showing in a zigzag pattern. 

Line With AA

Line With LINE_AA  (Anti Aliased)

Now in this image you can observe the curves are smooth

Program

#import cv2 module
import cv2 as cv

img = cv.imread("nature.jpg")

height,width= img.shape[0:2]

#creating horizontal line at the center of image
# Show curve in zigzag pattern

img = cv.line(img,(100,height/2),(800,height/2),(255,144,100),50)

#To avoid zigzag patten around curves of line.
#LINE_AA - Stands for Line Antialiased (Creates smooth curve)

"""img = cv.line(img,(100,height/2),(800,height/2), (255,144,100),50,cv.LINE_AA)"""

cv.imshow('Line',img)
cv.waitKey(0)
cv.destroyAllWindows()

Thanks for reading 🙂 🙂

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments