user961627 user961627 - 4 months ago 22
Python Question

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

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

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