forked from amueller/word_cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_integral_image.pyx
33 lines (30 loc) · 1.1 KB
/
query_integral_image.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# cython: boundscheck=False
# cython: wraparound=False
import array
import numpy as np
def query_integral_image(unsigned int[:,:] integral_image, int size_x, int size_y):
cdef int x = integral_image.shape[0]
cdef int y = integral_image.shape[1]
cdef int area, i, j
cdef int hits = 0
# count how many possible locations
for i in xrange(x - size_x):
for j in xrange(y - size_y):
area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
if not area:
hits += 1
if not hits:
# no room left
return None
# pick a location at random
cdef int goal = np.random.randint(hits)
hits = 0
for i in xrange(x - size_x):
for j in xrange(y - size_y):
area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
if not area:
hits += 1
if hits == goal:
return i, j