-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorfulness.py
73 lines (55 loc) · 1.92 KB
/
colorfulness.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from imutils import build_montages
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
import os
import csv
def image_colorfulness(image):
# split the image into its respective RGB components
(B, G, R) = cv2.split(image.astype("float"))
# compute rg = R - G
rg = np.absolute(R - G)
# compute yb = 0.5 * (R + G) - B
yb = np.absolute(0.5 * (R + G) - B)
# compute the mean and standard deviation of both `rg` and `yb`
(rgMean, rgStd) = (np.mean(rg), np.std(rg))
(ybMean, ybStd) = (np.mean(yb), np.std(yb))
# combine the mean and standard deviations
stdRoot = np.sqrt((rgStd ** 2) + (ybStd ** 2))
meanRoot = np.sqrt((rgMean ** 2) + (ybMean ** 2))
# derive the "colorfulness" metric and return it
return stdRoot + (0.3 * meanRoot)
# 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("-f", "--file", type=str, default="",
help="path to text file")
args = vars(ap.parse_args())
file = args["file"]
# initialize the results list
print("colorness of images were")
results = []
with open(file, 'w') as writeFile:
writer = csv.writer(writeFile)
writer.writerow(["imagename", "score"])
# loop over the image paths
for imagePath in paths.list_images(args["images"]):
# load the image, resize it (to speed up computation), and
# compute the colorfulness metric for the image
image = cv2.imread(imagePath)
# image = imutils.resize(image, width=250)
C = image_colorfulness(image)
line = [os.path.basename(imagePath), C]
writer.writerow(line)
# print(os.path.basename(imagePath),":",C)
#appending the results
results.append((os.path.basename(imagePath),C))
#print(results)
# sorting the results in descendint order
results = sorted(results, key=lambda x: x[1], reverse=True)
descColor = [r for r in results[:25]]
for i in descColor[:]:
print(i)