-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVLCDetector.java
More file actions
356 lines (282 loc) · 11.4 KB
/
Copy pathVLCDetector.java
File metadata and controls
356 lines (282 loc) · 11.4 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
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
package org.opencv.samples.colorblobdetect;
import android.util.Log;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfPoint;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
public class VLCDetector {
// Lower and Upper bounds for range checking in HSV color space
private Scalar mLowerBound = new Scalar(0);
private Scalar mUpperBound = new Scalar(0);
// Minimum contour area in percent for contours filtering
private static double mMinContourArea = 0.1;
// Color radius for range checking in HSV color space
private Scalar mColorRadius = new Scalar(25,50,50,0);
private Mat mSpectrum = new Mat();
private List<MatOfPoint> mContours = new ArrayList<MatOfPoint>();
// Cache
Mat mGrayMat = new Mat();
Mat mCorr = new Mat();
int [] detected;
Mat detectedMat;
double mMeasuredBitPeriod;
ArrayList<Integer> mFrames;
Boolean mDetectionIsRunning = true;
int mFrameTotalCntr = 0;
int mFrameBadCntr = 0;
public void setColorRadius(Scalar radius) {
mColorRadius = radius;
}
public void setHsvColor(Scalar hsvColor) {
double minH = (hsvColor.val[0] >= mColorRadius.val[0]) ? hsvColor.val[0]-mColorRadius.val[0] : 0;
double maxH = (hsvColor.val[0]+mColorRadius.val[0] <= 255) ? hsvColor.val[0]+mColorRadius.val[0] : 255;
mLowerBound.val[0] = minH;
mUpperBound.val[0] = maxH;
mLowerBound.val[1] = hsvColor.val[1] - mColorRadius.val[1];
mUpperBound.val[1] = hsvColor.val[1] + mColorRadius.val[1];
mLowerBound.val[2] = hsvColor.val[2] - mColorRadius.val[2];
mUpperBound.val[2] = hsvColor.val[2] + mColorRadius.val[2];
mLowerBound.val[3] = 0;
mUpperBound.val[3] = 255;
Mat spectrumHsv = new Mat(1, (int)(maxH-minH), CvType.CV_8UC3);
for (int j = 0; j < maxH-minH; j++) {
byte[] tmp = {(byte)(minH+j), (byte)255, (byte)255};
spectrumHsv.put(0, j, tmp);
}
Imgproc.cvtColor(spectrumHsv, mSpectrum, Imgproc.COLOR_HSV2RGB_FULL, 4);
}
public Mat getSpectrum() {
return mSpectrum;
}
public void setMinContourArea(double area) {
mMinContourArea = area;
}
public void process(Mat rgbaImage) {
if(!mDetectionIsRunning)
{
return;
}
int signalLength = (int)rgbaImage.size().height;
Mat grayVect = new Mat(1, signalLength, 4);
//transform to grayscale
Imgproc.cvtColor(rgbaImage, mGrayMat, Imgproc.COLOR_RGB2GRAY);
Mat transposedGrayMat = mGrayMat.t();
//sum to get 1D signal
Core.reduce(transposedGrayMat, grayVect, 0, Core.REDUCE_SUM, 4);
int bitPeriod = 14;
int preambleLength = 5;
int dataLength = 16;
double samplingLevel = 0.80;
int framePeriod = 295; //810~289; //(preambleLength+dataLength)*bitPeriod
double err = 0.03;
int maxPeriodError = 2;
Mat s0 = grayVect.submat(0, 1, 0, (int)(signalLength*2/13));
Mat s1 = grayVect.submat(0, 1, (int)(signalLength*6/13), (int)(signalLength*7/13));
Mat s2 = grayVect.submat(0, 1, (int)(signalLength*11/13), (int)(signalLength-1));
Mat[] ROIs = {s0, s1, s2};
int[] ROIOffsets = {0, (int)(signalLength*6/13),(int)(signalLength*11/13)};
float[] Xs = new float[ROIs.length];
float[] Ys = new float[ROIs.length];
Mat A = new Mat(ROIs.length, ROIs.length, 5);
Mat B = new Mat(ROIs.length, 1, 5);
Mat C = new Mat(ROIs.length, 1, 5);
//define linear equations matrices
for(int i = 0; i < ROIs.length; i++) {
//get points to determine quadratic equation
Core.MinMaxLocResult res = Core.minMaxLoc(ROIs[i]);
Xs[i] = (float)res.maxLoc.x + ROIOffsets[i]; //get index
Ys[i] = (float)res.maxVal; //get value
A.put(i, 0, Xs[i] * Xs[i]);
A.put(i, 1, Xs[i]);
A.put(i, 2, 1);
B.put(i, 0, Ys[i]);
}
//get quadratic euation
Core.solve(A, B, C);
detected = new int[(int)signalLength];
detectedMat = new Mat(1, signalLength, 4);
double C0 = C.get(0,0)[0];
double C1 = C.get(1,0)[0];
double C2 = C.get(2,0)[0];
//detect and store in detected vetor
for(int i = 0; i < signalLength; i++)
{
double tmp = C0*i*i + C1*i+ C2;
tmp *= samplingLevel;
if(grayVect.get(0,i)[0] > tmp){
detected[i] = 1;
detectedMat.put(0, i, 1);
}
else
{
detected[i] = -1;
detectedMat.put(0, i, -1);
}
}
// sampled preamble sequence
Mat sampledPreamble = new Mat(1, bitPeriod*preambleLength, 1);
int j = 0;
for(int bits = 0; bits < preambleLength; bits++) {
if (bits % 2 == 0) {
for (int i = 0; i < bitPeriod; i++) {
sampledPreamble.put(0, j, -2);
j++;
}
} else {
for (int i = 0; i < bitPeriod; i++) {
sampledPreamble.put(0, j, 2);
j++;
}
}
}
// correlate with sampled preamble sequence
Imgproc.filter2D(detectedMat, mCorr, -1, sampledPreamble); //detectedMat grayVect
ArrayList<Integer> mins = new ArrayList<Integer>();
ArrayList<Integer> maxs = new ArrayList<Integer>();
ArrayList<Integer> maxValues = new ArrayList<Integer>();
int prevDiff =(int)(mCorr.get(0, 0)[0] - mCorr.get(0, 1)[0]);
int i=1;
while(i<signalLength-1){
int currDiff = 0;
int zeroCount = 0;
while(currDiff == 0 && i<signalLength-1){
zeroCount++;
i++;
currDiff = (int)(mCorr.get(0, i-1)[0] - mCorr.get(0, i)[0]);
}
int signCurrDiff = Integer.signum(currDiff);
int signPrevDiff = Integer.signum(prevDiff);
if( signPrevDiff != signCurrDiff && signCurrDiff != 0){ //signSubDiff==0, the case when prev while ended bcoz of last elem
int index = i-1-(zeroCount)/2;
if(signPrevDiff == 1){
mins.add( index );
}else{
maxs.add( index );
maxValues.add((int)mCorr.get(0,index)[0]);
}
}
prevDiff = currDiff;
}
mMeasuredBitPeriod = bitPeriod;
int maxFreqSameLevel = detected.length/framePeriod + 1;
int trials =2*(detected.length/framePeriod); //maxValues.size();
ArrayList<ArrayList<Integer>> preamblesList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> maxsSorted = new ArrayList<Integer>(maxValues);
Collections.sort(maxsSorted);
for(int skip = 0; skip < trials; skip++)
{
ArrayList<Integer> preambles = new ArrayList<Integer>();
ArrayList<Integer> maxstemp = new ArrayList<Integer>(maxsSorted);
if(maxstemp.size() < trials)
{
Log.e("Error", "No preamble found!");
return;
}
int thismax = maxstemp.get(maxstemp.size()-skip-1);
int thisindex = maxValues.indexOf(thismax);
//drop last maximum
if(thisindex == (maxValues.size()-1))
{
continue;
}
//drop first maximum
if(thisindex == 0)
{
continue;
}
if(Collections.frequency(maxsSorted, thismax) > maxFreqSameLevel)
{
continue;
}
//this local maximum must be bigger than neighbor local maxima
if((thismax < maxValues.get(thisindex-1)) || (thismax < maxValues.get(thisindex+1)))
{
continue;
}
//the position of surrounding maxima must be close to bit period
if(Math.abs(Math.abs(maxs.get(thisindex) - maxs.get(thisindex-1)) - 2*bitPeriod) > maxPeriodError)
{
continue;
}
if(Math.abs(Math.abs(maxs.get(thisindex) - maxs.get(thisindex+1)) - 2*bitPeriod) > maxPeriodError)
{
continue;
}
double T = Math.abs(maxs.get(thisindex-1) - maxs.get(thisindex+1))/4.0;
mMeasuredBitPeriod = (T + mMeasuredBitPeriod)/2;
thisindex = maxs.get(thisindex);
//drop last and first maxima, due to mutuality
for(int idx = 1; idx < (maxs.size()-1); idx++) {
if(Collections.frequency(maxValues, maxValues.get(idx)) > maxFreqSameLevel)
{
continue;
}
double tmp = (1.0 * (Math.abs(thisindex - maxs.get(idx)) % framePeriod)) / framePeriod;
if((tmp < err) || (tmp > (1-err))) {
preambles.add(maxs.get(idx));
}
}
preamblesList.add(preambles);
}
ArrayList<Integer> preamblesNum = new ArrayList<Integer>();
for(int idx = 0; idx < preamblesList.size(); idx++)
{
ArrayList<Integer> tmp = preamblesList.get(idx);
preamblesNum.add(tmp.size());
}
int maxPreamblesNum;
try {
// pick the maximum number of preambles
maxPreamblesNum = Collections.max(preamblesNum);
}
catch(Exception e)
{
Log.e("Fatal detection error", "No preambles found!");
return;
}
// this should be detected with different shifts - if not, we are not locked
if (Collections.frequency(preamblesNum, maxPreamblesNum)!= maxPreamblesNum)
{
Log.e("Fatal detection error", "Failed to find preambles");
return;
}
else
{
//get SOF position of all detected frames
int correctPreamblesIdx = preamblesNum.indexOf(maxPreamblesNum);
// filter out uncomplete frames and get start-of-frames
ArrayList<Integer> tmp = preamblesList.get(correctPreamblesIdx);
ArrayList<Integer>SOFs = new ArrayList<Integer>();
for(int p = 0; p < tmp.size(); p++) {
if ((tmp.get(p) + framePeriod) < signalLength) {
// offset sampling points to middle and skip preamble
SOFs.add(tmp.get(p) + (int)(((preambleLength / 2.0)+0.5) * bitPeriod));
}
}
if(SOFs.size() <= 1)
{
return;
}
//create sorted list of received frames
ArrayList<Integer>frameList = new ArrayList<Integer>();
for(int sof = 0; sof < SOFs.size(); sof++)
{
int frame = 0;
for(int bits = 0; bits < dataLength; bits++) {
if (detected[SOFs.get(sof) + ((int)(bits * bitPeriod))] == 1)
frame |= (1 << bits);
}
frameList.add(frame);
}
mFrames = frameList;
}
}
public List<MatOfPoint> getContours() {
return mContours;
}
}