Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. #function to get RGB image from kinect
  5. def get_video():
  6. array,_ = freenect.sync_get_video()
  7. array = cv2.cvtColor(array,cv2.COLOR_RGB2BGR)
  8. return array
  9.  
  10. def preprocess(array):
  11. array = cv2.cvtColor(array,cv2.COLOR_BGR2HSV)
  12. array = cv2.resize(array, (0,0), fx=0.5, fy=0.5)
  13. kernel = np.ones((7,7),np.uint8)
  14. #array[:,:,2] = cv2.equalizeHist(array[:,:,2])
  15. #array = cv2.blur(array, (3,3), 5)
  16. array = cv2.erode(array,kernel, 10)
  17. array = cv2.dilate(array,kernel, 15)
  18. return array
  19.  
  20. def get_mask(img):
  21. array = preprocess(img)
  22. mask = np.ones_like(array[:,:,0])
  23. h = array[:,:,0]
  24. s = array[:,:,1]
  25. v = array[:,:,2]
  26. mask[(h > 30)] = 0
  27. mask[(h < 18)] = 0
  28. mask[(v < 90)] = 0
  29. mask[(s < 85)] = 0
  30. return mask
  31.  
  32. if __name__ == "__main__":
  33.  
  34. cap = cv2.VideoCapture(0)
  35. while 1:
  36. # Capture frame-by-frame
  37. brightness=50
  38. cap.set(cv2.CAP_PROP_AUTO_EXPOSURE, 10000)
  39.  
  40.  
  41. ret, frame = cap.read()
  42.  
  43. #get preprocessed image
  44. preprocessed = cv2.cvtColor(preprocess(frame),cv2.COLOR_HSV2BGR)
  45. # get the mask for areas that pass the color thresholds
  46. mask = get_mask(frame)
  47.  
  48. #display RGB image
  49. cv2.imshow('RGB image',frame)
  50. #display the mask
  51. cv2.imshow('Mask', mask*255)
  52. # quit program when 'esc' key is pressed
  53. k = cv2.waitKey(5) & 0xFF
  54. if k == 27:
  55. break
  56. cv2.destroyAllWindows()
Add Comment
Please, Sign In to add comment