矩形OpenCv数组中的交点

时间:2016-01-19 17:35:54

标签: opencv detection intersection

我尝试检测视频中的某些移动物体。如果任何两个/三个+矩形重叠/相交,我希望它们改变颜色。 我尝试过这样的事情:

$links = 'http://test.com/item/16';
preg_replace("/item","z/item",$links);

echo $links; 
// output >>> http://test.com/z/item/16

它确实有效,但现在总是如此。我不知道我做得对,还是有更好的方法。

1 个答案:

答案 0 :(得分:1)

您似乎以正确的方式计算交叉点,但您只为每个i绘制一次结果。如果矩形j不与i相交,则您将再次覆盖更改后的颜色。只需删除else用于设置颜色的情况,或者记住矩形是否(或多久)与另一个矩形相交。例如:

for (size_t i = 0; i < contours.size(); i++)
    {
        int nIntersections = 0;
        // intersection
        original = boundRect[i];
            for (size_t j = 0; j < boundRect.size(); j++){
              if (j == i) continue; // the same rectangle
              match = boundRect[j];
              if ((original & match).area() > 0)
              {
                  nIntersections++;
                  // if you only want to know whether intersections appear or not, you can stop the inner for-loop here, by using j=boundRect.size(); continue; or break;
              }
            }
            // draw the rectangle
            cv::Scalar color(0,255,0);
            if(nIntersections > 0) color = cv::Scalar(0,0,255);
            // adjust for different 
            // if(nIntersections > 1) color = ...
            rectangle(frame, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0);
            // for simplicity you can just call rectangle(frame, boundRect[i], color, 2, 8, 0); without getting top-left and bottom-right points first. cv::rectangle uses cv::Rect as a parameter directly if you like.
    }   

如果你想绘制每个交叉区域,它会变得有点复杂,但也可以做到......