如何在python中围绕感兴趣的区域绘制矩形

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23720875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:22:34  来源:igfitidea点击:

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

pythonopencvcomputer-visiondraw

提问by user961627

I'm having trouble with import cvin my python code.

import cv我的 python 代码有问题。

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.

我的问题是我需要在图像中感兴趣的区域周围绘制一个矩形。这怎么能在python中完成?我正在做对象检测并想围绕我相信我在图像中找到的对象绘制一个矩形。

采纳答案by berak

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

请不要尝试使用旧的 cv 模块,请使用 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

回答by jdhao

You can use cv2.rectangle():

您可以使用cv2.rectangle()

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.

I have a PIL Image object and I want to draw rectangle on this image, but PIL's ImageDraw.rectangle()method does not have the ability to specify line width. I need to convert Image object to opencv2's image format and draw rectangle and convert back to Image object. Here is how I do it:

我有一个 PIL Image 对象,我想在这个图像上绘制矩形,但是 PIL 的ImageDraw.rectangle()方法没有指定线宽的能力。 我需要将 Image 对象转换为 opencv2 的图像格式并绘制矩形并转换回 Image object。这是我如何做到的:

# 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)

回答by Yanfeng Liu

As the other answers said, the function you need is cv2.rectangle(), but keep in mind that the coordinates for the bounding box vertices need to be integers if they are in a tuple, and they need to be in the order of (left, top)and (right, bottom). Or, equivalently, (xmin, ymin)and (xmax, ymax).

正如其他答案所说,您需要的函数是cv2.rectangle(),但请记住,如果边界框顶点的坐标在元组中,则它们的坐标必须是整数,并且它们需要按(left, top)和的顺序排列(right, bottom)。或者,等价地,(xmin, ymin)(xmax, ymax)