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 Crop an image in Python

 Crop operation in OpenCV is very simple. You should first read the image. Then the values x, y, h, w must be determined to specify the region to be cropped. You can do this using the code below. import cv2 image = cv2.imread( 'image.jpg' ) y= 10 x= 20 h= 200 w= 250 crop = image[y:y+h , x:x+w] cv2.imshow( 'Image' , crop) cv2.waitKey( 0 )

Opencv Image Properties - rows, columns and channels, type of image data, number of pixels

Opencv Image Properties  OpenCV allows access to image properties. These properties are rows, columns and channels, type of image data, number of pixels. The img.shape command is used to access the shape of the image. Returns rows, columns and channels. import  cv2 img = cv2.imread( 'flower.jpg' ) print (img.shape) Result: (400, 400, 3) The data type of the image is obtained with img.dtype import cv2 img = cv2.imread( 'flower.jpg' ) print (img.dtype) Result: uint8