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

2020-07-10 05:32发布

I have two points (assumed to be from rectangle, which 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. i.e., both the points are indicating zero. So, How do I formulate a new Rect object with arbitrary two points ?

3条回答
2楼-- · 2020-07-10 05:55

You have to calcul basic informations from your two points. Width and height, and then create 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
查看更多
手持菜刀,她持情操
3楼-- · 2020-07-10 06:11

since Rect::tl() and Rect::br() just return copies, not references, try a constructor:

cv::Rect rRect(pTopLeft, pBottomRight);
查看更多
可以哭但决不认输i
4楼-- · 2020-07-10 06:11

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);
查看更多
登录 后发表回答