OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 28 Feb 2013 23:49:08 -0600roipoly matlab function equivalent in OpenCVhttp://answers.opencv.org/question/8268/roipoly-matlab-function-equivalent-in-opencv/I am converting a matlab code into C++ using OpenCV libraries.
**Can anyone tell me [roipoly][1] matlab function equivalent in OpenCV??**
**Or how to get the same functionality using OpenCV?**
BW = roipoly(I, c, r)
BW = roipoly(I, c, r) returns the ROI specified by the polygon described by vectors c and r, which specify the column and row indices of each vertex, respectively. c and r are of same size.
In my case I want to extract **triangular roi** from the image, so c and r are of size 3x1.
Can anyone tell me how to do this in C++ using OpenCV??
[1]: http://www.mathworks.in/help/images/ref/roipoly.htmlThu, 28 Feb 2013 21:28:50 -0600http://answers.opencv.org/question/8268/roipoly-matlab-function-equivalent-in-opencv/Answer by Abid Rahman K for <p>I am converting a matlab code into C++ using OpenCV libraries.</p>
<p><strong>Can anyone tell me <a href="http://www.mathworks.in/help/images/ref/roipoly.html">roipoly</a> matlab function equivalent in OpenCV??</strong></p>
<p><strong>Or how to get the same functionality using OpenCV?</strong></p>
<pre><code>BW = roipoly(I, c, r)
</code></pre>
<p>BW = roipoly(I, c, r) returns the ROI specified by the polygon described by vectors c and r, which specify the column and row indices of each vertex, respectively. c and r are of same size.</p>
<p>In my case I want to extract <strong>triangular roi</strong> from the image, so c and r are of size 3x1.</p>
<p>Can anyone tell me how to do this in C++ using OpenCV??</p>
http://answers.opencv.org/question/8268/roipoly-matlab-function-equivalent-in-opencv/?answer=8271#post-id-8271There 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][1]
[1]: http://i.stack.imgur.com/l9mM8.pngThu, 28 Feb 2013 23:49:08 -0600http://answers.opencv.org/question/8268/roipoly-matlab-function-equivalent-in-opencv/?answer=8271#post-id-8271