# How to translate the Region of interest to the Center of gravity of the image?

I'm using Opencv 3.0 c++.I have been able to find the COG of an irregular object but now I need to translate the ROI to the COG of the image,that is to make it in the center.

Any suggestions how to do it? edit retag close merge delete

Sort by » oldest newest most voted

if topleft corner (M) of ROI is (xr,yr) in orignal image (origin O) and gravity center (G) of contour is(xg,yg) in roi coordinates then gravity center coordinates in orignal image is (xr+xg,yr+yg).

or

if topleft corner of ROI is (xr,yr) in orignal image and gravity center of contour is(xg,yg) in image coordinates then gravity center coordinates in roi is (-xr+xg,-yr+yg).

OM+MG=OG

If you want to move all points belonging to a contour :

Point d(100,200);// translate vector d
for (int i=0;i<contour.size();i++
contour[i] += d;

more

Do you want to move contour ? If yes you have to move all contour points

I have been using the following code:

//Translate the ROI to the COG of the image
int x;
if (COG.x > center.x)
{
x = COG.x - center.x;
x = -x;
}
else
{
x = (COG.x - center.x)*-1;
}

int y;

if (COG.y < center.y)
{
y = center.y - COG.y;
}
else
{
y = center.y - COG.y;
}


But still no output.The object is not being positioned at the center. Did i made any mistake for the translation?

more

Official site

GitHub

Wiki

Documentation