Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 6-Oct-11 3:16am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Infinityever at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Infinityever at 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)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 6 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100