Debian Codegraph @

wordcloud/query_integral_image.pyx

raw
1 # cython: language_level=3
2 # cython: boundscheck=False
3 # cython: wraparound=False
4 import array
5 import numpy as np
6
7
8 def query_integral_image(unsigned int[:,:] integral_image, int size_x, int
9 size_y, random_state):
10 cdef int x = integral_image.shape[0]
11 cdef int y = integral_image.shape[1]
12 cdef int area, i, j
13 cdef int hits = 0
14
15 # count how many possible locations
16 for i in xrange(x - size_x):
17 for j in xrange(y - size_y):
18 area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
19 area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
20 if not area:
21 hits += 1
22 if not hits:
23 # no room left
24 return None
25 # pick a location at random
26 cdef int goal = random_state.randint(0, hits)
27 hits = 0
28 for i in xrange(x - size_x):
29 for j in xrange(y - size_y):
30 area = integral_image[i, j] + integral_image[i + size_x, j + size_y]
31 area -= integral_image[i + size_x, j] + integral_image[i, j + size_y]
32 if not area:
33 hits += 1
34 if hits == goal:
35 return i, j