Drawing Shapes and putting Text

Drawing and putting text on image



OpenCV also gives feature to add shapes and text to an image 


Fill Image with color

You can fill an image with color as 

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. dummy[:]=0,255,0
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. 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

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. dummy[200:300,300:400]=0,255,0
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()

Filling color







Drawing Rectangle

A rectangle can be drawn on image using cv2.rectangle() method

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.rectangle(dummy,(0,0),(100,100),(255,0,0),thickness=2)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. 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

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.rectangle(dummy,(0,0),(100,100),(255,0,0),thickness=cv2.FILLED)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()


Other examples:-

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.rectangle(dummy,(0,0),(dummy.shape[1]//2,dummy.shape[0]//2),(255,0,0),thickness=-1)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. 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

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.circle(dummy,(dummy.shape[1]//2,dummy.shape[0]//2),40,(255,0,0),thickness=-1)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()










Drawing line:

A line can be drawn using  cv2.line() which contains following

arguments

image,starting point,destination point,color,thickness

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.line(dummy,(0,0),(dummy.shape[1]//2,dummy.shape[0]//2),(255,0,0),thickness=2)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows()

line








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.

  1. import cv2
  2. import numpy as np
  3. dummy=np.zeros((500,500,3),dtype="uint8")
  4. cv2.putText(dummy,"Hello",(dummy.shape[1]//2,dummy.shape[0]//2),cv2.FONT_HERSHEY_TRIPLEX,1.0,(0,250,0),2)
  5. cv2.imshow("Dummy",dummy)
  6. cv2.waitKey(0)
  7. cv2.destroyAllWindows() 


text on image