-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect_blur.py
54 lines (43 loc) · 1.65 KB
/
detect_blur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# USAGE
# python detect_blur.py --images images
# import the necessary packages
from imutils import paths
import argparse
from cv2 import cv2
import os
import shutil
try:
if not os.path.exists('blur_clear'):
os.makedirs('blur_clear')
except OSError:
print('Error: Creating directory of data')
def variance_of_laplacian(image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(image, cv2.CV_64F).var()
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True,
help="path to input directory of images")
ap.add_argument("-t", "--threshold", type=float, default=1000.0,
help="focus measures that fall below this value will be considered 'blurry'")
args = vars(ap.parse_args())
currentFrame = 0
# loop over the input images
for imagePath in paths.list_images(args["images"]):
# load the image, convert it to grayscale, and compute the
# focus measure of the image using the Variance of Laplacian
# method
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
text = "Not Blurry"
# if the focus measure is less than the supplied threshold,
# then the image should be considered "blurry"
if fm > args["threshold"]:
shutil.move(imagePath, './blur_clear/')
# show the image
# cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
# cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
#cv2.imshow("Image", image)
key = cv2.waitKey(0)