-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistogram.java
More file actions
90 lines (75 loc) · 3.1 KB
/
Histogram.java
File metadata and controls
90 lines (75 loc) · 3.1 KB
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
/*
* Histogram.java
*
* Copyright (c) 2013, Pablo Garcia-Sanchez. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* Contributors:
* Daniel Calandria
*/
import com.sun.org.apache.xalan.internal.utils.FeatureManager;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Histogram implements FeatureManager.Feature {
public static final int BINS = 32;
public static final float MIN_VALUE = 0.0f;
public static final float MAX_VALUE = 255.0f;
static{
//System.load("/home/dcalandria/5hackathon/eclipse/FeaturesOpenCV/libopencv_java244.so");
System.load("/Users/dcalandria/hackathon/workspace/osgiliath-code/FeaturesOpenCV/libopencv_java245.dylib");
}
/* generate a 32-bar histogram for each color channel */
@Override
public double[] extract(String path) {
System.out.println("Path: " + path);
Mat image = Highgui.imread(path);
return extract(image);
}
@Override
public double[] extract(Mat image) {
Mat hsvImage = new Mat(image.width(), image.height(), image.type());
Mat histHue = new Mat();
Mat histSaturation = new Mat();
Imgproc.cvtColor(image, hsvImage, Imgproc.COLOR_BGR2HSV);
List<Mat> channels = new ArrayList<Mat>();
Core.split(hsvImage, channels);
//Histogram for hue
Imgproc.calcHist(Arrays.asList( new Mat[]{channels.get(0)} ), new MatOfInt(0),
new Mat(), histHue, new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));
//Histogram for saturation
Imgproc.calcHist(Arrays.asList( new Mat[]{channels.get(1)} ), new MatOfInt(0),
new Mat(), histSaturation, new MatOfInt(BINS), new MatOfFloat(MIN_VALUE, MAX_VALUE));
double sum = Core.sumElems(histHue).val[0];
double[] values = new double[histHue.height()+histSaturation.height()];
int k = 0;
for (int i = 0; i < histHue.height(); ++i ) {
values[k++] = histHue.get(i, 0)[0]/sum;
}
sum = Core.sumElems(histSaturation).val[0];
for ( int i = 0; i < histSaturation.height(); ++i) {
values[k++] = histSaturation.get(i, 0)[0]/sum;
}
return values;
}
}