This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
/
FCTH.java
516 lines (388 loc) · 17.7 KB
/
FCTH.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* This file is part of the LIRE project: http://lire-project.net
* LIRE is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* LIRE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LIRE; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* We kindly ask you to refer the any or one of the following publications in
* any publication mentioning or employing Lire:
*
* Lux Mathias, Savvas A. Chatzichristofis. Lire: Lucene Image Retrieval –
* An Extensible Java CBIR Library. In proceedings of the 16th ACM International
* Conference on Multimedia, pp. 1085-1088, Vancouver, Canada, 2008
* URL: http://doi.acm.org/10.1145/1459359.1459577
*
* Lux Mathias. Content Based Image Retrieval with LIRE. In proceedings of the
* 19th ACM International Conference on Multimedia, pp. 735-738, Scottsdale,
* Arizona, USA, 2011
* URL: http://dl.acm.org/citation.cfm?id=2072432
*
* Mathias Lux, Oge Marques. Visual Information Retrieval using Java and LIRE
* Morgan & Claypool, 2013
* URL: http://www.morganclaypool.com/doi/abs/10.2200/S00468ED1V01Y201301ICR025
*
* Copyright statement:
* ====================
* (c) 2002-2013 by Mathias Lux (mathias@juggle.at)
* http://www.semanticmetadata.net/lire, http://www.lire-project.net
*
* Updated: 11.07.13 10:31
*/
package net.semanticmetadata.lire.imageanalysis.features.global;
import net.semanticmetadata.lire.builders.DocumentBuilder;
import net.semanticmetadata.lire.imageanalysis.features.GlobalFeature;
import net.semanticmetadata.lire.imageanalysis.features.LireFeature;
import net.semanticmetadata.lire.imageanalysis.features.global.fcth.*;
import net.semanticmetadata.lire.utils.ImageUtils;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.Arrays;
/**
* The FCTH feature was created, implemented and provided by Savvas A. Chatzichristofis<br/>
* More information can be found in: Savvas A. Chatzichristofis and Yiannis S. Boutalis,
* <i>FCTH: Fuzzy Color and Texture Histogram - A Low Level Feature for Accurate Image
* Retrieval</i>, in Proceedings of the Ninth International Workshop on Image Analysis for
* Multimedia Interactive Services, IEEE, Klagenfurt, May, 2008.
*
* @author: Savvas A. Chatzichristofis, savvash@gmail.com
*/
public class FCTH implements GlobalFeature {
public boolean Compact = false;
protected double[] histogram = new double[192];
int tmp;
double distResult = 0;
double distTmp1 = 0;
double distTmp2 = 0;
double distTmpCnt1 = 0;
double distTmpCnt2 = 0;
double distTmpCnt3 = 0;
// Constructor
public FCTH() {
}
// Apply filter
public double[] Apply(BufferedImage image) {
Fuzzy10Bin Fuzzy10 = new Fuzzy10Bin(false);
Fuzzy24Bin Fuzzy24 = new Fuzzy24Bin(false);
FuzzyFCTHpart FuccyFCTH = new FuzzyFCTHpart();
double[] Fuzzy10BinResultTable = new double[10];
double[] Fuzzy24BinResultTable = new double[24];
double[] FuzzyHistogram192 = new double[192];
int Method = 2;
int width = image.getWidth();
int height = image.getHeight();
for (int R = 0; R < 192; R++) {
FuzzyHistogram192[R] = 0;
}
RGB2HSV HSVConverter = new RGB2HSV();
int[] HSV = new int[3];
WaveletMatrixPlus Matrix = new WaveletMatrixPlus();
double[][] ImageGrid = new double[width][height];
int[][] ImageGridRed = new int[width][height];
int[][] ImageGridGreen = new int[width][height];
int[][] ImageGridBlue = new int[width][height];
int pixel, r,g,b;
// extraction is based on a speedup fix from Michael Riegler & Konstantin Pogorelov
BufferedImage image_rgb = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
image_rgb.getGraphics().drawImage(image, 0, 0, null);
int[] pixels = ((DataBufferInt) image_rgb.getRaster().getDataBuffer()).getData();
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
pixel = pixels[y * width + x];
b = (pixel >> 16) & 0xFF;
g = (pixel >> 8) & 0xFF;
r = (pixel) & 0xFF;
ImageGridRed[x][y] = r;
ImageGridGreen[x][y] = g;
ImageGridBlue[x][y] = b;
int mean = (int) (0.114 * b + 0.587 * g + 0.299 * r);
ImageGrid[x][y] = mean;
}
}
int NumberOfBlocks = 1600;
int Step_X = (int) Math.floor(width / Math.sqrt(NumberOfBlocks));
int Step_Y = (int) Math.floor(height / Math.sqrt(NumberOfBlocks));
if ((Step_X % 2) != 0) {
Step_X = Step_X - 1;
}
if ((Step_Y % 2) != 0) {
Step_Y = Step_Y - 1;
}
if (Step_Y < 4) Step_Y = 4;
if (Step_X < 4) Step_X = 4;
///
// Filter
for (int y = 0; y < height - Step_Y; y += Step_Y) {
for (int x = 0; x < width - Step_X; x += Step_X) {
//int[][] BinaryBlock = new int[4][4];
double[][] Block = new double[4][4];
int[][] BlockR = new int[4][4];
int[][] BlockG = new int[4][4];
int[][] BlockB = new int[4][4];
int[][] BlockCount = new int[4][4];
int[] CororRed = new int[Step_Y * Step_X];
int[] CororGreen = new int[Step_Y * Step_X];
int[] CororBlue = new int[Step_Y * Step_X];
int[] CororRedTemp = new int[Step_Y * Step_X];
int[] CororGreenTemp = new int[Step_Y * Step_X];
int[] CororBlueTemp = new int[Step_Y * Step_X];
int MeanRed = 0;
int MeanGreen = 0;
int MeanBlue = 0;
int CurrentPixelX = 0;
int CurrentPixelY = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Block[i][j] = 0;
BlockCount[i][j] = 0;
}
}
//#endregion
int TempSum = 0;
for (int i = 0; i < Step_X; i++) {
for (int j = 0; j < Step_Y; j++) {
CurrentPixelX = 0;
CurrentPixelY = 0;
if (i >= (Step_X / 4)) CurrentPixelX = 1;
if (i >= (Step_X / 2)) CurrentPixelX = 2;
if (i >= (3 * Step_X / 4)) CurrentPixelX = 3;
if (j >= (Step_Y / 4)) CurrentPixelY = 1;
if (j >= (Step_Y / 2)) CurrentPixelY = 2;
if (j >= (3 * Step_Y / 4)) CurrentPixelY = 3;
Block[CurrentPixelX][CurrentPixelY] += ImageGrid[x + i][y + j];
BlockCount[CurrentPixelX][CurrentPixelY]++;
BlockR[CurrentPixelX][CurrentPixelY] = ImageGridRed[x + i][y + j];
BlockG[CurrentPixelX][CurrentPixelY] = ImageGridGreen[x + i][y + j];
BlockB[CurrentPixelX][CurrentPixelY] = ImageGridBlue[x + i][y + j];
CororRed[TempSum] = BlockR[CurrentPixelX][CurrentPixelY];
CororGreen[TempSum] = BlockG[CurrentPixelX][CurrentPixelY];
CororBlue[TempSum] = BlockB[CurrentPixelX][CurrentPixelY];
CororRedTemp[TempSum] = BlockR[CurrentPixelX][CurrentPixelY];
CororGreenTemp[TempSum] = BlockG[CurrentPixelX][CurrentPixelY];
CororBlueTemp[TempSum] = BlockB[CurrentPixelX][CurrentPixelY];
TempSum++;
}
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
Block[i][j] = Block[i][j] / BlockCount[i][j];
}
}
Matrix = singlePassThreshold(Block, 1);
for (int i = 0; i < (Step_Y * Step_X); i++) {
MeanRed += CororRed[i];
MeanGreen += CororGreen[i];
MeanBlue += CororBlue[i];
}
MeanRed = (int) (MeanRed / (Step_Y * Step_X));
MeanGreen = (int) (MeanGreen / (Step_Y * Step_X));
MeanBlue = (int) (MeanBlue / (Step_Y * Step_X));
HSV = HSVConverter.ApplyFilter(MeanRed, MeanGreen, MeanBlue);
if (Compact == false) {
Fuzzy10BinResultTable = Fuzzy10.ApplyFilter(HSV[0], HSV[1], HSV[2], Method);
Fuzzy24BinResultTable = Fuzzy24.ApplyFilter(HSV[0], HSV[1], HSV[2], Fuzzy10BinResultTable, Method);
FuzzyHistogram192 = FuccyFCTH.ApplyFilter(Matrix.F3, Matrix.F2, Matrix.F1, Fuzzy24BinResultTable, Method, 24);
} else {
Fuzzy10BinResultTable = Fuzzy10.ApplyFilter(HSV[0], HSV[1], HSV[2], Method);
FuzzyHistogram192 = FuccyFCTH.ApplyFilter(Matrix.F3, Matrix.F2, Matrix.F1, Fuzzy10BinResultTable, Method, 10);
}
}
}
// end of the filter
double TotalSum = 0;
for (int i = 0; i < 192; i++) {
TotalSum += FuzzyHistogram192[i];
}
for (int i = 0; i < 192; i++) {
FuzzyHistogram192[i] = FuzzyHistogram192[i] / TotalSum;
}
FCTHQuant Quant = new FCTHQuant();
FuzzyHistogram192 = Quant.Apply(FuzzyHistogram192);
return FuzzyHistogram192;
}
private WaveletMatrixPlus singlePassThreshold(double[][] inputMatrix, int level) {
WaveletMatrixPlus TempMatrix = new WaveletMatrixPlus();
level = (int) Math.pow(2.0, level - 1);
//GETLENGTH*************
double[][] resultMatrix = new double[inputMatrix.length][inputMatrix[0].length];
int xOffset = inputMatrix.length / 2 / level;
int yOffset = inputMatrix[0].length / 2 / level;
int currentPixel = 0;
//double size = inputMatrix.length * inputMatrix[0].length;
double multiplier = 0;
for (int y = 0; y < inputMatrix[0].length; y++) {
for (int x = 0; x < inputMatrix.length; x++) {
if ((y < inputMatrix[0].length / 2 / level) && (x < inputMatrix.length / 2 / level)) {
currentPixel++;
resultMatrix[x][y] = (inputMatrix[2 * x][2 * y] + inputMatrix[2 * x + 1][2 * y] + inputMatrix[2 * x][2 * y + 1] + inputMatrix[2 * x + 1][2 * y + 1]) / 4;
double vertDiff = (-inputMatrix[2 * x][2 * y] - inputMatrix[2 * x + 1][2 * y] + inputMatrix[2 * x][2 * y + 1] + inputMatrix[2 * x + 1][2 * y + 1]);
double horzDiff = (inputMatrix[2 * x][2 * y] - inputMatrix[2 * x + 1][2 * y] + inputMatrix[2 * x][2 * y + 1] - inputMatrix[2 * x + 1][2 * y + 1]);
double diagDiff = (-inputMatrix[2 * x][2 * y] + inputMatrix[2 * x + 1][2 * y] + inputMatrix[2 * x][2 * y + 1] - inputMatrix[2 * x + 1][2 * y + 1]);
resultMatrix[x + xOffset][y] = (int) (byte) (multiplier + Math.abs(vertDiff));
resultMatrix[x][y + yOffset] = (int) (byte) (multiplier + Math.abs(horzDiff));
resultMatrix[x + xOffset][y + yOffset] = (int) (byte) (multiplier + Math.abs(diagDiff));
} else {
if ((x >= inputMatrix.length / level) || (y >= inputMatrix[0].length / level))
{
resultMatrix[x][y] = inputMatrix[x][y];
}
}
}
}
double Temp1 = 0;
double Temp2 = 0;
double Temp3 = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
Temp1 += 0.25 * Math.pow(resultMatrix[2 + i][j], 2);
Temp2 += 0.25 * Math.pow(resultMatrix[i][2 + j], 2);
Temp3 += 0.25 * Math.pow(resultMatrix[2 + i][2 + j], 2);
}
}
//double[] MatrixResults = new double[4];
TempMatrix.F1 = Math.sqrt(Temp1);
TempMatrix.F2 = Math.sqrt(Temp2);
TempMatrix.F3 = Math.sqrt(Temp3);
TempMatrix.Entropy = 0;
return TempMatrix;
}
@Override
public void extract(BufferedImage bimg) {
bimg = ImageUtils.get8BitRGBImage(bimg);
histogram = Apply(bimg);
}
/**
* Creates a small byte array from an FCTH descriptor.
* Stuffs 2 numbers into one byte and omits all but 1 of the trailing 0's.
*
* @return
*/
@Override
public byte[] getByteArrayRepresentation() {
// find out the position of the beginning of the trailing zeros.
int position = -1;
for (int i = 0; i < histogram.length; i++) {
if (position == -1) {
if (histogram[i] == 0) position = i;
} else if (position > -1) {
if (histogram[i] != 0) position = -1;
}
}
if (position < 0) position = histogram.length - 1;
// find out the actual length. two values in one byte, so we have to round up.
int length = (position + 1) / 2;
if ((position + 1) % 2 == 1) length = position / 2 + 1;
byte[] result = new byte[length];
for (int i = 0; i < result.length; i++) {
tmp = ((int) (histogram[(i << 1)] * 2)) << 4;
tmp = (tmp | ((int) (histogram[(i << 1) + 1] * 2)));
result[i] = (byte) (tmp - 128);
}
return result;
}
/**
* Reads descriptor from a byte array. Much faster than the String based method.
*
* @param in byte array from corresponding method
* @see CEDD#getByteArrayRepresentation
*/
@Override
public void setByteArrayRepresentation(byte[] in) {
setByteArrayRepresentation(in, 0, in.length);
}
@Override
public void setByteArrayRepresentation(byte[] in, int offset, int length) {
if (length << 1 < histogram.length) Arrays.fill(histogram, length << 1, histogram.length, 0);
for (int i = offset; i < offset + length; i++) {
tmp = in[i] + 128;
histogram[((i - offset) << 1) + 1] = ((double) (tmp & 0x000F)) / 2d;
histogram[(i - offset) << 1] = ((double) (tmp >> 4)) / 2d;
}
}
@Override
public double[] getFeatureVector() {
return histogram;
}
@Override
public double getDistance(LireFeature vd) { // added by mlux //TODO Tanimoto MetricUtils
// Check if instance of the right class ...
if (!(vd instanceof FCTH))
throw new UnsupportedOperationException("Wrong descriptor.");
// casting ...
FCTH ch = (FCTH) vd;
// check if parameters are fitting ...
if ((ch.histogram.length != histogram.length))
throw new UnsupportedOperationException("Histogram lengths or color spaces do not match");
// Tanimoto coefficient
distResult = 0;
distTmp1 = 0;
distTmp2 = 0;
distTmpCnt1 = 0;
distTmpCnt2 = 0;
distTmpCnt3 = 0;
for (int i = 0; i < ch.histogram.length; i++) {
distTmp1 += ch.histogram[i];
distTmp2 += histogram[i];
}
if (distTmp1 == 0 && distTmp2 == 0) return 0d;
if (distTmp1 == 0 || distTmp2 == 0) return 100d;
for (int i = 0; i < ch.histogram.length; i++) {
distTmpCnt1 += (ch.histogram[i] / distTmp1) * (histogram[i] / distTmp2);
distTmpCnt2 += (histogram[i] / distTmp2) * (histogram[i] / distTmp2);
distTmpCnt3 += (ch.histogram[i] / distTmp1) * (ch.histogram[i] / distTmp1);
}
distResult = (100 - 100 * (distTmpCnt1 / (distTmpCnt2 + distTmpCnt3 - distTmpCnt1)));
return distResult;
}
// public String getStringRepresentation() {
// // FCTH is quantized to 3bits / bin ... therefore ints are enough.
// StringBuilder sb = new StringBuilder(histogram.length * 2 + 25);
// sb.append("fcth");
// sb.append(' ');
// sb.append(histogram.length);
// sb.append(' ');
// for (double aData : histogram) {
// sb.append((int) aData);
// sb.append(' ');
// }
// return sb.toString().trim();
// }
//
// public void setStringRepresentation(String s) {
// StringTokenizer st = new StringTokenizer(s);
// if (!st.nextToken().equals("fcth"))
// throw new UnsupportedOperationException("This is not a FCTH descriptor.");
// histogram = new double[Integer.parseInt(st.nextToken())];
// for (int i = 0; i < histogram.length; i++) {
// if (!st.hasMoreTokens())
// throw new IndexOutOfBoundsException("Too few numbers in string representation.");
// histogram[i] = Integer.parseInt(st.nextToken());
// }
// }
@Override
public String toString() {
StringBuilder sb = new StringBuilder(histogram.length * 2 + 25);
for (double aData : histogram) {
sb.append((int) aData);
sb.append(' ');
}
return "FCTH{" + sb.toString().trim() + "}";
}
@Override
public String getFeatureName() {
return "FCTH";
}
@Override
public String getFieldName() {
return DocumentBuilder.FIELD_NAME_FCTH;
}
}