How to Draw Points in OpenCV Python

You can draw points in OpenCV Python by any of the given methods. I highly recommend you get the “Computer Vision: Models, Learning, and Inference Book” to learn Computer Vision.

How to Draw Points in OpenCV Python
How to Draw Points in OpenCV Python

Method 1

In this method, I will draw a red point on a black image/canvas using a circle function with zero radii.

Step 1

Import OpenCV and NumPy libraries. If OpenCV is not installed in your system then first install it using This Method.

import cv2
#cv2 is used for OpenCV library
import numpy as np 
#numpy for 

Step 2

Now create a black image/canvas using the NumPy library.

image=np.zeros((30,60,3),np.uint8) 
#Black Image

Step 3

Now I will draw a red circle on a black image with zero radii. The center of the circle is (30,10) and thickness is equal to -1 to fill the circle. This code will create a red point on the image.

#Draw a red circle with zero radius and -1 for filled circle
image2 = cv2.circle(image, (30,10), radius=0, color=(0, 0, 255), thickness=-1)

Step 4

To display the image in a specified window use ”imshow” function.

cv2.imshow("Red Point on Black Image",image2)

Step 5

“waitKey(0)” will display a window until any key is pressed. “destroyAllWindows()” will destroy all the windows that we created.

cv2.waitKey(0)
cv2.destroyAllWindows()

Method 2

In this method, I will draw a red point on a black image/canvas using a pixel value.

Step 1

Import OpenCV and NumPy libraries. If OpenCV is not installed in your system then first install it using This Method.

import cv2
#cv2 is used for OpenCV library
import numpy as np 
#numpy for 

Step 2

Now create a black image/canvas using the NumPy library.

image=np.zeros((30,60,3),np.uint8) 
#Black Image

Step 3

Now I will draw a red point on a black image using pixel value. The pixel value [10,30] is the point on the image. The combination for red color is [0,0,255].

#Draw a red point
image[10,30]=[0,0,255]

Step 4

To display the image in a specified window use ”imshow” function.

cv2.imshow("Red Point on Black Image",image2)

Step 5

“waitKey(0)” will display a window until any key is pressed. “destroyAllWindows()” will destroy all the windows that we created.

cv2.waitKey(0)
cv2.destroyAllWindows()

Output

My Recommendations: Master Deep Learning with These Specializations

  1. Deep Learning Specialization
  2. TensorFlow Developer Professional Certificate
  3. TensorFlow: Advanced Techniques

Leave a Comment

Your email address will not be published.