Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
hi all
i have an image contains white and black pixels only, how can i find the center of white pixels
Posted
Comments
Sergey Alexandrovich Kryukov 24-Oct-11 18:21pm    
It depends on how you define the center. Is it the center of gravity?
--SA
Philippe Mori 24-Oct-11 18:25pm    
What is you definition of center? Does you have only 1 white area? What is the shape of your area?

If this is a center of gravity to be found:

First, define some arbitrary point (say, in the center) and calculate their positions relative to this points, weighted by their masses, which you can assign to 1. Then follow this algorithm: http://en.wikipedia.org/wiki/Center_of_mass[^]. That's it.

—SA
 
Share this answer
 
[UPDATED]

What i should do is:

C#
//the width and height of your screen.
int width = 640;
int height = 480;

//The average white pixels of x and y.
int average_x;
int average_y;

//counts the x and y axis.
int count_x = 0;
int count_y = 0;

//tells you how many white pixels there are.
int count = 0;

//Make a for loop for the x axis and the y axis.
for(int a = 0; a < width; a = a+1;){
for(int b = 0; a < height; b = b+1;){

//If the pixel is white
if(pixel(a, b) == white)
{
count = count+1;
count_x = count_x+a;
count_y = count_y+b;
}

}
}

//Calculate the average x and y axis for the white pixels.
average_x = count_x/count;
average_y = count_y/count;


The average_x and average_y are the center axis you want.
 
Share this answer
 
v2
Comments
Philippe Mori 24-Oct-11 18:27pm    
Humm... if only the last point is white, you code would give x= 1 and y = 1 as the center.

By the way, there is no count variable but count_x and count_y.
xzjitzx 25-Oct-11 8:06am    
sorry i forgot something i will change it.
xzjitzx 25-Oct-11 8:09am    
It should work now.
Amir Mahfoozi 25-Oct-11 8:25am    
5+
How about
(sum(x)/count(x), sum(y)/count(y))
 
Share this answer
 
Comments
Philippe Mori 24-Oct-11 18:36pm    
What are the sum and count? I guess that the count is the count of white pixels while the sum is the of the coordinate of white pixels. Not clear as an answer.

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