Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. import cv2
  2. video=cv2.VideoCapture(0)
  3. first_frame=None
  4.  
  5. while True:
  6.     check,frame=video.read()
  7.     gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
  8.     gray=cv2.GaussianBlur(gray,(21,21),0)
  9.    
  10.     if first_frame is None:
  11.         first_frame=gray
  12.         continue
  13.  
  14.     delta_frame=cv2.absdiff(first_frame,gray)
  15.     thresh_frame=cv2.threshold(delta_frame,30,255,cv2.THRESH_BINARY)[1]
  16.     thresh_frame=cv2.dilate(thresh_frame,None,iterations=2)
  17.  
  18.  
  19.     (_,cnts,_)=cv2.findContours(thresh_frame.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
  20.  
  21.     for contour in cnts:
  22.         if cv2.contourArea(contour) < 1000:
  23.             continue
  24.  
  25.         (x,y,w,h)=cv2.boundingRect(contour)
  26.         cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,0),4)
  27.  
  28.  
  29.     cv2.imshow("thresh",thresh_frame)
  30.     cv2.imshow("delta frame",delta_frame)
  31.     cv2.imshow("gray",gray)
  32.     cv2.imshow("frame",frame)
  33.     key=cv2.waitKey(1)
  34.     if key ==ord("q"):
  35.         break
  36.  
  37. video.release()
  38. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement