【问题标题】:roipoly matlab function equivalent in OpenCVOpenCV中的roipoly matlab函数等效
【发布时间】:2013-03-01 03:25:17
【问题描述】:

我正在使用 OpenCV 库将 matlab 代码转换为 C++。

谁能告诉我roipoly OpenCV 中等效的 matlab 函数?

或者如何使用 OpenCV 获得相同的功能?

BW = roipoly(I, c, r)

BW = roipoly(I, c, r) 返回由向量 c 和 r 描述的多边形指定的 ROI,它们分别指定每个顶点的列索引和行索引。 c 和 r 大小相同。

在我的例子中,我想从图像中提取 triangular roi,因此 c 和 r 的大小为 3x1。

谁能告诉我如何使用 OpenCV 在 C++ 中做到这一点?

【问题讨论】:

    标签: matlab opencv image-processing computer-vision roi


    【解决方案1】:

    OpenCV 中没有像 roipoly 这样对应的内置函数。

    相反,OpenCV 提供了 cv2.polyline() 和 cv2.drawContours() 等函数。如果你有顶点的坐标,(如 matlab 所示)你可以用它们创建一个 numpy 数组。然后在黑色图像上绘制这个多边形,它会为您提供 roipoly 返回的蒙版图像。下面是一个例子:

    import cv2
    import numpy as np
    
    img = cv2.imread('eight.png')
    mask = np.zeros(img.shape[:2],dtype = 'uint8')
    
    c = [194, 253, 293, 245]
    r = [72, 14, 76, 125]
    
    rc = np.array((c,r)).T
    
    cv2.drawContours(mask,[rc],0,255,-1)
    cv2.drawContours(img,[rc],0,255,2)
    mask = cv2.cvtColor(mask,cv2.COLOR_GRAY2BGR)
    
    res = np.hstack((img,mask))
    

    下面是我得到的结果:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2013-08-01
    • 2013-04-14
    • 1970-01-01
    相关资源
    最近更新 更多