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 package org.openimaj.image.feature.global;
31
32 import org.apache.commons.math.stat.descriptive.SummaryStatistics;
33 import org.openimaj.citation.annotation.Reference;
34 import org.openimaj.citation.annotation.ReferenceType;
35 import org.openimaj.feature.DoubleFV;
36 import org.openimaj.feature.EnumFV;
37 import org.openimaj.feature.FeatureVectorProvider;
38 import org.openimaj.image.analyser.PixelAnalyser;
39
40
41
42
43
44
45
46 @Reference(
47 type = ReferenceType.Inproceedings,
48 author = { "Hasler, David", "S\\\"{u}sstrunk, Sabine" },
49 title = "Measuring {C}olourfulness in {N}atural {I}mages",
50 year = "2003",
51 booktitle = "Proc. {IS}&{T}/{SPIE} {E}lectronic {I}maging 2003: {H}uman {V}ision and {E}lectronic {I}maging {VIII}",
52 pages = { "87", "", "95" },
53 volume = "5007",
54 url = "http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf?version=1",
55 customData = {
56 "details", "http://infoscience.epfl.ch/record/33994",
57 "documenturl", "http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf",
58 "doi", "10.1117/12.477378",
59 "keywords", "IVRG; colorfulness; image quality; image attributes; colourfulness",
60 "location", "San Jose, CA",
61 "oai-id", "oai:infoscience.epfl.ch:33994",
62 "oai-set", "conf; fulltext; fulltext-public",
63 "review", "NON-REVIEWED",
64 "status", "PUBLISHED",
65 "unit", "LCAV IVRG"
66 })
67 public class Colorfulness implements PixelAnalyser<Float[]>, FeatureVectorProvider<DoubleFV> {
68 SummaryStatistics rgStats = new SummaryStatistics();
69 SummaryStatistics ybStats = new SummaryStatistics();
70
71 @Override
72 public void analysePixel(Float[] pixel) {
73 final float r = pixel[0];
74 final float g = pixel[1];
75 final float b = pixel[2];
76
77 final float rg = r - g;
78 final float yb = 0.5f * (r + g) - b;
79
80 rgStats.addValue(rg);
81 ybStats.addValue(yb);
82 }
83
84
85
86
87
88
89 @Reference(
90 type = ReferenceType.Inproceedings,
91 author = { "Hasler, David", "S\\\"{u}sstrunk, Sabine" },
92 title = "Measuring {C}olourfulness in {N}atural {I}mages",
93 year = "2003",
94 booktitle = "Proc. {IS}&{T}/{SPIE} {E}lectronic {I}maging 2003: {H}uman {V}ision and {E}lectronic {I}maging {VIII}",
95 pages = { "87", "", "95" },
96 volume = "5007",
97 url = "http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf?version=1",
98 customData = {
99 "details", "http://infoscience.epfl.ch/record/33994",
100 "documenturl", "http://infoscience.epfl.ch/record/33994/files/HaslerS03.pdf",
101 "doi", "10.1117/12.477378",
102 "keywords", "IVRG; colorfulness; image quality; image attributes; colourfulness",
103 "location", "San Jose, CA",
104 "oai-id", "oai:infoscience.epfl.ch:33994",
105 "oai-set", "conf; fulltext; fulltext-public",
106 "review", "NON-REVIEWED",
107 "status", "PUBLISHED",
108 "unit", "LCAV IVRG"
109 })
110 public enum ColorfulnessAttr implements FeatureVectorProvider<EnumFV<ColorfulnessAttr>> {
111
112
113
114 NOT(0.0),
115
116
117
118 SLIGHTLY(15.0 / 255.0),
119
120
121
122 MODERATELY(33.0 / 255.0),
123
124
125
126 AVERAGELY(45.0 / 255.0),
127
128
129
130 QUITE(59.0 / 255.0),
131
132
133
134 HIGHLY(82.0 / 255.0),
135
136
137
138 EXTREMELY(109.0 / 255.0);
139
140 private double threshold;
141
142 private ColorfulnessAttr(double val) {
143 this.threshold = val;
144 }
145
146
147
148
149
150
151
152
153 public static ColorfulnessAttr getAttr(double val) {
154 final ColorfulnessAttr[] attrs = values();
155 for (int i = attrs.length - 1; i >= 0; i--) {
156 if (val >= attrs[i].threshold)
157 return attrs[i];
158 }
159 return null;
160 }
161
162 @Override
163 public EnumFV<ColorfulnessAttr> getFeatureVector() {
164 return new EnumFV<ColorfulnessAttr>(this);
165 }
166 }
167
168
169
170
171 public ColorfulnessAttr getColorfulnessAttribute() {
172 return ColorfulnessAttr.getAttr(getColorfulness());
173 }
174
175
176
177
178 public double getColorfulness() {
179 final double var_rg = rgStats.getVariance();
180 final double var_yb = ybStats.getVariance();
181 final double mean_rg = rgStats.getMean();
182 final double mean_yb = ybStats.getMean();
183
184 final double stddev = Math.sqrt(var_rg + var_yb);
185 final double mean = Math.sqrt(mean_rg * mean_rg + mean_yb * mean_yb);
186
187 return stddev + 0.3 * mean;
188 }
189
190 @Override
191 public DoubleFV getFeatureVector() {
192 return new DoubleFV(new double[] { getColorfulness() });
193 }
194
195
196
197
198
199
200 @Override
201 public void reset() {
202 rgStats.clear();
203 ybStats.clear();
204 }
205 }