简体   繁体   中英

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. 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. Which is -0.5f to 0.5f not the original image plane.

BoundingRect almost does what I want.

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 .

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

Finds a rotated rectangle of the minimum area enclosing the input 2D point set.

You can call box.size and get the width and height.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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