Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #import cv2 for vision
  2. #numpy to generate backgrounds
  3. import cv2
  4. import numpy as np
  5.  
  6. #capture video from the webcam
  7. cap = cv2.VideoCapture(0)
  8. #read one frame
  9. _, img = cap.read()
  10.  
  11. #define two points, used to make a mask
  12. x1 = 100
  13. y1 = 100
  14. x2 = 200
  15. y2 = 200
  16. # create a mask
  17. mask = np.zeros(img.shape[:2], np.uint8)
  18. mask[y1:y2, x1:x2] = 255
  19.  
  20. #define the height of the graph
  21. graph_height = 200
  22. graph_length = 640
  23.  
  24. #main loop of
  25. while True:
  26.  
  27. #read image from the webcam
  28. _, img = cap.read()
  29.  
  30. #convert image to hsv color space
  31. img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  32.  
  33. #calculate the histogram for the hue value in the masked area
  34. hist_mask = cv2.calcHist([img_hsv], [0], mask, [255], [0,256])
  35.  
  36. #create a mask of the image
  37. masked_img = cv2.bitwise_and(img,img,mask = mask)
  38.  
  39. #create gray background for graph
  40. graph = np.zeros((graph_height,graph_length,3),np.uint8)
  41. graph[::] = (123,123,123)
  42.  
  43. #define values for graphing for graphing
  44. x_value = 0
  45. previous_x = 0
  46. previous_y = 640
  47. #scale the histogram
  48. hist_scaled = hist_mask*(graph.shape[0]/max(hist_mask))
  49.  
  50. #make graph
  51. for y_value in hist_scaled:
  52.  
  53. #tedious method to convert values to get color
  54. test = np.zeros((1,1,3), np.uint8)
  55. test[::] = int(x_value/3.0), 255,255
  56. test = cv2.cvtColor(test, cv2.COLOR_HSV2BGR)
  57. cvc = int(test[0][0][0]), int(test[0][0][1]), int(test[0][0][2])
  58.  
  59. #spacing issues
  60. x_value+=3
  61. y_value = graph_height-y_value
  62.  
  63. #graph
  64. cv2.line(graph, (x_value, y_value),
  65. (previous_x, previous_y), cvc, 8)
  66.  
  67. #update previous values
  68. previous_x = x_value
  69. previous_y = y_value
  70.  
  71. #put a rectangle on the image to show where the mask
  72. cv2.rectangle(img,(x1,y1), (x2,y2), (123,123,123), 2)
  73.  
  74. #show image
  75. cv2.imshow('img', img)
  76. cv2.imshow('graph', graph)
  77. cv2.waitKey(1)
  78.  
  79. cv2.destroyAllWindows()
  80. cap.release()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement