【问题标题】:Min and max value of Point2f vectorPoint2f 向量的最小值和最大值
【发布时间】:2015-11-28 18:55:33
【问题描述】:

我想从 Point2f 点的向量中找到矩形的四个角。

这些点将是具有各自值的最大和最小 x 值,而其他两个角将是具有各自 y 值的最大 y 值。

到目前为止,我找到了 x 和 y 的最大值和最小值。我试图弄清楚如何将这些最大值与它们的其他值重新结合起来,所以我有四个 x,y 点。这可能吗?

编辑:矩形与图片视图成一定角度。

这是我的代码,目前运行良好:

vector<cv::KeyPoint> keypoints;
blob_detector->detect(backproj_dilate, keypoints);

vector<Point2f> XY;
for (size_t i=0; i<keypoints.size(); i++){ 
    XY.push_back(keypoints[i].pt);

}

float X, Y;
float maxX= 0;
float minX = 10000;
float maxY= 0;
float minY = 10000;
for(size_t i=0; i<keypoints.size(); i++){
    X = XY[i].x;
    Y = XY[i].y;
    if( X > maxX){
        maxX = X;
    }
    if( X < minX){
        minX = X;
    }
    if( Y > maxY){
        maxY = Y;
    }
    if( Y < minY){
        minY = Y;
    }

}

【问题讨论】:

  • 你尝试解决stackoverflow.com/questions/33938487/opencv-connecting-dots 吗?如果是这样,请尝试@Miki 的建议。
  • 在研究这些时,我遇到了一个我认为很棒的 simplBlobDetector,因为它给了我每个点的坐标。我现在就是这样。你认为我试图做的事情是不可能的吗?
  • 如果您发布完整的代码,我可以展示如何做您想做的事情。
  • 查看 OpenCV 示例 minarea.cpp 。
  • 我现在不在笔记本电脑旁,但我会看看并回复您。感谢您的帮助。

标签: c++ opencv vector


【解决方案1】:

在CV中可以使用以下两个函数,具体取决于边界框是否旋转:

1) boundingRect

2) minAreaRect

在 rect/roated rect 数组中返回的后续 x,y 是 xmin 和 ymin。其他参数可以用来得到xmax,ymax。

【讨论】:

    【解决方案2】:

    除了您当前的结构之外,例如:

    struct vector2 { float x, y; };
    struct key_point { vector2 pt; };
    

    你可以有一个包含所有四个角的矩形结构:

    struct rect {
        vector2 top_left;
        vector2 top_right;
        vector2 bottom_left;
        vector2 bottom_right;
    };
    

    然后您可以使用以下内容填充它们:

    std::vector<key_point> key_points;
    auto x_comparator = [](auto const& a, auto const& b) { return a.pt.x < b.pt.x; };
    auto y_comparator = [](auto const& a, auto const& b) { return a.pt.x < b.pt.x; };
    
    float max_x = boost::max_element(key_points, x_comparator)->pt.x;
    float min_x = boost::min_element(key_points, x_comparator)->pt.x;
    float max_y = boost::max_element(key_points, y_comparator)->pt.y;
    float min_y = boost::min_element(key_points, y_comparator)->pt.y;
    auto rectangle = rect{
        {min_x, min_y},
        {max_x, min_y},
        {min_x, max_y},
        {max_x, max_y}
    };
    

    Live demo

    【讨论】:

    • 您好,抱歉,我的问题应该更明确。矩形是一个角度,因此与 maxX 值对应的 y 值不是最大或最小 y 值。
    【解决方案3】:

    现在你有

    (maxX, maxY)
    (minX, minY)
    

    你已经知道它是一个矩形,所以其他两个点是

    (maxX, minY)
    (minX, maxY)
    
           minX          maxX    
       0|---|--------------|--
        |
     minY (minX, minY)  (maxX, minY)
        |
        |
        |
        |
        |
     maxY (minX,maxY)   (maxX,maxY)
        |
    

    【讨论】:

    • 您好,抱歉,我的问题应该更明确。矩形是一个角度,因此与 maxX 值对应的 y 值不是最大或最小 y 值。
    【解决方案4】:

    嗯,你有 4 分:

    P1 = { XMin, YMin }
    P2 = { XMax, YMin }
    P3 = { XMin, YMax }
    P4 = { XMax, YMax }
    
    P1 ---- P2
    |        |
    P3 ---- P4
    

    你可以把它放在一个数据结构中

    struct Rectangle
    {
        float UpLeft[2];
        float UpRight[2];
        float DownLeft[2];
        float DownRight[2];
    };
    

    (我这里假设 Y = 0 在顶部,X = 0 在左侧)

    【讨论】:

    • 您好,抱歉,我的问题应该更明确。矩形是一个角度,因此 maxX 值对应的 y 值不是最大或最小 y 值。
    猜你喜欢
    • 2015-05-04
    • 2020-06-02
    • 2015-10-11
    • 1970-01-01
    • 2012-08-09
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多