简体   繁体   English

Point2f向量的最小值和最大值

[英]Min and max value of Point2f vector

I want to find four corners of a rectangle out of a vector of Point2f points. 我想从Point2f点的向量中找到矩形的四个角。

These points will be the max and min x values with their respective values and the other two corners will be the max y values with their respective y values. 这些点将是具有各自值的max和min x值,其他两个角将是具有各自y值的max y值。

So far I found the max and mins for x and y. 到目前为止,我找到了x和y的最大值和最小值。 I am trying to figure out how to reunite these max values with their other value so i have four x,y points. 我试图弄清楚如何使这些最大值与其他值重新统一,所以我有四个x,y点。 Is this possible? 这可能吗?

Edit: The rectangle is at an angle with respect to the picture view. 编辑:矩形相对于图片视图成一定角度。

This is my code as it stands and is working well: 这是我目前的代码,并且运行良好:

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;
    }

}

You can use the following two functions in CV, depending upon whether the bounding box is rotated or not: 您可以在CV中使用以下两个功能,具体取决于是否旋转了边界框:

1) boundingRect 1) boundingRect

2) minAreaRect 2) minAreaRect

The subsequent x,y returned in the rect/roated rect array are the xmin and ymin. 在rect / roated rect数组中返回的后续x,y是xmin和ymin。 and the other parameters can be used to get xmax, ymax. 其他参数可用于获取xmax,ymax。

Well now you have 好吧,你现在有

(maxX, maxY)
(minX, minY)

You already know it's a rectangle, so the other two points are 您已经知道这是一个矩形,所以其他两点是

(maxX, minY)
(minX, maxY)

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

Well you've got 4 points : 好吧,您得了4分:

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

P1 ---- P2
|        |
P3 ---- P4

You can put that in a data structure 您可以将其放在数据结构中

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

(I supposed here that Y = 0 is at the top and X = 0 is on the left) (我在这里假设Y = 0在顶部,X = 0在左侧)

Other than your current structures, which are like: 除了您当前的结构,例如:

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

you could have a rectangle structure that holds all four corners: 您可能拥有一个包含所有四个角的矩形结构:

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

and then you can populate them all with: 然后您可以使用以下命令填充它们:

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 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM