Reputation: 2896
I use OpenCV 2.4.4 in C++ and have Rect I want to make bigger by x pixels. OpenCV documentation provides nice example (look for "expanding or shrinking rectangle by a certain amount") how to use this in image http://opencv.willowgarage.com/documentation/cpp/_images/math/a6f41031fb2ccaa600520bcbde63a8a9fcff9edf.png
Cool. Only problem I do not know how to enter this in actual C++ code. I tried:
Rect rect=oldrect+-10; Rect rect=oldrect-+10; and Rect rect=oldrect±10; (I copied this symbol from http://en.wikipedia.org/wiki/Plus-minus_sign to be sure.
I get ugly errors on any of them.
Can someone please explain, what is that clever symbol I should use and was impossible to include inside HTML, so it is embedded as image.
Upvotes: 2
Views: 11897
Reputation: 2869
If you're looking to expand the Rect
while keeping the same origin (just increase its width
and height
), you can use:
cv::Size inflationSize(20, 20);
myRect += inflationSize;
Which would add +20 to both width
and height
. If you also want to offset the x
and y
points and actually inflate the rect from all sides, you can do the following:
constexpr int inflation = 20;
myRect += cv::Point(-inflation/2, -inflation/2);
myRect += cv::Size(inflation, inflation);
cv::Rect
has the +
(and other) operators overloaded such that if it's a cv::Point
it will adjust the origin, and if it's a cv::Size
it will adjust width
and height
.
Upvotes: 7
Reputation: 4194
That's the mathematical notation for "+=" in C++. You don't actually use ± directly. For example:
//make a rectangle that's 10x10 and centered at (0, 0)
cv::Rect rect(0, 0, 10, 10);
std::cout << "Original Rectangle: " << rect.area() << std::endl;
//manual addition to rectangle dimensions -- this will make bigger_rect be 20x20
cv::Rect bigger_rect = rect;
bigger_rect.height += 10;
bigger_rect.width += 10;
std::cout << "Bigger Rectangle: " << bigger_rect.area() << std::endl;
//other method of adding to a rectangle's area -- this will increase rect to be 20x20
rect += cv::Size(10, 10);
std::cout << rect.area() << std::endl;
So to do your example of adding 10 to the height and width of a rectangle, think of the ±
symbol as being equivalent to += cv::Size(width, height)
Well, you more or less had the right idea...
EDIT: In regards to your comment, I should elaborate: rect += point, rect -= point, rect += size, rect -= size are 4 distinct operations, such that:
rect += point
and rect -= point
: shifts the rectangle by point, where point refers to a cv::Point
object, i.e. you can declare a point as:
cv::Point pt(5, 5);
, then if you do rect += pt
, it'll shift the rectangle's x and y members (by default, these refer to the rectangle's upper left coordinate) by 5 -- e.g. it's equivalent to doing rect.x += 5
and rect.y += 5
. The case for rect -= point
is the same, albeit for subtraction rather than addition (so rect.x -= 5
and rect.y -= 5
).
rect += size
and rect -= size
will change the rectangle's size as opposed to it's coordinate position, such that +=
will increase the rectangle area and -=
will decrease it.
Upvotes: 6