| 66 | |
| 67 | |
| 68 | def unique(ar, return_index=False, return_inverse=False, return_counts=False): |
| 69 | ar = np.asanyarray(ar).flatten() |
| 70 | |
| 71 | optional_indices = return_index or return_inverse |
| 72 | optional_returns = optional_indices or return_counts |
| 73 | |
| 74 | if ar.size == 0: |
| 75 | if not optional_returns: |
| 76 | ret = ar |
| 77 | else: |
| 78 | ret = (ar,) |
| 79 | if return_index: |
| 80 | ret += (np.empty(0, np.bool),) |
| 81 | if return_inverse: |
| 82 | ret += (np.empty(0, np.bool),) |
| 83 | if return_counts: |
| 84 | ret += (np.empty(0, np.intp),) |
| 85 | return ret |
| 86 | if optional_indices: |
| 87 | perm = ar.argsort(kind='mergesort' if return_index else 'quicksort') |
| 88 | aux = ar[perm] |
| 89 | else: |
| 90 | ar.sort() |
| 91 | aux = ar |
| 92 | flag = np.concatenate(([True], aux[1:] != aux[:-1])) |
| 93 | |
| 94 | if not optional_returns: |
| 95 | ret = aux[flag] |
| 96 | else: |
| 97 | ret = (aux[flag],) |
| 98 | if return_index: |
| 99 | ret += (perm[flag],) |
| 100 | if return_inverse: |
| 101 | iflag = np.cumsum(flag) - 1 |
| 102 | inv_idx = np.empty(ar.shape, dtype=np.intp) |
| 103 | inv_idx[perm] = iflag |
| 104 | ret += (inv_idx,) |
| 105 | if return_counts: |
| 106 | idx = np.concatenate(np.nonzero(flag) + ([ar.size],)) |
| 107 | ret += (np.diff(idx),) |
| 108 | return ret |
| 109 | |
| 110 | |
| 111 | def colorEncode(labelmap, colors, mode='RGB'): |