Is there any simple way to multiply cv::Rect size and coordinates?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Let's say I have 2 rectangle. I want the second rectangle to be twice bigger than the first rectangle and the position of x,y also twice bigger.

cv::Rect r1=Rect(10,20,40,60);
cv::Rect r2 = r1 * 2;  //this won't work

Setting the rectangle 2 parameter 1 by 1 will work

r2.height = r1.height * 2;
r2.width = r1.height * 2;
r2.x = r1.x * 2;
r2.y = r2.y * 2;

It works">works, but is there any simpler way to do it (like single line code)?

profilepic.png
manpreet 2 years ago

We can overload the * operator:

ref="https://forum.tuteehub.com/tag/cv">cv::Rect operator*(ref="https://forum.tuteehub.com/tag/cv">cv::Rect r, double ref="https://forum.tuteehub.com/tag/scale">scale) {
    r.height *= ref="https://forum.tuteehub.com/tag/scale">scale;
    r.width *= ref="https://forum.tuteehub.com/tag/scale">scale;
    r.x *= ref="https://forum.tuteehub.com/tag/scale">scale;
    r.y *= ref="https://forum.tuteehub.com/tag/scale">scale;
    return r;
}

And then you can multiply ref="https://forum.tuteehub.com/tag/rectangles">rectangles directly:

Rect r2 = Rect(10, 20, 40, ref="https://forum.tuteehub.com/tag/60">60) * 2;

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.