Expand Rect at 100px

I am using OpenCV 2.4.4 in C ++ and have Rect, I want to do more by x pixels. The OpenCV documentation provides a good example (look at "expanding or contracting a rectangle by a certain amount") how to use this in an image http://opencv.willowgarage.com/documentation/cpp/_images/math/a6f41031fb2ccaa600520bcbde63a8a9fcff9edf.png

Cool. Just the problem, I don't know how to inject this into 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 .

I get ugly errors on any of them.

Can someone explain that this is a smart character that I have to use, and it was impossible to include inside the HTML, so it is embedded in the image.

0
source share
2 answers

This is the mathematical notation for "+ =" in C ++. You are not actually using ± directly. For instance:

//make a rectangle that 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 area -- this will increase rect to be 20x20 rect += cv::Size(10, 10); std::cout << rect.area() << std::endl; 

So, to make your example of adding 10 to the height and width of the rectangle, think of the ± symbol as equivalent to += cv::Size(width, height)

Well, you more or less had the right idea ...

EDIT: As for your comment, I have to clarify: rect + = point, rect - = point, rect + = size, rect - = size - 4 different operations, for example:

rect += point and rect -= point : moves the rectangle at the point where the point refers to the cv::Point object, i.e. you can declare a point as: cv::Point pt(5, 5); , then if you do rect += pt , it will shift the rectangle of x and y members (by default they refer to the upper left left side of the rectangle) by 5 - for example. this is equivalent to doing rect.x += 5 and rect.y += 5 . The case for rect -= point the same, although for subtraction and not for addition (so rect.x -= 5 and rect.y -= 5 ).

rect += size and rect -= size will change the size of the rectangle, not the coordinate, so += will increase the area of ​​the rectangle, and -= will reduce it.

+3
source

If you want to expand Rect while maintaining the same source (just increase its width and height ), you can use:

 cv::Size inflationSize(20, 20); myRect += inflationSize; 

To add +20 to both width and height . If you also want to compensate for the x and y points and actually inflate the rectangle from all sides, you can do the following:

 cv::Point inflationPoint(-20, -20); cv::Size inflationSize(20, 20); myRect += inflationPoint; myRect += inflationSize; 

cv::Rect has + (and other) operators, overloaded so that if it is a cv::Point , it will adjust the origin, and if it is a cv::Size , it will adjust width and height .

+2
source

Source: https://habr.com/ru/post/1241015/


All Articles