algorithm - Challenge: Bitmap Centroid -


I want to calculate the nucleus of a bitmap (black and white), which is the bit-pack array of integers is stored. I know that there are fast algorithms to calculate the number of bits set in an integer, but does this help me to calculate a centroid, any ideas?

For example, if my bitmap looks like this:

 <111> 111000 00000 000000 000000 000000   

Center 1, At 1. Pack up to 32bit integers (you choose endurance), it can look like this: {width: 6, height: 6} {3817734144, 0}.

Bonus points if you can get each bit again without having to extensively (9 in the example).

It is about to process a line at a time. (Once you get the total mass, and the center of the mass of each line, then it is a weighted average to get the centered X and Y coordinates).

In other words, you want to calculate the sum of bits b i and the sum of b i f (i) for some tasks F if F (I) = 1, it's a little count (let's say C), and if F (I) = i, it's total MM moment we get to the center of mass For C input less than 8 bits, you can easily store the tables for C and M, each 256 bytes wide. Let's write a bigger number than the H: L with eight buttons: L, where L is the lower 8 bits of the number, and h is the rest bits.

then

  C (H: L) = C (H: 0) + C (0: L) = C (H) + C (L) M ( H: L = M (H: 0) + M (0: L) 8C (H) + M (L)   

8C (H), only those C (H ) Is being transferred to 8 locations equal to bits when we calculate M (H) instead of M (H).

Non-recurring, if your input is byte x0, x1, x2, x3 ...

  c = x (c) X0) + C2 (x2) + C3x + ... m (x0) + m (x1) + m (x2) + m (x3) + ... + 8C (x1) + 16C (x2) + 24C (x3) + ...   

And then you can pass the average M and C on all the lines.

Comments