Skip to content

Instantly share code, notes, and snippets.

@deroneriksson
Last active December 15, 2017 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deroneriksson/3ef56e0cc84b3d313be5e287fee0bd7b to your computer and use it in GitHub Desktop.
Save deroneriksson/3ef56e0cc84b3d313be5e287fee0bd7b to your computer and use it in GitHub Desktop.

Python Stuff

NumPy array - print without cutoffs

from scipy.ndimage import measurements
im = array(...)
labels, num_features = measurements.label(im)
set_printoptions(threshold=nan, linewidth=nan)
print(str(labels[0:100,0:100]))

NumPy array - compute labeled feature sizes

# includes 0 count
counts = bincount(ravel(labels))
print(counts)

Separate and combine RGB channels in NumPy array

r = rgb[:, :, 0]
g = rgb[:, :, 1]
b = rgb[:, :, 2]
# do something
rgb2 = np.dstack((r, g, b))

Incomplete - Purple shade filter

def filter_blah(rgb, thresh, tolerance=70):
  rgb_int = rgb.astype(np.int)
  (r_thresh, g_thresh, b_thresh) = thresh
  r = (abs(rgb_int[:, :, 0] - r_thresh) <= tolerance)
  g = (abs(rgb_int[:, :, 1] - g_thresh) <= tolerance)
  b = (abs(rgb_int[:, :, 2] - b_thresh) <= tolerance)
  result = (r & g & b)
  return result

# result = filter_blah(rgb, (230, 230, 250)) | \
#          filter_blah(rgb, (216, 191, 216)) | \ Y
#          filter_blah(rgb, (221, 160, 221)) | \
#          filter_blah(rgb, (238, 130, 238)) | \
#          filter_blah(rgb, (218, 112, 214)) | \
#          filter_blah(rgb, (255, 0, 255)) | \
#          filter_blah(rgb, (186, 85, 211)) | \
#          filter_blah(rgb, (147, 112, 219)) | \
#          filter_blah(rgb, (138, 43, 226)) | \
#          filter_blah(rgb, (148, 0, 211)) | \
#          filter_blah(rgb, (153, 50, 204)) | \
#          filter_blah(rgb, (139, 0, 139)) | \
#          filter_blah(rgb, (128, 0, 128)) | \
#          filter_blah(rgb, (75, 0, 130))

result = filter_blah(rgb, (218, 112, 214))

add_text_and_display(mask_rgb(rgb, result), "Color Threshold")

Create 2D NumPy Array

a = np.array([[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]])
print(str(a))
b = np.arange(1,26).reshape(5,5)
print(str(b))

gives

[[ 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]]
[[ 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]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment