轮廓与矩形的交集 OPENcv c++

4

我使用cv.rectangle绘制了一个矩形,并得到了一个轮廓形状(来自FindContours),矩形覆盖在轮廓上。

矩形与轮廓完全相交于两个点。如何找到矩形与轮廓轮廓线之间的这些交点?

我可以将两个图像叠加在一起并查找最大值,但我不知道矩形顶点是如何存储的,因为我需要一个填充有一组点的线类型向量。

谢谢

1个回答

2
如果您确定您的矩形仅在两个点处与形状相交,那么您可以遍历轮廓点,并检查这些点是否在矩形边界上。
std::vector<cv::Point> shape; // computed with FindContours
cv::Rect myRect; //whatever

const int NUMPOINTS = 2;
int found = 0;
for(std::vector<cv::Point>::iterator it = shape.begin(); it != shapes.end() && found < NUMPOINTS; ++it) {
  if (it->y == myRect.y && it->x >= myRect.x && it->x < myRect.x + width)
    // that point cross the top line of the rectangle
    found++; // you might want to store the point
  else if (// ... add the other checks here)

}  

是的...谢谢,它只会在两个位置相交....感谢您的答案..我下周度假回来后会尝试一下。 - barsil sil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接