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.saliency;
31
32 import org.openimaj.citation.annotation.Reference;
33 import org.openimaj.citation.annotation.ReferenceType;
34 import org.openimaj.image.FImage;
35 import org.openimaj.image.analysis.algorithm.HorizontalProjection;
36 import org.openimaj.image.analysis.algorithm.VerticalProjection;
37 import org.openimaj.math.geometry.shape.Rectangle;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 @Reference(
57 type = ReferenceType.Inproceedings,
58 author = { "Luo, Yiwen", "Tang, Xiaoou" },
59 title = "Photo and Video Quality Evaluation: Focusing on the Subject",
60 year = "2008",
61 booktitle = "Proceedings of the 10th European Conference on Computer Vision: Part III",
62 pages = { "386", "", "399" },
63 url = "http://dx.doi.org/10.1007/978-3-540-88690-7_29",
64 publisher = "Springer-Verlag",
65 series = "ECCV '08",
66 customData = {
67 "isbn", "978-3-540-88689-1",
68 "location", "Marseille, France",
69 "numpages", "14",
70 "doi", "10.1007/978-3-540-88690-7_29",
71 "acmid", "1478204",
72 "address", "Berlin, Heidelberg"
73 }
74 )
75 public class LuoTangSubjectRegion implements SaliencyMapGenerator<FImage> {
76 DepthOfFieldEstimator dofEstimator;
77
78 Rectangle roi;
79 private FImage dofMap;
80 private float alpha = 0.9f;
81
82
83
84
85
86 public LuoTangSubjectRegion() {
87 dofEstimator = new DepthOfFieldEstimator();
88 }
89
90
91
92
93
94
95
96
97
98 public LuoTangSubjectRegion(float alpha, int maxKernelSize, int kernelSizeStep, int nbins, int windowSize) {
99 this.dofEstimator = new DepthOfFieldEstimator(maxKernelSize, kernelSizeStep, nbins, windowSize);
100 this.alpha = alpha;
101 }
102
103
104
105
106 @Override
107 public void analyseImage(FImage image) {
108 image.analyseWith(dofEstimator);
109 dofMap = dofEstimator.getSaliencyMap();
110
111 for (int y=0; y<dofMap.height; y++) {
112 for (int x=0; x<dofMap.width; x++) {
113 if (dofMap.pixels[y][x] == 0)
114 dofMap.pixels[y][x] = 1;
115 else
116 dofMap.pixels[y][x] = 0;
117 }
118 }
119 }
120
121
122
123
124 public Rectangle calculateROI() {
125 float [] pUx = HorizontalProjection.project(dofMap);
126 float [] pUy = VerticalProjection.project(dofMap);
127
128 float energy = 0;
129 for (float f : pUx) energy += f;
130 float thresh = energy * ((1 - alpha) / 2);
131
132 int x1 = 0;
133 float tmp = pUx[x1];
134 while (tmp < thresh) {
135 x1++;
136 tmp += pUx[x1];
137 }
138
139 int y1 = 0;
140 tmp = pUy[y1];
141 while (tmp < thresh) {
142 y1++;
143 tmp += pUy[y1];
144 }
145
146 int x2 = pUx.length - 1;
147 tmp = pUx[x2];
148 while (tmp < thresh) {
149 x2--;
150 tmp += pUx[x2];
151 }
152
153 int y2 = pUy.length - 1;
154 tmp = pUy[y2];
155 while (tmp < thresh) {
156 y2--;
157 tmp += pUy[y2];
158 }
159
160 return new Rectangle(x1, y1, x2-x1, y2-y1);
161 }
162
163 @Override
164 public FImage getSaliencyMap() {
165 return dofMap;
166 }
167
168
169
170
171 public FImage getROIMap() {
172 FImage image = new FImage(dofMap.width, dofMap.height);
173 image.drawShapeFilled(calculateROI(), 1f);
174 return image;
175 }
176 }