├── All_Methods.py ├── b_s.py ├── edge.py ├── frame_diff.py └── optical_flow.py /All_Methods.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | cap = cv2.VideoCapture("/media/suresh_arunachalam/user/Project/python_coding/Drone_Detection.mp4") 4 | fgbg = cv2.createBackgroundSubtractorMOG2() 5 | ret, current_frame = cap.read() 6 | previous_frame = current_frame 7 | ret, frame1 = cap.read() 8 | prvs = cv2.cvtColor(frame1,cv2.COLOR_BGR2GRAY) 9 | hsv = np.zeros_like(frame1) 10 | hsv[...,1] = 255 11 | while(1): 12 | ret, frame = cap.read() 13 | fgmask = fgbg.apply(frame) 14 | # cv2.resizeWindow("Background Subtraction", 500, 500) 15 | cv2.imshow('Background Subtraction',fgmask) 16 | current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) 17 | previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY) 18 | frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray) 19 | #cv2.resizeWindow("Original", 500, 500) 20 | cv2.imshow('Original',current_frame) 21 | # cv2.resizeWindow("Frame Difference", 500, 500) 22 | cv2.imshow('Frame Difference',frame_diff) 23 | ret, frame2 = cap.read() 24 | next = cv2.cvtColor(frame2,cv2.COLOR_BGR2GRAY) 25 | flow = cv2.calcOpticalFlowFarneback(prvs,next, None, 0.5, 3, 15, 3, 5, 1.2, 0) 26 | mag, ang = cv2.cartToPolar(flow[...,0], flow[...,1]) 27 | hsv[...,0] = ang*180/np.pi/2 28 | hsv[...,2] = cv2.normalize(mag,None,0,255,cv2.NORM_MINMAX) 29 | bgr = cv2.cvtColor(hsv,cv2.COLOR_HSV2BGR) 30 | # cv2.imshow('Dense Optical Flow',bgr) 31 | 32 | frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 33 | blurred_frame = cv2.GaussianBlur(frame, (5, 5), 0) 34 | 35 | laplacian = cv2.Laplacian(blurred_frame, cv2.CV_64F) 36 | canny = cv2.Canny(blurred_frame, 100, 150) 37 | 38 | 39 | # cv2.imshow("Laplacian", laplacian) 40 | # cv2.resizeWindow("Canny", 500, 500) 41 | cv2.imshow("Canny", canny) 42 | k = cv2.waitKey(30) & 0xff 43 | if k == 27: 44 | break 45 | elif k == ord('s'): 46 | cv2.imwrite('opticalfb.png',frame2) 47 | cv2.imwrite('opticalhsv.png',bgr) 48 | prvs = next 49 | previous_frame = current_frame.copy() 50 | ret, current_frame = cap.read() 51 | cap.release() 52 | cv2.destroyAllWindows() 53 | -------------------------------------------------------------------------------- /b_s.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | scaling_factorx=0.5 4 | scaling_factory=0.5 5 | cap = cv2.VideoCapture(0) 6 | fgbg = cv2.createBackgroundSubtractorMOG2() 7 | 8 | while(1): 9 | ret, frame = cap.read() # ret = 1 if the video is captured; frame is the image 10 | frame=cv2.resize(frame,None,fx=scaling_factorx,fy=scaling_factory,interpolation=cv2.INTER_AREA) 11 | 12 | fgmask = fgbg.apply(frame) 13 | cv2.imshow('fgmask',frame) 14 | cv2.imshow('frame',fgmask) 15 | 16 | 17 | k = cv2.waitKey(30) & 0xff 18 | if k == 27: 19 | break 20 | 21 | 22 | cap.release() 23 | cv2.destroyAllWindows() 24 | -------------------------------------------------------------------------------- /edge.py: -------------------------------------------------------------------------------- 1 | # OpenCV program to perform Edge detection in real time 2 | # import libraries of python OpenCV 3 | # where its functionality resides 4 | import cv2 5 | 6 | # np is an alias pointing to numpy library 7 | import numpy as np 8 | 9 | 10 | # capture frames from a camera 11 | cap = cv2.VideoCapture(0) 12 | 13 | 14 | # loop runs if capturing has been initialized 15 | while(1): 16 | 17 | # reads frames from a camera 18 | ret, frame = cap.read() 19 | 20 | # converting BGR to HSV 21 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 22 | 23 | # define range of red color in HSV 24 | lower_red = np.array([30,150,50]) 25 | upper_red = np.array([255,255,180]) 26 | 27 | # create a red HSV colour boundary and 28 | # threshold HSV image 29 | mask = cv2.inRange(hsv, lower_red, upper_red) 30 | 31 | # Bitwise-AND mask and original image 32 | res = cv2.bitwise_and(frame,frame, mask= mask) 33 | 34 | # Display an original image 35 | cv2.imshow('Original',frame) 36 | 37 | # finds edges in the input image image and 38 | # marks them in the output map edges 39 | edges = cv2.Canny(frame,100,200) 40 | 41 | # Display edges in a frame 42 | cv2.imshow('Edges',edges) 43 | 44 | # Wait for Esc key to stop # add 45 | k = cv2.waitKey(5) & 0xFF 46 | if k == 27: 47 | break 48 | 49 | 50 | # Close the window 51 | cap.release() 52 | 53 | # De-allocate any associated memory usage 54 | cv2.destroyAllWindows() 55 | 56 | -------------------------------------------------------------------------------- /frame_diff.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | cap = cv2.VideoCapture("C:/Users/SURESH ARUNACHALAM/Desktop/Project/python_coding/a.mp4") 4 | ret, current_frame = cap.read() 5 | previous_frame = current_frame 6 | 7 | while(cap.isOpened()): 8 | current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) 9 | previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY) 10 | 11 | frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray) 12 | cv2.imshow('fgmask',current_frame) 13 | 14 | cv2.imshow('frame diff ',frame_diff) 15 | 16 | #cv2.moveWindow(cap, 40,30) 17 | if cv2.waitKey(1) & 0xFF == ord('q'): 18 | break 19 | 20 | previous_frame = current_frame.copy() 21 | ret, current_frame = cap.read() 22 | 23 | cap.release() 24 | cv2.destroyAllWindows() 25 | -------------------------------------------------------------------------------- /optical_flow.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import cv2 3 | cap = cv2.VideoCapture('C:/Users/SURESH ARUNACHALAM/Desktop/Python/video.avi') 4 | # params for ShiTomasi corner detection 5 | feature_params = dict( maxCorners = 100, 6 | qualityLevel = 0.3, 7 | minDistance = 7, 8 | blockSize = 7 ) 9 | # Parameters for lucas kanade optical flow 10 | lk_params = dict( winSize = (15,15), 11 | maxLevel = 2, 12 | criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 13 | # Create some random colors 14 | color = np.random.randint(0,255,(100,3)) 15 | # Take first frame and find corners in it 16 | ret, old_frame = cap.read() 17 | old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) 18 | p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params) 19 | # Create a mask image for drawing purposes 20 | mask = np.zeros_like(old_frame) 21 | while(1): 22 | ret,frame = cap.read() 23 | frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 24 | # calculate optical flow 25 | p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) 26 | # Select good points 27 | good_new = p1[st==1] 28 | good_old = p0[st==1] 29 | # draw the tracks 30 | for i,(new,old) in enumerate(zip(good_new,good_old)): 31 | a,b = new.ravel() 32 | c,d = old.ravel() 33 | mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) 34 | frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) 35 | img = cv2.add(frame,mask) 36 | cv2.imshow('frame',img) 37 | k = cv2.waitKey(30) & 0xff 38 | if k == 27: 39 | break 40 | # Now update the previous frame and previous points 41 | old_gray = frame_gray.copy() 42 | p0 = good_new.reshape(-1,1,2) 43 | cv2.destroyAllWindows() 44 | cap.release() 45 | --------------------------------------------------------------------------------