Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. blue_lower = np.array([110,50,50])
  5. blue_upper = np.array([130,255,255])
  6.  
  7.  
  8.  
  9.  
  10. class get:
  11.  
  12.  
  13. def __init__(self, source):
  14.  
  15. self.counter = 0
  16.  
  17. self.cap = cv2.VideoCapture(source)
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. def blocks_between_hue(self, lower_value, upper_value):
  25. _, img = self.cap.read()
  26. # Convert image type to HSV
  27. hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  28. # Add some blur to reduce noise
  29. # blur = cv2.GaussianBlur(hsv, (11, 11), 0)
  30. # Look for colours in that hue range
  31. block = cv2.inRange(hsv, lower_value, upper_value)
  32. # Get rid of some noise
  33. mask = cv2.erode(block, None, iterations=2)
  34. # Find the contours of the blob so we can draw a rectangle around it
  35. contours, hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
  36.  
  37. for pic, contour in enumerate(contours):
  38. area = cv2.contourArea(contour)
  39. if(area>300):
  40. x,y,w,h = cv2.boundingRect(contour)
  41. im2 = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),3)
  42. return(x,y,w,h)
  43.  
  44.  
  45.  
  46. get = get(0)
  47. while True:
  48. detect_block = get.blocks_between_hue(blue_lower, blue_upper)
  49. print(detect_block)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement