forked from ankurag12/Bluetooth-SPP-Image-Tx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveContrastEnhancement.java
More file actions
146 lines (114 loc) · 4.12 KB
/
AdaptiveContrastEnhancement.java
File metadata and controls
146 lines (114 loc) · 4.12 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
package ru.sash0k.bluetooth_terminal.image;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.widget.ImageView;
import ru.sash0k.bluetooth_terminal.R;
import ru.sash0k.bluetooth_terminal.Utils;
/**
* Created by rv on 12/25/15.
*/
public class AdaptiveContrastEnhancement {
/**
* Adaptive Contrast Enhancement is modification of the gray level values based on some
* criterion
* that adjusts its parameters as local image characteristics change.
*
* @author Diego Catalano
*/
int windowSize;
double k1, k2, maxGain, minGain;
/**
* Initialize a new instance of the AdaptiveContrastEnhancement class.
*
* @param windowSize Size of window(should be an odd number).
* @param k1 Local gain factor, between 0 and 1.
* @param k2 Local mean constant, between 0 and 1.
* @param minGain The minimum gain factor.
* @param maxGain The maximum gain factor.
*/
public AdaptiveContrastEnhancement(int windowSize, double k1, double k2, double minGain,
double maxGain) {
this.windowSize = windowSize;
this.k1 = k1;
this.k2 = k2;
this.minGain = minGain;
this.maxGain = maxGain;
}
public Bitmap applyInPlace(Bitmap Bitmap) {
int width = Bitmap.getWidth();
int height = Bitmap.getHeight();
int lines = CalcLines(windowSize);
Bitmap bmp2 = Bitmap.copy(Bitmap.getConfig(), true);
// the mean (average) for the entire image I(x,y);
double mean = getMean(Bitmap);
int colour;
for (int x = 0; x < height; x++) {
for (int y = 0; y < width; y++) {
int hits = 0;
int windowSize2 = windowSize * windowSize;
int[] values = new int[windowSize2];
double sumMean = 0;
double sumVar = 0;
double factor;
for (int i = x - lines; i <= x + lines; i++) {
for (int j = y - lines; j <= y + lines; j++) {
if ((i >= 0) && (i < height) && (j >= 0) && (j < width)) {
values[hits] = getGray(bmp2, i, j);
//sumGray += values[hits];
sumMean += values[hits];
sumVar += values[hits] * values[hits];
hits++;
}
}
}
sumMean /= windowSize2;
sumVar /= windowSize2;
sumVar -= sumMean * sumMean;
if (sumVar != 0)
factor = k1 * (mean / sumVar);
else
factor = maxGain;
if (factor > maxGain) factor = maxGain;
if (factor < minGain) factor = minGain;
double gray = factor * (getGray(bmp2, x, y) - sumMean) + k2 * sumMean;
//Utils.log("Gray values "+x +" "+y+" before: "+getGray(Bitmap,x,y)+" after:
// "+gray);
setGray(Bitmap, x, y, (int) gray);
}
}
return Bitmap;
}
/**
* Get Gray.
*
* @param x X axis coordinate.
* @param y Y axis coordinate.
* @return Gray channel's value.
*/
public int getGray(Bitmap bm, int x, int y) {
return Color.red(bm.getPixel(y, x));
}
/**
* Set Gray.
*
* @param x X axis coordinate.
* @param y Y axis coordinate.
* @param value Gray channel's value.
*/
public void setGray(Bitmap bm, int x, int y, int value) {
int color = Color.rgb(value, value, value);
bm.setPixel(y, x, color);
}
private double getMean(Bitmap Bitmap) {
int sum = 0;
for (int i = 0; i < Bitmap.getHeight(); i++) {
for (int j = 0; j < Bitmap.getWidth(); j++) {
sum += getGray(Bitmap, i, j);
}
}
return sum / (Bitmap.getWidth() * Bitmap.getHeight());
}
private int CalcLines(int windowSize) {
return (windowSize - 1) / 2;
}
}