SHARE
TWEET

Untitled

a guest Mar 24th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import cv2
  3. import time
  4. cap = cv2.VideoCapture(0)
  5. lower_red = np.array([0,100,100])
  6. upper_red = np.array([20,255,255])
  7.  
  8.  
  9. while(1):
  10.  
  11.     start = time.time()
  12.  
  13.     # Load an color image in grayscale
  14.     _,img = cap.read()
  15.     print("read")
  16.  
  17.     #get the image's width and height
  18.  
  19.     #cv2.imshow('ORIGINAL IMAGE',img)
  20.  
  21.     blurred = cv2.GaussianBlur(img, (11, 11), 0)
  22.     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
  23.  
  24.     hsv2 = cv2.cvtColor(blurred,cv2.COLOR_BGR2HSV)
  25.  
  26.     mask2 = cv2.inRange(hsv2, lower_red, upper_red)
  27.     mask2 = cv2.erode(mask2, None, iterations=2)
  28.     mask2 = cv2.dilate(mask2, None, iterations=2)
  29.  
  30.     #circles = cv2.HoughCircles(mask2, cv2.HOUGH_GRADIENT, 0.9, 1, param1=50, param2=30, minRadius=1, maxRadius=200)
  31.     #print("circles= ",circles)
  32.     #inal_num = 0
  33.     #final_sum = 0
  34.  
  35.     imageWidth = mask2.shape[1]
  36.     imageHeight = mask2.shape[0]
  37.  
  38.     sum_i_white = 1
  39.     num_white = 1
  40.     sum_j_white = 1
  41.     for i in range(0,imageHeight):
  42.         for j in range(0,imageWidth):
  43.             if mask2[i][j] == 255:
  44.                 sum_i_white += i
  45.                 sum_j_white += j
  46.                 num_white += 1
  47.  
  48.     #print("after loop",sum_i_white,sum_j_white,num_white)
  49.     #print("so X = ",sum_i_white/num_white)
  50.     #print("so Y = ",sum_j_white/num_white)
  51.     cv2.circle(img,(int(sum_j_white/num_white),int(sum_i_white/num_white)),3,(0,0,255), -1)
  52.     cv2.putText(img,'here',(int(sum_j_white/num_white),int(sum_i_white/num_white)), cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 1, cv2.LINE_AA)
  53.     cv2.imshow("final",img)
  54.     print(time.time()-start)
  55.     if cv2.waitKey(1) & 0xFF == ord('q'):
  56.         break
  57.    
  58. cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top