You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

  1. import numpy as np
  2. from PIL import Image
  3. import matplotlib.pyplot as plt
  4. import os
  5. from skimage import io
  6. from skimage import color
  7. from skimage import filters
  8. def imgFileToData(path):
  9. image = Image.open(path)
  10. image_data = np.asarray(image)
  11. return image_data
  12. def imgFileToData2(path):
  13. img = io.imread(path)
  14. return img
  15. def detectObj(image_data):
  16. #image_data_blue = image_data[:,:,2]
  17. image_data_blue = color.rgb2grey(image_data)
  18. #image_data_blue = threshold(image_data)
  19. median_blue = np.median(image_data_blue)
  20. print median_blue
  21. median_blue = median_blue - median_blue/1.5
  22. print median_blue
  23. print image_data_blue
  24. non_empty_columns = np.where(image_data_blue.min(axis=0)<median_blue)[0]
  25. non_empty_rows = np.where(image_data_blue.min(axis=1)<median_blue)[0]
  26. boundingBox = (min(non_empty_rows), max(non_empty_rows), min(non_empty_columns), max(non_empty_columns))
  27. print boundingBox
  28. return boundingBox
  29. def threshold(img):
  30. #img = color.rgb2grey(img)
  31. #img = img[:,:,2]
  32. img = color.rgb2grey(img)
  33. thresh = filters.threshold_mean(img)
  34. binary = img > thresh
  35. return binary
  36. def prova(img):
  37. #return color.rgb2grey(img)
  38. return img
  39. def crop(image_data, box):
  40. return image_data[box[0]:box[1], box[2]:box[3]]
  41. def saveDataToImageFile(data, filename):
  42. image = Image.fromarray(data)
  43. image.save(filename)