roipoly matlab函数在OpenCV中相当 [英] roipoly matlab function equivalent in OpenCV
问题描述
我使用OpenCV库将matlab代码转换为C ++。
任何人都可以告诉我 roipoly matlab函数在OpenCV中等效
strong>或者如何使用OpenCV获得相同的功能?
BW = roipoly(I,c,r)
BW = roipoly(I,c,r)返回由向量c和r,它们分别指定每个顶点的列和行索引。 c和r的大小相同。
在我的例子中,我想从图像中提取 triangle roi ,所以c和r的大小任何人都可以告诉我如何在C ++中使用OpenCV?
在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))
:
>
I am converting a matlab code into C++ using OpenCV libraries.
Can anyone tell me roipoly 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??
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 :
这篇关于roipoly matlab函数在OpenCV中相当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!