user5608415
user5608415

Reputation: 35

Min and max value of Point2f vector

I want to find four corners of a rectangle out of a vector of Point2f points.

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.

So far I found the max and mins for x and y. I am trying to figure out how to reunite these max values with their other value so i have four x,y points. 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;
    }

}

Upvotes: 0

Views: 2472

Answers (4)

Ashutosh Gupta
Ashutosh Gupta

Reputation: 680

You can use the following two functions in CV, depending upon whether the bounding box is rotated or not:

1) boundingRect

2) minAreaRect

The subsequent x,y returned in the rect/roated rect array are the xmin and ymin. and the other parameters can be used to get xmax, ymax.

Upvotes: 2

Shoe
Shoe

Reputation: 76280

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

Upvotes: 0

Sharundaar
Sharundaar

Reputation: 322

Well you've got 4 points :

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)

Upvotes: 0

Ceros
Ceros

Reputation: 490

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)
    |

Upvotes: 0

Related Questions