Task: Centroid Bitmap

I would like to calculate the centroid of a bitmap (black and white) stored as a bitmap of integers. I know that there are fast algorithms for counting the number of bits of a set into an integer, but this does not help me calculate the centroid. Any ideas?

As an example, if my bitmap looks like this:

111000
111000
111000
000000
000000
000000

The centroid is 1, 1. Packed in 32 bit integers (you choose Endian), it might look like this: {width: 6, height: 6} {3817734144, 0}.

Bonus points if you can also get mass (9 in the example) without repeating each bit.

+3
source share
1 answer

, . ( , , x y ).

, b i, b i f (i) f. f (i) = 1, ( C), f (i) = i, M ( C, ).

8 C M, 256 . 8 h: l, l - 8 , h - .

C(h:l) = C(h:0) + C(0:l) = C(h) + C(l)
M(h:l) = M(h:0) + M(0:l) = M(h) + 8C(h) + M(l)

8C (h), , C (h) 8 , M (h) M (h: 0).

, x0, x1, x2, x3...

C(x) = C(x0) + C(x1) +   C(x2) +   C(x3) + ...
M(x) = M(x0) + M(x1) +   M(x2) +   M(x3) + ...
             +8C(x1) + 16C(x2) + 24C(x3) + ...

M C .

+2

All Articles