Skip to main content

OpenCV Drawing a Line

How To Draw a Line In OpenCV Python?

To draw a line, we need to select the starting and ending coordinates of the line. First, let's create a black image and draw a green line. We need to use the code below to create a black image:

image = np.zeros((360,360,3), np.uint8)

Now we can create our line.

cv2.line(image,(0,0),(511,511),(0,255,0),5)

The code for creating a line is as follows:

import numpy as np
import cv2

image = np.zeros((360,360,3), np.uint8)

cv2.line(image,(0,0),(511,511),(0,255,0),5)

cv2.imshow("image",image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:


Another Example

import numpy as np
import cv2

image = np.zeros((360,360,3), np.uint8)

cv2.line(image,(0,180),(360,180),(0,255,0),5)

cv2.imshow("image",image)

cv2.waitKey(0)
cv2.destroyAllWindows()

Output:



Comments

Popular posts from this blog

OpenCV Add Border to Image in Python

How to add border to image in python? To create a frame around the image, the function cv2.copyMakeBorder () is used. This function takes the following arguments: src - login image top, bottom, left, right - edge width in number of pixels in corresponding directions borderType - defines what type of border to add. There may be the following types cv2.BORDER_CONSTANT  cv2.BORDER_REFLECT - cv2.BORDER_REFLECT_101 cv2.BORDER_REPLICATE cv2.BORDER_WRAP  value  - Color of border if border type is cv2.BORDER_CONSTANT import cv2 import numpy as np from matplotlib import pyplot as plt BLUE = [ 255 , 0 , 0 ] img1 = cv2.imread( 'flower.jpg' ) replicate = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REPLICATE) reflect = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT) reflect101 = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_REFLECT_101) wrap = cv2.copyMakeBorder(img1 , 10 , 10 , 10 , 10 , cv2.BORDER_WRAP) constant= cv2.copyMakeBorder(img1 ...