roipoly matlab function equivalent in OpenCV

后端 未结 1 992
孤街浪徒
孤街浪徒 2020-12-21 21:43

I am converting a matlab code into C++ using OpenCV libraries.

Can anyone tell me roipoly matlab function equivalent in OpenCV??

Or

相关标签:
1条回答
  • 2020-12-21 22:32

    There is no corresponding inbuilt function like roipoly in OpenCV.

    Instead, OpenCV provides functions like cv2.polyline() and cv2.drawContours(). If you have the coordinates of the vertices, (as shown in matlab) you can create a numpy array with them. Then draw this polygon on a black image, which gives you the mask image as returned by roipoly. An example is demonstrated below :

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

    Below is the result I got :

    enter image description here

    0 讨论(0)
提交回复
热议问题