# Finding area center of rectangle

I am trying to find an area center of various types of rectagles.

(Center of gravity and midpoint of 4 vertices never work so please think in different way) I have to find the position of position of red dots

Point vertices;
Point areaCenter;


I have to find areaCenter so (nearly)

area(areaCenter, vertices, vertices) = area(areaCenter, vertices, vertices) =

area(areaCenter, vertices, vertices) = area(areaCenter, vertices, vertices)

I tried many different ways to find the mid point but none of them covered every types of rectangles.

Can any1 give me some idea?

edit retag close merge delete

Sort by » oldest newest most voted You need to add the positions of non-zero pixels in horizontal (vertical) directions and divide them by the number of nonzero pixels to get the center of gravity.

xsum = 0;
ysum = 0;
num_nonzero_pxl = 0;
for each pixel (p(x,y))
if pixel p(x,y) is non-zero {
xsum += x;
ysum += y;
num_nonzero_pxl++;
}
cgix=xsum/num_nonzero_pxl;
cgiy=ysum/num_nonzero_pxl;

more

Thanks for the answer. But I don't quite understand what you are trying to say. Can you write some lines of pseudocode please?

that's not gonna work. Equally shaped rectangle may have different midpoint depending over its rotation.

It should. Remember, you are adding x and y coordinates (the locations) if the pixel is nonzero and not the actual value of pixel.

Shouldn't it be cgix=xsum/total_num_nonzero; cgiy=ysum/total_num_nonzero; ?

And I am pretty sure the midpoint will be placed outside of rectangle in the rectangle #6 in the image as well as it takes too long.

Yes, you are right about the denominator. It should be total_num_nonzero. My bad. Fixed the answer above.

Perhaps this is what you are looking for :

http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

more

Thanks for the link. I have implemented the Centroid of polygon in your link and even posted my code in the page. However it also has chance to be placed outside of polygon.. :(

Official site

GitHub

Wiki

Documentation