장규범
import cv2
import re
import os
import matplotlib.pyplot as plt
import numpy as np
path = "C:/Datasets/vid15"
count = 0
img_data = []
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('15.avi',fourcc, 10, (270,450))
dir_list = os.listdir(path)
dir_list.sort(key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)])
for img in dir_list:
img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_COLOR)
new_array = cv2.resize(img_array, (270,450))
#cv2.imwrite('out{}.jpg'.format(count), new_array)
out.write(new_array)
count += 1
\ No newline at end of file
import cv2
import numpy as np
cap = cv2.VideoCapture('./videos/test.mp4') #load video
count1 = 0 #counts number of frames
#capture two frames
rval, frame1 = cap.read()
rval, frame2 = cap.read()
while cap.isOpened():
diff = cv2.absdiff(frame1, frame2) #difference of two frames
gray = cv2.cvtColor(diff,cv2.COLOR_BGR2GRAY) #convert diff into greyscale
blur = cv2.GaussianBlur(gray, (5,5), 0) #blur image to reduce noise
_, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY) #set threshold
dilated = cv2.dilate(thresh, None, iterations=3) #fill holes in the thresholded image
#find contour
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
count2 = 0 #counts number of motions in a frame
#further implementation(removing, rectangle)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour) #values of contour
if cv2.contourArea(contour) < 7000: #to remove small unnecessary contours, (modify number for better results)
continue
filename = "clip{}-{}.jpg".format(count1,count2)
motion = frame1[y:y+h, x:x+w]
cv2.imwrite(filename, motion)
count2 += 1
#cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 0, 255), 2) #draw rectangle on frame1
count1 += 1
#show results
#cv2.imshow("feed", frame1)
#change to next frame
frame1 = frame2
rval, frame2 = cap.read()
#incase of error
if cv2.waitKey(40) == 27:
break
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file