Wendie Lamero
Wendie Lamero

Reputation: 59

Opencv resizing rect whilst maintaining centroid

I am having a bit of an issue, I have a rectangle from contour detection but I want to re-size the rectangle whilst maintaining the centroid I noticed when I resize it, its obtaining a new centroid, this is because I want it to be at the center of my object not but when re-sized its moving to the sizes here is an image before resize, my centroid is marked by the red and blue markers

Before resize:

After resize:

2

Here is how I am resizing:

private static Rect rotateRect(Rect rect, int heightPercentage, int widthPercetange)
{

    int originalX = 0;
    int originalY = 0;
    originalX = rect.x;
    originalY = rect.y;
    rect.height = resizeObject(rect.height, heightPercentage);
    rect.width = resizeObject(rect.width, widthPercetange);
    rect.x = originalX;
    rect.y = originalY;

    return rect;
}




private static int resizeObject(int resize, int percentage)
{

    return  (int)(resize *(percentage/100.0f));

}

Upvotes: 1

Views: 3770

Answers (1)

Miki
Miki

Reputation: 41765

You simply need to add to x and y coordinates half the amount you remove from width and height.

private static Rect rotateRect(Rect rect, int heightPercentage, int widthPercetange)
{
    int rwidth  = rect.width;
    int rheight = rect.height;

    rect.width = Math.round((rect.width * widthPercetange) / 100.0f);
    rect.height = Math.round((rect.height * heightPercentage) / 100.0f);        
    rect.x += (rwidth - rect.width) / 2;
    rect.y += (rheight - rect.height) / 2;

    return rect;
}

Upvotes: 1

Related Questions