utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import cv2
  2. import numpy as np
  3. import imutils
  4. def saturation(img):
  5. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  6. return hsv[:, :, 1]
  7. def mean_image(images):
  8. mean = np.zeros(images[0].shape)
  9. for image in images:
  10. mean += image/len(images)
  11. return mean.astype(np.uint8)
  12. # If percentage should be linked to a smaller region than whole image, fill img_ratio with factor value
  13. def mean_percent_value(img, img_ratio=1.0):
  14. return np.sum(img, dtype=np.int64) / img_ratio / img.size / 255 * 100
  15. def find_food(img, min_food_size, lower_color_boundary, upper_color_boundary, kernel=None):
  16. img = img.copy()
  17. lower = np.array(lower_color_boundary, dtype="uint8")
  18. upper = np.array(upper_color_boundary, dtype="uint8")
  19. mask = cv2.inRange(img, lower, upper)
  20. if kernel is None:
  21. kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (int(min_food_size/2), int(min_food_size/2)))
  22. cleaned = mask
  23. cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_CLOSE, kernel)
  24. cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_OPEN, kernel)
  25. cnts = cv2.findContours(cleaned.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  26. cnts = imutils.grab_contours(cnts)
  27. foods = []
  28. mask = np.zeros(img.shape[0:2], np.uint8)
  29. for c in cnts:
  30. (x, y, w, h) = cv2.boundingRect(c)
  31. if w >= min_food_size and h >= min_food_size:
  32. foods.append((x, y, w, h))
  33. cv2.drawContours(mask, [c], -1, 255, cv2.FILLED)
  34. cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 10)
  35. return foods, mask, img
  36. # Set area_ratio to 0 if you want to have the maximum possible blobs.
  37. # Otherwise adding of blobs is break when the next blob to add is smaller than the first blob times the area_ratio
  38. def find_blob(sat_img, max_blob=1, area_ratio=0.8, kernel=None):
  39. blur = cv2.GaussianBlur(sat_img, (5, 5), 0)
  40. thresh = cv2.threshold(blur, 0, 255,
  41. cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
  42. if kernel is None:
  43. kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))
  44. cleaned = thresh
  45. cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_CLOSE, kernel)
  46. cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_OPEN, kernel)
  47. contours, hierarchy = cv2.findContours(cleaned, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
  48. blobs = []
  49. while len(contours) != 0 and len(blobs) < max_blob:
  50. c = max(contours, key=cv2.contourArea)
  51. if len(blobs) != 0 and cv2.contourArea(blobs[0]) * area_ratio >= cv2.contourArea(c):
  52. break
  53. blobs.append(c)
  54. contours.remove(c)
  55. mask = np.zeros(sat_img.shape, np.uint8)
  56. cv2.drawContours(mask, blobs, -1, 255, cv2.FILLED)
  57. kept_cleaned = cv2.bitwise_and(cleaned, cleaned, mask=mask)
  58. return kept_cleaned