How to draw line on image in OpenCv Python
Video Explanation:
As we all know a image in nothing a 2d graph in which several pixels are there arranging rows and colum
Note : I am assuming that you image is in the same folder where your python code is saving if you then you need to give proper directory
Image taken as input :
Code:
import cv2
a = cv2.imread('1.jpg')
start= (0,0)
end = (500,500)
cv2.line(a,start,end,(255,0,0),2)
cv2.imshow("line",a)
cv2.waitKey(0)
cv2.destroyAllWindows()
You can also write this code like this
import cv2
a = cv2.imread('1.jpg')
cv2.line(a,(0,0),(500,500),(255,0,0),2)
cv2.imshow("line",a)
cv2.waitKey(0)
cv2.destroyAllWindows()
Let's try to understand how this code work
- Fist we have import cv2 so that we can use opencv in our code
- After we are reading our image by using command cv2.imread and store this image into a variable called a
- Now we need to define a starting point and ending point of a line .Remember that image is nothing just a 2D graph
- Now let's try to understand cv2.line working
cv2.line(image_name, starting_point_of _line ,ending_point_of_line , color_of_the_line ,thickness_of_the_line)
Note: Opencv by default sotre image in BGR format so (255,0,0) means highest value of blue color scale , lowest value of green color scale ,lowest value of red color scale .
Recommend try to play with these value
After that we define thickness that is 2 here 2 means 2 pixels thickness of the line - cv2.imshow("line",a)
here "line" is the window name in which we are going to show our image and a is the name of the image that we want to show - cv2.waitKey(0)
- cv2.destroyAllWindows()
the purpose of cv2.waitKey(0) that after output it will stay on the screen until any keypad is pressed and cv2.destroyAllWindows() make sure to close out window properly
0 Comments
if you are not getting it then ask i am glad to help