How to draw a rectangle around a region of interest in python

user961627 picture user961627 · May 18, 2014 · Viewed 156.6k times · Source

I'm having trouble with import cv in my python code.

My issue is I need to draw a rectangle around regions of interest in an image. How can this be done in python? I'm doing object detection and would like to draw a rectangle around the objects I believe I've found in the image.

Answer

berak picture berak · May 18, 2014

please don't try with the old cv module, use cv2:

import cv2

cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)


x1,y1 ------
|          |
|          |
|          |
--------x2,y2

[edit] to append the follow-up questions below:

cv2.imwrite("my.png",img)

cv2.imshow("lalala", img)
k = cv2.waitKey(0) # 0==wait forever