-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayUtils.java
381 lines (344 loc) · 15.9 KB
/
ArrayUtils.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
/*
* Copyright (C) Photon Vision.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.photonvision.calibrator;
import java.util.zip.Deflater;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* ArrayUtils class */
/* ArrayUtils class */
/* ArrayUtils class */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
public class ArrayUtils
{
private static final Logger logger = new Logger(ArrayUtils.class, LogGroup.General);
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* argmax */
/* argmax */
/* argmax */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/**
* Find index of the maximum value in an array
* @param array an array
* @return the argmax (lowest index in case of duplicate values)
*/
static int argmax(double[] array)
{
int locationOfExtreme = 0;
double extreme = array[locationOfExtreme];
for (int i = 1; i < array.length; i++)
{
if (array[i] > extreme)
{
extreme = array[i];
locationOfExtreme = i;
}
}
return locationOfExtreme;
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* argmin */
/* argmin */
/* argmin */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/**
* Find index of the minimum value in an array
* @param array an array
* @return the argmin (lowest index in case of duplicate values)
*/
static int argmin(double[] array)
{
int locationOfExtreme = 0;
double extreme = array[locationOfExtreme];
for (int i = 1; i < array.length; i++)
{
if (array[i] < extreme)
{
extreme = array[i];
locationOfExtreme = i;
}
}
return locationOfExtreme;
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* isAllTrue */
/* isAllTrue */
/* isAllTrue */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/**
* Test for all values in a boolean array are true
* @param array
* @return true if all true or false if any false
*/
static boolean isAllTrue(boolean[] array)
{
for (boolean element : array)
{
if ( ! element) return false;
}
return true;
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* brief */
/* brief */
/* brief */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/**
* Partially print an OpenCV Mat<p>
* Convenience method of brief(Mat, int, int, int, int) with defaults
* of first 4 rows, last 4 rows, first 12 values in a row, last 12 values in a row.<p>
* Each ".r." or ".c." represents 2000 items or the remainder.
* @param mat OpenCV Mat to be partially printed
* @return String of the values in the corners of the Mat if large or entire Mat if small
*/
static String brief(Mat mat)
{
return brief(mat, 4, 4, 12, 12);
}
/**
* Partially print an OpenCV Mat<p>
* Each ".r." or ".c." represents 2000 items or the remainder.
* @param mat OpenCV Mat to be partially printed<p>
* @param firstRows - count first few rows
* @param lastRows - count last few rows
* @param firstRowData - count first few column/channel values
* @param lastRowData - count last few column/channel values
* @return String of the values in the corners of the Mat if large or entire Mat if small
*/
static String brief(Mat mat, int firstRows, int lastRows, int firstRowData, int lastRowData)
{
final int acknowledgeRowsSkipped = 2000; // skip count to print an icon
final int acknowledgeRowDataSkipped = 2000; // skip count to print an icon
StringBuilder sb = new StringBuilder();
sb.append(mat);
sb.append("\n");
double[] matRowD = null;
float[] matRowF = null;
byte[] matRowB = null;
int[] matRowI = null;
short[] matRowS = null;
switch (CvType.depth(mat.type()))
{
case CvType.CV_64F: // double
matRowD = new double[mat.channels()*mat.cols()];
break;
case CvType.CV_32F: // float
matRowF = new float[mat.channels()*mat.cols()];
break;
case CvType.CV_8U: // byte
case CvType.CV_8S:
matRowB = new byte[mat.channels()*mat.cols()];
break;
case CvType.CV_32S: // int
matRowI = new int[mat.channels()*mat.cols()];
break;
case CvType.CV_16U: // short
case CvType.CV_16S:
matRowS = new short[mat.channels()*mat.cols()];
break;
default:
logger.error("Print Mat Error - Unknown OpenCV Mat depth. Not printing requested data. " + CvType.depth(mat.type()));
return "Print Mat Error";
}
int printCountRow = 0;
int printCountColChan = 0;
boolean skippedRow = false;
for (int row = 0; row < mat.rows(); row++)
{
if (row >= firstRows && row < mat.rows() - lastRows)
{
skippedRow = true;
if (printCountRow % acknowledgeRowsSkipped == 0)
{
printCountRow = 0;
sb.append(".r.");
}
printCountRow++;
continue;
}
if (skippedRow)
{
sb.append("\n");
skippedRow = false;
}
switch (CvType.depth(mat.type()))
{
case CvType.CV_64F: // double
mat.get(row, 0, matRowD);
break;
case CvType.CV_32F: // float
mat.get(row, 0, matRowF);
break;
case CvType.CV_8U: // byte
case CvType.CV_8S:
mat.get(row, 0, matRowB);
break;
case CvType.CV_32S: // int
mat.get(row, 0, matRowI);
break;
case CvType.CV_16U: // short
case CvType.CV_16S:
mat.get(row, 0, matRowS);
break;
default:
return "ArrayUtils.brief(Mat) should not be here.";
}
printCountColChan = 0;
for (int colChan = 0; colChan < mat.cols()*mat.channels(); colChan++)
{
if (colChan >= firstRowData && colChan < mat.cols()*mat.channels() - lastRowData)
{
if (printCountColChan % acknowledgeRowDataSkipped == 0)
{
printCountColChan = 0;
sb.append(".c. ");
}
printCountColChan++;
continue;
}
switch (CvType.depth(mat.type()))
{
case CvType.CV_64F: // double
sb.append(matRowD[colChan]);
break;
case CvType.CV_32F: // float
sb.append(matRowF[colChan]);
break;
case CvType.CV_8U: // byte
case CvType.CV_8S:
sb.append(matRowB[colChan]);
break;
case CvType.CV_32S: // int
sb.append(matRowI[colChan]);
break;
case CvType.CV_16U: // short
case CvType.CV_16S:
sb.append(matRowS[colChan]);
break;
default:
return "ArrayUtils.brief(Mat) should not be here.";
}
sb.append(" ");
}
sb.append("\n");
}
return sb.toString();
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* intToByteArray */
/* intToByteArray */
/* intToByteArray */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/**
* Convert one 4-byte int variable to the equivalent four element byte array
* The 4 bytes are located starting at "offset" from the designated output array
* @param toBeConvertedInt - effectively considered an unsigned int; unless you like the complemented value of a negative number you should limit this to positive numbers
* @param dst - array segment for the converted int; array length must be >= offset + 4
* @param offset - starting element to be changed in the output array (number of changed elements is always 4 for the int type)
* @return byte array of the input
*/
static void intToByteArray(int toBeConvertedInt, byte[] dst, int offset)
{
dst[offset + 3] = (byte) (toBeConvertedInt & 0xff); // least significant byte
toBeConvertedInt >>= 8;
dst[offset + 2] = (byte) (toBeConvertedInt & 0xff);
toBeConvertedInt >>= 8;
dst[offset + 1] = (byte) (toBeConvertedInt & 0xff);
toBeConvertedInt >>= 8;
dst[offset + 0] = (byte) (toBeConvertedInt); // most significant byte
}
/**
*
* @param bytesToCompress
* @return
*/
static byte[] compress(byte[] bytesToCompress)
{
Deflater deflater = new Deflater();
deflater.setInput(bytesToCompress);
deflater.finish();
byte[] bytesCompressed = new byte[bytesToCompress.length]; // assumes compressed data no longer than input - not always true but it is for the ChArUcoBoard
int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
byte[] returnValues = new byte[numberOfBytesAfterCompression];
System.arraycopy
(
bytesCompressed,
0,
returnValues,
0,
numberOfBytesAfterCompression
);
return returnValues;
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* ArrayUtils constructor */
/* ArrayUtils constructor */
/* ArrayUtils constructor */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
private ArrayUtils()
{
throw new UnsupportedOperationException("This is a utility class");
}
}
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/* */
/* End ArrayUtils class */
/* End ArrayUtils class */
/* End ArrayUtils class */
/* */
/*-------------------------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------------*/
/*
CV_8U - 8-bit unsigned integers ( 0..255 )
CV_8S - 8-bit signed integers ( -128..127 )
CV_16U - 16-bit unsigned integers ( 0..65535 )
CV_16S - 16-bit signed integers ( -32768..32767 )
CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )
*/