简体   繁体   中英

Convert two points to a rectangle (cv::Rect)

I have two points (assumed to be from a rectangle and are its top-left corner & bottom-right corner).

Point pTopLeft;
Point pBottomRight;

I want to formulate a cv::Rect using these points. So, I tried

cv::Rect rRect;
rRect.tl() = pTopLeft;
rRect.br() = pBottomRight;

There is no error. But the Rect seems to be containing nothing. ie, both the points are indicating zero. So, How do I formulate a new Rect object with arbitrary two points?

由于Rect::tl()Rect::br()只返回副本,而不是引用,请尝试构造函数:

cv::Rect rRect(pTopLeft, pBottomRight);

You have to calculate basic information from your two points: width and height. Then, create a new object using the following constructor :


(Object) rect(x, y, width, height)

pTopLeft.x = x

pTopLeft.y = y

pBottomRight.x - pTopLeft.x = width

pTopLeft.y - pBottomRight.y = height

You can make it this way also,

Point pTopLeft;
Point pBottomRight;
cv::Rect rRect(pTopLeft.x,pTopLeft.y,pBottomRight.x-pTopLeft.x,pBottomRight.y-pTopLeft.y);

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