Basic Drawing Examples
Drawing a line:
cv2.line(img, Point pt1, Point pt2, color[,thickness[,lineType[,shift]]]) --> img
Parameters: |
|
---|
Example 1: Drawing a line:
--------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a blue line with thickness of 5 px cv2.line(img,(15,20),(70,50),(255,0,0),5) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
--------------
Drawing a Circle:
cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → img
Parameters: |
|
---|
Example 2: Drawing a Circle:
---------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a red closed circle cv2.circle(img,(200,200), 40, (0,0,255), -1) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
---------------
Drawing an Ellipse:
cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → img
cv2.ellipse(img, box, color[, thickness[, lineType]]) → img
Parameters: |
|
---|
Example 3: Drawing an Ellipse:
----------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw ellipse cv2.ellipse(img,(200,200),(80,50),0,0,360,(0,255,0),-1) cv2.ellipse(img,(200,200),(80,50),45,0,360,(0,0,255),1) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
----------------
Drawing a Rectangle:
cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → img
Parameters: |
|
---|
Example 4: Drawing a Rectangle:
--------------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw rectangle(not filled) cv2.rectangle(img,(15,20),(70,50),(0,255,0),3) # Draw filled rectangle cv2.rectangle(img,(115,120),(170,150),(255,0,0),-1) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
--------------------
Drawing a Polygon:
cv2.polylines(img, pts, isClosed, color[, thickness[, lineType[, shift]]]) → img
Parameters: |
|
---|
Example 4: Drawing a Polygon:
-----------------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Draw a polygon pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32) pts = pts.reshape((-1,1,2)) cv2.polylines(img,[pts],True,(0,255,255)) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
-----------------------
Putting Text in image:
cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]]) → None
Parameters: |
|
---|
Example 5: Putting Text in image:
--------------------
import numpy as np import cv2 # Create a black image img = np.zeros((512,512,3), np.uint8) # Write some Text font = cv2.FONT_HERSHEY_SIMPLEX cv2.putText(img,'Hello World!',(10,500), font, 1,(255,255,255),2) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
---------------------