我需要知道cv2.findContours()
函数后的方向(顺时针/逆时针)轮廓。
我可以通过调用cv2.contourArea()
参数来做到这一点oriented
。但是如何更快呢?也许轮廓总是朝着一个方向?
谢谢!
搜索轮廓上的极值点 p,例如最右边的点。确定p的前任(pred)和后继(succ)。构建两个向量 a = pred - p,b = succ - p。然后叉积 axb 的 z 分量的符号告诉您轮廓的方向。
bool contourOrientationCW(const std::vector<cv::Point>& contour) {
if (contour.size() >= 3) {
cv::Point rm; // right most point
size_t rmIdx; // index of right most point
for (size_t i = 0; i < contour.size(); i++) {
const cv::Point& p = contour[i];
if (p.x > rm.x || p.x == rm.x && p.y > rm.y) {
rm = p;
rmIdx = i;
}
}
size_t i = rmIdx - 1; if (i < 0) i = contour.size() - 1;
const cv::Point& pred = contour[i];
i = rmIdx + 1; if (i == contour.size()) i = 0;
const cv::Point& succ = contour[i];
Vec2i a = pred - rm, b = succ - rm;
return a[0]*b[1] >= a[1]*b[0];
}
return true;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句