Drawing a Rectangle on Image in Python using OpenCV

In [2]:
import cv2
img = cv2.imread('machinelearning.png',1)
cv2.imshow('Machine Learning',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Drawing a Rectangle

rectangle() function take 5 Arguments

  • Image
  • Top-left corner co-ordinates
  • Bottom-right corner co-ordinates
  • Color (in BGR format)
  • Line width
In [3]:
import cv2
img = cv2.imread('machinelearning.png',1)

output = img.copy() 
rectangle = cv2.rectangle(output, (260, 10),(460, 200), (0, 255, 0), 5) 

cv2.imshow('Machine Learning',rectangle)
cv2.waitKey(0)
cv2.destroyAllWindows()