Updated April 19, 2023
Introduction to OpenCV Line
Different geometric shapes like line, circle, etc. needs to be drawn on an image in cases when we are trying to solve the computer vision problems and in order to draw a line on a given image, we make use of a function called line() function in OpenCV using which a line beginning from a given starting point and ending at a given end point of required thickness and required color can be drawn on a given image and the image with a line drawn on it as per the specifications is returned as the output by making use of line() function.
The syntax to define line() function in OpenCV is as follows:
line(image, starting_point, ending_point, color, thickness)
where image is the image on which the line must be drawn,
- starting_point is the x coordinate and y coordinate from which the line should begin,
- ending_point is the x coordinate and y coordinate at which the line should end,
- color represents the color of the line to be drawn on the given image and
- thickness is the thickness of the line to be drawn on the given image.
Working of line() Function in OpenCV
Working of line() function in OpenCV is as follows:
- Different geometric shapes like line, circle, etc. need to be drawn on an image in cases when we are trying to solve the computer vision problems.
- In order to be able to draw a line on a given image, we make use of a function called line() function in OpenCV.
- The line() function takes five parameters namely image, starting_point, ending_point, color, and thickness.
- The parameter image is the image on which the line must be drawn.
- The parameter starting_point is the x coordinate and y coordinate from which the line should begin.
- The parameter ending_point is the x coordinate and y coordinate at which the line should end.
- The parameter color represents the color of the line to be drawn on the given image.
- The parameter thickness is the thickness of the line to be drawn on the given image.
- The line() function in OpenCV returns the image with a line drawn on it as per the specifications as the output.
Example #1
OpenCV program in python to demonstrate line() function to read an image using imread() function and then draw a line on the given image from the specified starting point, ending point having the specified color and thickness using line() function and then display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which a line is to be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
#using line() function to draw a line on the given image
imageline = cv2.line(imageread, (5, 5), (500, 500), (255, 0, 0), 2)
#displaying the image on which the line is drawn as the output on the screen
cv2.imshow('Tree',imageline)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which a line is to be drawn using imread() function. Then we making use of line() function by specifying the image name, starting point, ending point, color and thickness as the parameters to draw a line on the given image which is then displayed as the output on the screen.
Example #2
OpenCV program in python to demonstrate line() function to read an image using imread() function and then draw a line on the given image from the specified starting point, ending point having the specified color and thickness using line() function and then display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which a line is to be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
#using line() function to draw a line on the given image
imageline = cv2.line(imageread, (5, 5), (3000, 3000), (0, 255, 0), 2)
#displaying the image on which the line is drawn as the output on the screen
cv2.imshow('Aeroplane',imageline)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which a line is to be drawn using imread() function. Then we making use of line() function by specifying the image name, starting point, ending point, color, and thickness as the parameters to draw a line on the given image which is then displayed as the output on the screen.
Example #3
OpenCV program in python to demonstrate line() function to read an image using imread() function and then draw a line on the given image from the specified starting point, ending point having the specified color and thickness using line() function and then display the image as the output on the screen:
#importing the module cv2
import cv2
#reading the image from a given path on which a line is to be drawn
imageread = cv2.imread('C:/Users/admin/Desktop/educba.jpg')
#using line() function to draw a line on the given image
imageline = cv2.line(imageread, (5, 5), (10000, 5000), (0, 255, 0), 2)
#displaying the image on which the line is drawn as the output on the screen
cv2.imshow('EDUCBA',imageline)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, we are importing the module cv2. Then we are reading the image on which a line is to be drawn using imread() function. Then we making use of line() function by specifying the image name, starting point, ending point, color, and thickness as the parameters to draw a line on the given image which is then displayed as the output on the screen.
Conclusion
In this article, we have learned the concept of line() function in OpenCV through definition, syntax, and working of line() function in OpenCV with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV Line” was beneficial to you. You can view EDUCBA’s recommended articles for more information.