矩形中的最大和最小顶点 [英] Max and min vertices in a rectangle

查看:67
本文介绍了矩形中的最大和最小顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在用更好的解释来问这个问题.

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

这张图片的角如下:左上角 - (1106, 331),右上角 - (810, 747),左下角- (825, 187),右下角 - (510, 537)

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

我找到了最大和最小 x 和 y 值,如下所示:

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

}

这给了我:

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

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

我想知道如何将最大值和最小值重新组合为其各自的值,以便我可以使用这些点来执行变换.我对 opencv 还很陌生,如果这很明显,我很抱歉.

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.

推荐答案

要在不丢失其他坐标信息的情况下获得角点,您可以做的是找到整点.而不是只存储 float maxX= 0; 你可以存储 Point2f maxX(0,0); 并修改你 for 循环以跟踪点本身,而不仅仅是 x 坐标.

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.

更好的是,摆脱那些循环并使用 STL

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

这篇关于矩形中的最大和最小顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆