Guest User

Untitled

a guest
Sep 12th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. # Import helper functions
  5. from Utility import *
  6. helperFunction = Utility()
  7.  
  8.  
  9. # Create a camera object
  10. captureImage = cv2.VideoCapture(0)
  11.  
  12. # Resize the camera resolution to 640x480px (480p) for faster processing time
  13. helperFunction.resizeWindow(captureImage, 640, 480)
  14.  
  15. # Create HSV filter
  16. HSVLow = np.array([15, 105, 216])
  17. HSVHigh = np.array([44, 250, 255])
  18.  
  19. while True:
  20. # Read the captured frames
  21. result, frame = captureImage.read()
  22.  
  23. # Apply a blur to the frame to reduce some of the sharpnesse
  24. frame = cv2.GaussianBlur(frame,(5,5),0)
  25.  
  26. # TODO - Image Processing Steps Go Here
  27. # YOUR CODE
  28. convertedImageToHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  29.  
  30. # Create the filter mask
  31. mask = cv2.inRange(convertedImageToHSV, HSVLow, HSVHigh)
  32.  
  33. # Morphological Operation
  34. # Create a block of filled with black pixel.
  35. kernelOpen = np.ones((5,5), np.uint8)
  36. kernelClose = np.ones((20,20), np.uint8)
  37.  
  38. maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen)
  39. maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose)
  40.  
  41. # Set Primary Filter
  42. primaryFilter = maskClose
  43.  
  44. # Draw Contour around filter object
  45. image, contours, h = cv2.findContours(primaryFilter.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  46. cv2.drawContours(frame, contours, -1, (255,0,0), 3)
  47.  
  48. for i in range(len(contours)):
  49. x,y,w,h=cv2.boundingRect(contours[i])
  50. cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255), 2)
  51. cv2.putText(frame, str(i+1), (x,y+h), cv2.FONT_HERSHEY_SIMPLEX, 1.9, (0,255,255))
  52.  
  53. # Show the image frame in a window name "Video Feed"
  54. cv2.imshow("RGB Original Video Feed", frame)
  55. cv2.imshow("HSV Feed", convertedImageToHSV)
  56. cv2.imshow("Color Mask Feed", mask)
  57. cv2.imshow("Morphological Closing", maskClose)
  58. cv2.imshow("Morphological Opening", maskOpen)
  59.  
  60. # This help us exist the video when hit Esc key
  61. if cv2.waitKey(10) == 27:
  62. break
  63.  
  64. # Clean up procedure to prevent memory leaking
  65. cv2.destroyAllWindows
  66. captureImage.release()
Add Comment
Please, Sign In to add comment