Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- # Import helper functions
- from Utility import *
- helperFunction = Utility()
- # Create a camera object
- captureImage = cv2.VideoCapture(0)
- # Resize the camera resolution to 640x480px (480p) for faster processing time
- helperFunction.resizeWindow(captureImage, 640, 480)
- # Create HSV filter
- HSVLow = np.array([15, 105, 216])
- HSVHigh = np.array([44, 250, 255])
- while True:
- # Read the captured frames
- result, frame = captureImage.read()
- # Apply a blur to the frame to reduce some of the sharpnesse
- frame = cv2.GaussianBlur(frame,(5,5),0)
- # TODO - Image Processing Steps Go Here
- # YOUR CODE
- convertedImageToHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
- # Create the filter mask
- mask = cv2.inRange(convertedImageToHSV, HSVLow, HSVHigh)
- # Morphological Operation
- # Create a block of filled with black pixel.
- kernelOpen = np.ones((5,5), np.uint8)
- kernelClose = np.ones((20,20), np.uint8)
- maskOpen = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernelOpen)
- maskClose = cv2.morphologyEx(maskOpen, cv2.MORPH_CLOSE, kernelClose)
- # Set Primary Filter
- primaryFilter = maskClose
- # Draw Contour around filter object
- image, contours, h = cv2.findContours(primaryFilter.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- cv2.drawContours(frame, contours, -1, (255,0,0), 3)
- for i in range(len(contours)):
- x,y,w,h=cv2.boundingRect(contours[i])
- cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255), 2)
- cv2.putText(frame, str(i+1), (x,y+h), cv2.FONT_HERSHEY_SIMPLEX, 1.9, (0,255,255))
- # Show the image frame in a window name "Video Feed"
- cv2.imshow("RGB Original Video Feed", frame)
- cv2.imshow("HSV Feed", convertedImageToHSV)
- cv2.imshow("Color Mask Feed", mask)
- cv2.imshow("Morphological Closing", maskClose)
- cv2.imshow("Morphological Opening", maskOpen)
- # This help us exist the video when hit Esc key
- if cv2.waitKey(10) == 27:
- break
- # Clean up procedure to prevent memory leaking
- cv2.destroyAllWindows
- captureImage.release()
Add Comment
Please, Sign In to add comment