1
votes

I am re asking this question with a better explanation.

I have obtained the four points of a rectangle that I wish to perform a warp perspective transformation on. I have successfully achieved a transform on one of my images as I manually selected the points and assigned their location for the transform.

cv::Point2f src_vertices[4];
src_vertices[0] = corners[3];
src_vertices[1] = corners[1];
src_vertices[2] = corners[0];
src_vertices[3] = corners[2];

Point2f dst_vertices[4];
dst_vertices[0] = Point(0, 0);
dst_vertices[1] = Point(box.boundingRect().width-1, 0); 
dst_vertices[2] = Point(0, box.boundingRect().height-1);
dst_vertices[3] = Point(box.boundingRect().width-1, box.boundingRect().height-1);

As you can see I assigned the corners to the transformation vertices manually.

As I wish to use this code on multiple slightly different images I would like a more accurate way of assigning the corners.

All the images will look similar to this:

http://imgur.com/t0cgTzr

The corners for this image are as follows: Top left - (1106, 331), Top right- (810, 747), Bottom left- (825, 187), Bottom right- (510, 537)

I have found the max and min x and y values as follows:

float X, Y;
float maxX= 0;
float minX = 10000;
float maxY= 0;
float minY = 10000;
for(int i=0; i< 4; i++){
    if( corners[i].x > maxX){
        maxX = corners[i].x;
    }
    if( corners[i].x < minX){
        minX = corners[i].x;
    }
    if( corners[i].y > maxY){
        maxY = corners[i].y;
    }
    if( corners[i].y < minY){
        minY = corners[i].y;
    }

}

This gives me:

maxX - 1106, minX - 510, maxY - 747, minY - 187.

I would like to know how to recombine the max and min values to their respective values so I can use the points to perform the transform. I am quite new to opencv so sorry if this is very obvious.

1
You mean you want to get (1106, 331) back from 1106? There's no way you can do it only with the max and min info, you will need to search for a vertex with that x value. - villasv

1 Answers

1
votes

What you can do to get the corners without losing information on the other coordinate is to find the whole point. Instead of storing just float maxX= 0; you can store Point2f maxX(0,0); and modify you for loop to keep track of the point itself, not only the x coordinate.

Better yet, get rid of those loops and make use of STL

Point2f maxX = *std::max_element(corners, corners+4, [](Point2f a, Point2f b){return a.x < b.x;});