Goal

- Learn to draw different geometric shapes with OpenCV
- You will learn these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse() etc.
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)
Running the program you will see the blue line.

————–
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), 3) #Display the image cv2.imshow("img",img) cv2.waitKey(0)
Running the program you will see red color circle

—————
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)
Running the program you will see ellipse

—————-
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)
Running the program you will see rectangle

——————–
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)
Running the program you will see polygon

No comments:
Post a Comment