Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- cimport numpy as np
- cimport cython
- @cython.boundscheck(False)
- @cython.wraparound(False)
- @cython.cdivision(True)
- def test_v11(np.ndarray[np.uint8_t,ndim=3] frame
- , np.ndarray[np.int32_t,ndim=2] labels
- , np.ndarray[np.float64_t,ndim=1] counts):
- cdef int num_labels = counts.shape[0]
- cdef np.ndarray[np.int64_t,ndim=1] green_sums = np.zeros([num_labels], np.int64)
- cdef np.ndarray[np.int64_t,ndim=1] red_sums = np.zeros([num_labels], np.int64)
- cdef np.ndarray[np.int32_t,ndim=1] flat_labels = labels.ravel()
- cdef np.ndarray[np.uint8_t,ndim=1] flat_pixels = frame.ravel()
- cdef np.int32_t[::1] flat_labels_view = flat_labels
- cdef np.uint8_t[::1] flat_pixels_view = flat_pixels
- cdef np.int64_t[::1] green_sums_view = green_sums
- cdef np.int64_t[::1] red_sums_view = red_sums
- cdef size_t pixel_count = flat_labels.shape[0]
- cdef size_t j = 0
- cdef size_t i
- cdef int label
- for i in range(pixel_count):
- label = flat_labels_view[i]
- if label == 0:
- j += 3
- continue
- j += 1
- green_sums_view[label] += flat_pixels_view[j]
- j += 1
- red_sums_view[label] += flat_pixels_view[j]
- j += 1
- cdef np.ndarray[np.float64_t,ndim=1] result = np.zeros((num_labels - 1,), np.float64)
- cdef np.float64_t count
- for i in range(1, num_labels):
- count = counts[i]
- result[i-1] = red_sums_view[i] / count - green_sums_view[i] / count
- return result.tolist()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement