Advertisement
dan-masek

Untitled

Apr 26th, 2018
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # Minimum percentage of pixels of same hue to consider dominant colour
  5. MIN_PIXEL_CNT_PCT = (1.0/20.0)
  6.  
  7. image = cv2.imread('colourblobs.png')
  8. image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  9. h,_,_ = cv2.split(image_hsv)
  10. bins = np.bincount(h.flatten())
  11. peaks = np.where(bins > (h.size * MIN_PIXEL_CNT_PCT))[0]
  12.  
  13. for i,peak in enumerate(peaks):
  14.     mask = cv2.inRange(h, peak, peak)
  15.     blob = cv2.bitwise_and(image, image, mask=mask)
  16.     cv2.imwrite("colourblobs-%d-hue_%03d.png" % (i, peak), blob)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement