Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Created March 22, 2023 13:19
Show Gist options
  • Select an option

  • Save EteimZ/7bd0af05d682789f3cc714785b2692fa to your computer and use it in GitHub Desktop.

Select an option

Save EteimZ/7bd0af05d682789f3cc714785b2692fa to your computer and use it in GitHub Desktop.
Drawing in opencv
import numpy as np
import cv2 as cv
# create a black image
img = np.zeros((512, 512, 3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv.line(img,(0,0),(512,512),(255,0,0),5)
# Draw a green rectangle with thickness of 3 px without fill
cv.rectangle(img,(384,0),(510,128),(0,255,0),3)
# Draw a red circle that has a fill
cv.circle(img,(447,63), 63, (0,0,255), -1)
# Draw an ellipse
cv.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
# Define points for a polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
# Draw the polygon with points
cv.polylines(img,[pts],True,(0,255,255))
# Define the font style
font = cv.FONT_HERSHEY_SIMPLEX
# Draw the text
cv.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv.LINE_AA)
# display image
cv.imshow("black image", img)
k = cv.waitKey(0)
if k == ord('x'):
sys.exit("Exiting image")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment