【问题标题】:How to draw a rectangle around a region of interest in python如何在python中的感兴趣区域周围绘制一个矩形
【发布时间】:2014-07-06 10:01:10
【问题描述】:

我在 python 代码中遇到了import cv 的问题。

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

【问题讨论】:

    标签: python opencv computer-vision draw


    【解决方案1】:

    请不要尝试使用旧的 cv 模块,使用 cv2:

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

    [编辑]以附加以下后续问题:

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

    【讨论】:

    • 我刚刚尝试导入 cv2。我得到:DLL load failed: %1 is not a valid Win32 application。我正在运行win7 64位。还有 spyder 和 scikit 库,一切都运行 64 位。
    • @user961627 从这里下载 64 位安装二进制文件 lfd.uci.edu/~gohlke/pythonlibs/#opencv
    • 谢谢!我已经下载了...知道如何让它与 Anaconda 的 Spyder 一起使用吗?
    • 好的,我已经在 Spyder 上运行了。 @berak,如何显示或保存图像?我应该将它分配给一个 numpy 数组吗?我试过 ig = ImageViewer( cv2.rectangle(image1, (10, 10), (100, 100), (255,0,0), 2)) ig.show() 但得到了这个错误:TypeError: 输出数组 img 的布局与 cv::Mat 不兼容(step[ndims-1] != elemsize 或 step[1] != elemsize*nchannels)
    • 如何找到这些坐标 x1,y1 和 x2,y2 ?
    【解决方案2】:

    你可以使用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.
    

    我有一个 PIL Image 对象,我想在这个图像上绘制矩形。 我想用opencv2画矩形,然后转回PIL Image对象。这是我的做法:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        在图像上绘制矩形的代码是:

        cv2.rectangle(image, pt1, pt2, color, thickness)
        

        pt1 是起点-->(x,y),pt2 是终点-->(x+w,y+h)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-02-28
          • 2017-08-27
          • 2018-12-02
          • 2013-10-19
          • 2013-04-07
          • 2013-03-03
          • 1970-01-01
          • 2013-07-11
          相关资源
          最近更新 更多