Reputation: 11192
I want to extract only clear frames (unshaken) from video file. This will be useful for my annotation process.
I am using below code to extract frames from a video file.
counter=0
cap = cv2.VideoCapture(file_)
success, frame = cap.read()
while success:
counter += 1
cv2.imwrite(f"{counter}.jpg", frame)
for i in range(15):
cap.read()
success, frame = cap.read()
In the above snippet i am skipping every 15 consecutive frames to avoid extracting all the frames. Is there anyway I can find blurriness in frame and skip only those?
Any hint would be appreciable.
Upvotes: 3
Views: 2141
Reputation: 11192
I am posting complete solution to extract one frame from one sec clip. The solution is based on @Kuldeep Singh's Solution.
cap = cv2.VideoCapture("input.mp4")
fps = int(cap.get(cv2.CAP_PROP_FPS))
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
for i in range(int(frame_count/fps)):
arr_frame=[]
arr_lap=[]
for j in range(fps):
success, frame = cap.read()
laplacian = cv2.Laplacian(frame, cv2.CV_64F).var()
arr_lap.append(laplacian)
arr_frame.append(frame)
selected_frame = arr_frame[arr_lap.index(max(arr_lap))]
cv2.imwrite(f"{i}.jpg", selected_frame)
The above code reads frame for every sec video clip. i.e., let's say the input video has 30 fps then the code reads 30 frames and calculates laplacian variance value. Once the 30 frames are read, it writes frame into image for the maximum variance value. Because, the higher the number, the sharper the edge is. The above process will be continued until the last second.
so, the final output would be, if you pass 10 secs video, you will get 10 clear frames/images.
Upvotes: 7
Reputation: 108
I don't know about the image type you have, but i guess this link would be helpful. Also there is an answer on stackoverflow for the same purpose here.
Almost all methods use the sharpness of the image to determine if the image is blurry of not.( Edge detection function will have low variance).
laplace = cv2.Laplacian(frame, cv2.CV_64F).var()
if laplace>5:
write image
This threshold of 5 is not absolute. You may have to tinker with it.
Upvotes: 6