Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. i.e., both the points are indicating zero. So, How do I formulate a new Rect object with arbitrary two points?

like image 938
Karthik_elan Avatar asked Mar 28 '14 10:03

Karthik_elan


2 Answers

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

cv::Rect rRect(pTopLeft, pBottomRight);
like image 106
berak Avatar answered Sep 28 '22 09:09

berak


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
like image 34
Orelsanpls Avatar answered Sep 28 '22 07:09

Orelsanpls