简体   繁体   English

OpenCV测量矩形图像大小

[英]OpenCV measure rectangular image size

I have an app that finds an object in a frame and uses warpPerspective to correct the image to be square. 我有一个可以在框架中找到对象并使用warpPerspective将图像校正为正方形的应用。 In the course of doing so you specify an output image size. 在此过程中,您可以指定输出图像尺寸。 However, I want to know how to do so without harming its apparent size. 但是,我想知道如何在不损害其外观大小的情况下进行操作。 How can I unwarp the 4-corners of the image without changing the size of the image? 如何在不更改图像尺寸的情况下使图像的四角变形? I don't need the image itself, I just want to measure its height and width in pixels within the original image. 我不需要图像本身,我只想测量原始图像中像素的高度和宽度。

Get a transform matrix that will square up the corners. 得到一个变换矩阵,将四角化。

std::vector<cv::Point2f> transformedPoints;
cv::Mat M = cv::getPerspectiveTransform(points, objectCorners);
cv::perspectiveTransform(points, transformedPoints, M);

This will square up the image, but in terms of the objectCorners coordinate system. 这将使图像平方,但要考虑objectCorners坐标系。 Which is -0.5f to 0.5f not the original image plane. 不是原始图像平面的-0.5f到0.5f。

BoundingRect almost does what I want. BoundingRect几乎可以满足我的要求。

cv::Rect boundingRectangle = cv::boundingRect(points);

But as the documentation states 但正如文档所述

The function calculates and returns the minimal up-right bounding rectangle for the specified point set. 该函数计算并返回指定点集的最小垂直边界矩形。

And what I want is the bounding rectangle after it has been squared-up, not without squaring it up. 我想要的是将矩形平方后的边界矩形,而不是不对其进行平方。

According to my understanding to your post, here is something which should help you. 根据我对您的帖子的了解,以下内容应该会对您有所帮助。 OpenCV perspective transform example . OpenCV透视图转换示例

Update if it still doesn't help you out in finding the height and width within the image 如果仍然无法帮助您找到图像的高度和宽度,请进行更新

Minimum bounding rect of the points 点的最小边界矩形

cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));

As the minAreaRect reference on OpenCV's website states 正如OpenCV网站上的minAreaRect参考所指出的

Finds a rotated rectangle of the minimum area enclosing the input 2D point set. 查找包含输入2D点集的最小面积的旋转矩形。

You can call box.size and get the width and height. 您可以调用box.size并获取宽度和高度。

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

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