Drawing Shapes and putting Text
OpenCV also gives feature to add shapes and text to an image
Fill Image with color
You can fill an image with color as
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- dummy[:]=0,255,0
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
3 line creates a dummy image using numpy,zeros,’uint8’ is datatype of
image
4 line fills all pixels in dummy image with green color
You can also fill certain portion of image with color by just mentioning
the range of pixels in line 4
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- dummy[200:300,300:400]=0,255,0
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Drawing Rectangle
A rectangle can be drawn on image using cv2.rectangle() method
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.rectangle(dummy,(0,0),(100,100),(255,0,0),thickness=2)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
cv2.rectangle contains the first argument is image,
second is location of top left corner
and
third is location of bottom right corner,
fourth is color of boundaries of rectangle
and finally thickness of boundaries.
You can also fill the rectangle as following using thickness with cv2.FILLED or by thickness=-1
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.rectangle(dummy,(0,0),(100,100),(255,0,0),thickness=cv2.FILLED)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Other examples:-
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.rectangle(dummy,(0,0),(dummy.shape[1]//2,dummy.shape[0]//2),(255,0,0),thickness=-1)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Drawing Circle
A circle can be drawn using cv2.circle() whose arguments are image ,
center,
radius of circle,color of circle,thickness of border of circle
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.circle(dummy,(dummy.shape[1]//2,dummy.shape[0]//2),40,(255,0,0),thickness=-1)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Drawing line:
A line can be drawn using cv2.line() which contains following
arguments
image,starting point,destination point,color,thickness
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.line(dummy,(0,0),(dummy.shape[1]//2,dummy.shape[0]//2),(255,0,0),thickness=2)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Put Text in Image:
Text can be inserted using cv2.putText() which contains the following
arguments: image,Text to be inserted,place to be inserted,font name,
scaling factor,colour,thickness.
- import cv2
- import numpy as np
- dummy=np.zeros((500,500,3),dtype="uint8")
- cv2.putText(dummy,"Hello",(dummy.shape[1]//2,dummy.shape[0]//2),cv2.FONT_HERSHEY_TRIPLEX,1.0,(0,250,0),2)
- cv2.imshow("Dummy",dummy)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
0 Comments
Post a Comment