Advertisement
dan-masek

Untitled

Jan 3rd, 2020
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. import numpy as np
  2. cimport numpy as np
  3. cimport cython
  4.  
  5. @cython.boundscheck(False)
  6. @cython.wraparound(False)
  7. @cython.cdivision(True)
  8. def test_v11(np.ndarray[np.uint8_t,ndim=3] frame
  9.     , np.ndarray[np.int32_t,ndim=2] labels
  10.     , np.ndarray[np.float64_t,ndim=1] counts):
  11.     cdef int num_labels = counts.shape[0]
  12.     cdef np.ndarray[np.int64_t,ndim=1] green_sums = np.zeros([num_labels], np.int64)
  13.     cdef np.ndarray[np.int64_t,ndim=1] red_sums = np.zeros([num_labels], np.int64)
  14.    
  15.     cdef np.ndarray[np.int32_t,ndim=1] flat_labels = labels.ravel()
  16.     cdef np.ndarray[np.uint8_t,ndim=1] flat_pixels = frame.ravel()
  17.    
  18.     cdef np.int32_t[::1] flat_labels_view = flat_labels
  19.     cdef np.uint8_t[::1] flat_pixels_view = flat_pixels
  20.     cdef np.int64_t[::1] green_sums_view = green_sums
  21.     cdef np.int64_t[::1] red_sums_view = red_sums
  22.    
  23.     cdef size_t pixel_count = flat_labels.shape[0]
  24.     cdef size_t j = 0
  25.     cdef size_t i
  26.     cdef int label
  27.     for i in range(pixel_count):
  28.         label = flat_labels_view[i]
  29.         if label == 0:
  30.             j += 3
  31.             continue
  32.         j += 1
  33.         green_sums_view[label] += flat_pixels_view[j]
  34.         j += 1
  35.         red_sums_view[label] += flat_pixels_view[j]
  36.         j += 1
  37.  
  38.     cdef np.ndarray[np.float64_t,ndim=1] result = np.zeros((num_labels - 1,), np.float64)
  39.     cdef np.float64_t count
  40.     for i in range(1, num_labels):
  41.         count = counts[i]
  42.         result[i-1] = red_sums_view[i] / count - green_sums_view[i] / count
  43.  
  44.     return result.tolist()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement