-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageColorData.java
More file actions
188 lines (170 loc) · 8.79 KB
/
Copy pathImageColorData.java
File metadata and controls
188 lines (170 loc) · 8.79 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
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
package photorainbow;
import java.lang.Integer;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ImageColorData
{
private static final Logger log = Logger.getLogger(ImageColorData.class.getName());
private int imageWidth = 0;
private int imageHeight = 0;
private float rangeValue = 0.0f;
private HashMap<String, float> _brightnessColorDict = new HashMap<String, float>();
private HashMap<String, List<Color>> _colorByPixel = new HashMap<String, List<Color>>();
private HashMap<float, String> imgVIBGYORHueDiffDict = new HashMap<float, String>();
private HashMap<Image, HashMap<String, float>> imageDataDictSorted = new HashMap<Image, HashMap<String, float>>();
public HashMap<String, float> getBrightnessColorDict() {
return this._brightnessColorDict;
}
public HashMap<String, List<Color>> getColorByPixel() {
return this._colorByPixel;
}
//Ayesha Logic...
public HashMap<Image, float> getSortedImagesByColor(String color) {
HashMap<Image, float> dicImgHue = new HashMap<Image, float>();
HashMap<Image, float> orderedItems = ImageColorSorter.sortByColorValue(imageDataDictSorted);
Iterator<Map.Entry<Image, float>> entries = orderedItems.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry<Image, float> entry = entries.next();
dicImgHue.add(entry.getKey(), entry.getValue());
}
return dicImgHue;
}
public HashMap<Image, HashMap<String, float>> getSortedImagesByRainbow() {
HashMap<Image, HashMap<String, float>> dicImgRainbowHue = new HashMap<Image, HashMap<String, float>>();
List<int> colors = new ArrayList<int>();
colors.add(Color.VIOLET);
colors.add(Color.INDIGO);
colors.add(Color.GREEN);
colors.add(Color.YELLOW);
HashMap<Image, float> orderedItems = ImageColorSorter.sortByColorValue(imageDataDictSorted, colors);
Iterator<Map.Entry<Image, float>> entries = orderedItems.entrySet().iterator();
while(entries.hasNext()) {
Map.Entry<Image, float> entry = entries.next();
dicImgRainbowHue.add(entry.getKey(), entry.getValue());
}
return dicImgRainbowHue;
}
private boolean InRange(int min, int max, operand) {
return (operand >= min && operand <= max) ? true : false;
}
public HashMap<String, List<Color>> getColorsInImage(Bitmap imageAsBitmap) {
int xCoord = 0, yCoord = 0;
float temp = 0;
this.imageWidth = imageAsBitmap.getWidth();
this.imageHeight = imageAsBitmap.getHeight();
try {
for (xCoord = 0; xCoord < this.imageWidth; xCoord++) {
for (yCoord = 0; yCoord < this.imageHeight; yCoord++) {
List<float> imgVIBGYORHueDiff = new List<float>();
Color imgPixelColor = imageAsBitmap.getPixel(xCoord, yCoord);
float pixelColorHue = imgPixelColor.getHue();
temp = pixelColorHue;
rangeValue = temp;
if (!imgVIBGYORHueDiffDict.containsKey(rangeValue)) {
if (InRange(300, 360, Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Violet"); break;
}
else if (InRange(275, 299, Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Indigo"); continue;
}
else if (InRange(240, 274,Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Blue"); continue;
}
else if (InRange(120, 239,Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Green"); continue;
}
else if (InRange(60, 119, Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Yellow"); continue;
}
else if (InRange(40, 59, Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Orange"); continue;
}
else if (InRange(0, 39, Integer.valueOf(rangeValue))) {
imgVIBGYORHueDiffDict.add(rangeValue, "Red"); continue;
}
}
for (float hueDiff : imgVIBGYORHueDiffDict.getKeys()) {
imgVIBGYORHueDiff.add(hueDiff);
}
float closestPixelColorByHue = imgVIBGYORHueDiff.getMinimum();
if (_colorByPixel.containsKey(imgVIBGYORHueDiffDict[closestPixelColorByHue])) {
_colorByPixel[imgVIBGYORHueDiffDict[closestPixelColorByHue]].add(imgPixelColor);
_brightnessColorDict[imgVIBGYORHueDiffDict[closestPixelColorByHue]] += imgPixelColor.GetBrightness();
}
else {
List<Color> pixelColorStructure = new List<Color>();
pixelColorStructure.add(imgPixelColor);
_colorByPixel.add(imgVIBGYORHueDiffDict[closestPixelColorByHue], pixelColorStructure);
_brightnessColorDict.add(imgVIBGYORHueDiffDict[closestPixelColorByHue], imgPixelColor.GetBrightness());
}
}
}
}
catch (Exception e) {
log.log(Level.ERROR, e.printStackTrace());
}
return _colorByPixel;
}
public HashMap<Bitmap, List<HashMap<String, float>>> GetColorData2(Image imgObj) {
_colorByPixel = getColorsInImage(imgObj.Img);
HashMap<Bitmap, List<HashMap<String, float>>> imageDataDict = new HashMap<Bitmap, List<HashMap<String, float>>>();
HashMap<String, float> colPercentageDict = new HashMap<String, float>();
List<HashMap<String, float>> imgInfoDictList = new List<HashMap<string, float>>();
colPercentageDict.add("Violet", percentageOfColorInImage("Violet"));
colPercentageDict.add("Indigo", percentageOfColorInImage("Indigo"));
colPercentageDict.add("Blue", percentageOfColorInImage("Blue"));
colPercentageDict.add("Green", percentageOfColorInImage("Green"));
colPercentageDict.add("Yellow", percentageOfColorInImage("Yellow"));
colPercentageDict.add("Orange", percentageOfColorInImage("Orange"));
colPercentageDict.add("Red", percentageOfColorInImage("Red"));
imgInfoDictList.add(colPercentageDict);
HashMap<String, float> colAvgBrightnessDict = new HashMap<String, float>();
colAvgBrightnessDict.add("Violet", calcAverageBrightnessByColor("Violet"));
colAvgBrightnessDict.add("Indigo", calcAverageBrightnessByColor("Indigo"));
colAvgBrightnessDict.add("Blue", calcAverageBrightnessByColor("Blue"));
colAvgBrightnessDict.add("Green", calcAverageBrightnessByColor("Green"));
colAvgBrightnessDict.add("Yellow", calcAverageBrightnessByColor("Yellow"));
colAvgBrightnessDict.add("Orange", calcAverageBrightnessByColor("Orange"));
colAvgBrightnessDict.add("Red", calcAverageBrightnessByColor("Red"));
imgInfoDictList.add(colAvgBrightnessDict);
imageDataDict.add(imgObj.Img, imgInfoDictList);
imageDataDictSorted.add(imgObj, colPercentageDict);
return imageDataDict;
}
public float percentageOfColorInImage(String colorName) {
float percentageOfColor = 0;
if(_colorByPixel.containsKey(colorName)) {
float numberofPixelsByColor = _colorByPixel[colorName].Length;
float totalPixelsInImage = imageWidth * imageHeight;
percentageOfColor = (numberofPixelsByColor / totalPixelsInImage) * 100;
return percentageOfColor;
}
else {
return percentageOfColor;
}
}
public float calcAverageBrightnessByColor(String colorName) {
float averageBrightnessByColor = 0;
if(_colorByPixel.containsKey(colorName)) {
int numberOfPixelsByColor = _colorByPixel[colorName].Length;
averageBrightnessByColor = _brightnessColorDict[colorName] / numberOfPixelsByColor;
return averageBrightnessByColor;
}
else {
return averageBrightnessByColor;
}
}
private static float adjustHue(float temp) {
if (temp < 0) {
temp = temp * (-1);
return temp;
}
else {
return temp;
}
}
}