Click here to Skip to main content
15,850,917 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an PNG image with one un-transparent object in it, other region else is transparent. Now, how I get region of that object and how I get center of that region? If anyone have answer, please share it. And if it have codes, that is best (please write it in c#). Thankyou!

My english is not good, because i'm not english man.
Posted

Define 'centre'.

One approach is to get the 'centre of mass' (pseudocode):
float xaccum = 0, yaccum = 0;
float divisor = 0;

for(x = 0 to width-1)
 for(y = 0 to height-1) {
  float thisweight = pixels[x,y].A / 255f
  xaccum += x * thisweight;
  yaccum += y * thisweight;

  divisor += thisweight;
 }
}

PointF centre = [xaccum / divisor, yaccum / divisor];


(Yes, that can easily be optimised, particularly the divisor, but this forumlation is clear about the 'weighted average' nature of the calculation.)

This requires reading over the whole pixel array so it will be slow (even using LockBits and scanning the array instead of using GetPixel). I can't currently think of a way that doesn't involve that, though.

It weights by the transparency, if you want all non-transparent pixels (even with an alpha of 1 part in 255) to count as full weight set thisweight to 1 or 0.
 
Share this answer
 
Comments
Infinityever 6-Oct-11 11:57am    
I know way using "LockBits", but you said it still slow. I don't know any other way. Does anyone know it?
BobJanova 6-Oct-11 12:00pm    
It's much faster than using GetPixel, and depending on the size of the image, may be good enough. Anything which requires scanning all the pixels in the image is going to be relatively slow though (and scale as n² with the size of the image).

You might be able to get away with resampling the image to a small size (say 100×100) at which point the iteration won't take much time.
I suggest iterating through the image data.

Determine the formula for the smallest circle which contains all the opaque points inside or on the circle.

The center of that circle would be the center of the opaque object.

[Edit]
Find the two points that are farthest apart.

distance = sqrt((x1 - x2)^2 + (y1 - y2)^2)

Then make a circle with those two points

(distance /2 )^2 = (x - (x1+x2)/2)^2 + (y - (y1+y2)/2)^2

if all other points are in this circle, then you have the center point of ((x1+x2)/2, (y1+y2)/2)

if not, take the point farthest from the circle and combine with your original 3 points to form a circle

r^2 = (x - h)^2 + (y - k)^2

3 points : (a,b) (c,d) (e,f)

h = (1/2)((a²+b²)(f-d) + (c²+d²)(b-f) + (e²+f²)(d-b)) / (a(f-d)+c(b-f)+e(d-b))

k = (1/2)((a²+b²)(e-c) + (c²+d²)(a-e) + (e²+f²)(c-a)) / (b(e-c)+d(a-e)+f(c-a))

r² = (a-h)² + (b-k)²

(h,k) is the center point
 
Share this answer
 
v2
Comments
Infinityever 6-Oct-11 12:00pm    
Please describe more detail on it! Maybe some pseudocode at "Determine the formula for the smallest circle which contains all the opaque points"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900