cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift)
Draws a simple, thick, or filled up-right rectangle.
The function rectangle draws a rectangle outline or a filled rectangle
whose two opposite corners are pt1 and pt2.
Parameters
img Image.
pt1 Vertex of the rectangle.
pt2 Vertex of the rectangle opposite to pt1 .
color Rectangle color or brightness (grayscale image).
thickness Thickness of lines that make up the rectangle. Negative values,
like CV_FILLED , mean that the function has to draw a filled rectangle.
lineType Type of the line. See the line description.
shift Number of fractional bits in the point coordinates.
# im is a PIL Image object
im_arr = np.asarray(im)
# convert rgb array to opencv's bgr format
im_arr_bgr = cv2.cvtColor(im_arr, cv2.COLOR_RGB2BGR)
# pts1 and pts2 are the upper left and bottom right coordinates of the rectangle
cv2.rectangle(im_arr_bgr, pts1, pts2,
color=(0, 255, 0), thickness=3)
im_arr = cv2.cvtColor(im_arr_bgr, cv2.COLOR_BGR2RGB)
# convert back to Image object
im = Image.fromarray(im_arr)
4条答案
按热度按时间8ftvxx2r1#
请不要尝试旧的cv模块,使用cv2:
[edit]在下面附加后续问题:
vm0i2vca2#
正如其他答案所说,您需要的函数是
cv2.rectangle()
,但请记住,如果边界框顶点在元组中,则它们的坐标需要是整数,并且它们需要按照(left, top)
和(right, bottom)
的顺序。或者,等价地,(xmin, ymin)
和(xmax, ymax)
。9nvpjoqh3#
可以使用
cv2.rectangle()
:我有一个PIL图像对象,我想在这个图像上绘制矩形。* * 我想使用opencv2绘制矩形,然后转换回PIL Image对象**。我是这样做的:
zd287kbt4#
在图像上绘制矩形的代码是:
其中
pt1
是起点-->(x,y),pt2
是终点-->(x+w,y+h)。