@@ -1205,12 +1205,11 @@ void Sample_Derivative_Response_Radius6(const Mat &Lx, const Mat &Ly,
1205
1205
{ 0 .00344629f , 0 .00318132f , 0 .00250252f , 0 .00167749f , 0 .00095820f , 0 .00046640f , 0 .00019346f },
1206
1206
{ 0 .00142946f , 0 .00131956f , 0 .00103800f , 0 .00069579f , 0 .00039744f , 0 .00019346f , 0 .00008024f }
1207
1207
};
1208
- static const int id[] = { 6 , 5 , 4 , 3 , 2 , 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 };
1209
1208
static const struct gtable
1210
1209
{
1211
1210
float weight[109 ];
1212
- int8_t xidx[109 ];
1213
- int8_t yidx[109 ];
1211
+ int xidx[109 ];
1212
+ int yidx[109 ];
1214
1213
1215
1214
explicit gtable (void )
1216
1215
{
@@ -1219,29 +1218,28 @@ void Sample_Derivative_Response_Radius6(const Mat &Lx, const Mat &Ly,
1219
1218
for (int i = -6 ; i <= 6 ; ++i) {
1220
1219
for (int j = -6 ; j <= 6 ; ++j) {
1221
1220
if (i*i + j*j < 36 ) {
1222
- weight[k] = gauss25[id[i + 6 ]][id[j + 6 ]];
1223
- yidx[k] = static_cast <int8_t >(i);
1224
- xidx[k] = static_cast <int8_t >(j);
1221
+ CV_Assert (k < 109 );
1222
+ weight[k] = gauss25[abs (i)][abs (j)];
1223
+ yidx[k] = i;
1224
+ xidx[k] = j;
1225
1225
++k;
1226
1226
}
1227
1227
}
1228
1228
}
1229
- CV_DbgAssert (k == 109 );
1230
1229
}
1231
1230
} g;
1232
1231
1233
- const float * lx = Lx.ptr <float >(0 );
1234
- const float * ly = Ly.ptr <float >(0 );
1235
- int cols = Lx.cols ;
1232
+ CV_Assert (x0 - 6 * scale >= 0 && x0 + 6 * scale < Lx.cols );
1233
+ CV_Assert (y0 - 6 * scale >= 0 && y0 + 6 * scale < Lx.rows );
1236
1234
1237
- for (int i = 0 ; i < 109 ; i++) {
1238
- int j = (y0 + g.yidx [i] * scale) * cols + (x0 + g.xidx [i] * scale);
1239
-
1240
- resX[i] = g.weight [i] * lx[j];
1241
- resY[i] = g.weight [i] * ly[j];
1235
+ for (int i = 0 ; i < 109 ; i++)
1236
+ {
1237
+ int y = y0 + g.yidx [i] * scale;
1238
+ int x = x0 + g.xidx [i] * scale;
1242
1239
1243
- CV_DbgAssert (isfinite (resX[i]));
1244
- CV_DbgAssert (isfinite (resY[i]));
1240
+ float w = g.weight [i];
1241
+ resX[i] = w * Lx.at <float >(y, x);
1242
+ resY[i] = w * Ly.at <float >(y, x);
1245
1243
}
1246
1244
}
1247
1245
@@ -1250,7 +1248,7 @@ void Sample_Derivative_Response_Radius6(const Mat &Lx, const Mat &Ly,
1250
1248
* @param a[] Input floating point array to sort
1251
1249
* @param n The length of a[]
1252
1250
* @param quantum The interval to convert a[i]'s float values to integers
1253
- * @param max The upper bound of a[], meaning a[ i] must be in [0, max]
1251
+ * @param nkeys a[ i] < nkeys * quantum
1254
1252
* @param idx[] Output array of the indices: a[idx[i]] forms a sorted array
1255
1253
* @param cum[] Output array of the starting indices of quantized floats
1256
1254
* @note The values of a[] in [k*quantum, (k + 1)*quantum) is labeled by
@@ -1260,25 +1258,35 @@ void Sample_Derivative_Response_Radius6(const Mat &Lx, const Mat &Ly,
1260
1258
*/
1261
1259
static inline
1262
1260
void quantized_counting_sort (const float a[], const int n,
1263
- const float quantum, const float max ,
1264
- uint8_t idx[], uint8_t cum[])
1261
+ const float quantum, const int nkeys ,
1262
+ int idx[/* n */ ], int cum[/* nkeys + 1 */ ])
1265
1263
{
1266
- const int nkeys = (int )(max / quantum);
1267
-
1268
- // The size of cum[] must be nkeys + 1
1269
- memset (cum, 0 , nkeys + 1 );
1264
+ memset (cum, 0 , sizeof (cum[0 ]) * (nkeys + 1 ));
1270
1265
1271
1266
// Count up the quantized values
1272
1267
for (int i = 0 ; i < n; i++)
1273
- cum[(int )(a[i] / quantum)]++;
1268
+ {
1269
+ int b = (int )(a[i] / quantum);
1270
+ if (b < 0 || b >= nkeys)
1271
+ b = 0 ;
1272
+ cum[b]++;
1273
+ }
1274
1274
1275
1275
// Compute the inclusive prefix sum i.e. the end indices; cum[nkeys] is the total
1276
1276
for (int i = 1 ; i <= nkeys; i++)
1277
+ {
1277
1278
cum[i] += cum[i - 1 ];
1279
+ }
1280
+ CV_Assert (cum[nkeys] == n);
1278
1281
1279
1282
// Generate the sorted indices; cum[] becomes the exclusive prefix sum i.e. the start indices of keys
1280
1283
for (int i = 0 ; i < n; i++)
1281
- idx[--cum[(int )(a[i] / quantum)]] = static_cast <uint8_t >(i);
1284
+ {
1285
+ int b = (int )(a[i] / quantum);
1286
+ if (b < 0 || b >= nkeys)
1287
+ b = 0 ;
1288
+ idx[--cum[b]] = i;
1289
+ }
1282
1290
}
1283
1291
1284
1292
/* *
@@ -1309,17 +1317,18 @@ void Compute_Main_Orientation(KeyPoint& kpt, const std::vector<Evolution>& evolu
1309
1317
// Sort by the angles; angles are labeled by slices of 0.15 radian
1310
1318
const int slices = 42 ;
1311
1319
const float ang_step = (float )(2.0 * CV_PI / slices);
1312
- uint8_t slice[slices + 1 ];
1313
- uint8_t sorted_idx[ang_size];
1314
- quantized_counting_sort (Ang, ang_size, ang_step, ( float )( 2.0 * CV_PI) , sorted_idx, slice);
1320
+ int slice[slices + 1 ];
1321
+ int sorted_idx[ang_size];
1322
+ quantized_counting_sort (Ang, ang_size, ang_step, slices , sorted_idx, slice);
1315
1323
1316
1324
// Find the main angle by sliding a window of 7-slice size(=PI/3) around the keypoint
1317
1325
const int win = 7 ;
1318
1326
1319
1327
float maxX = 0 .0f , maxY = 0 .0f ;
1320
1328
for (int i = slice[0 ]; i < slice[win]; i++) {
1321
- maxX += resX[sorted_idx[i]];
1322
- maxY += resY[sorted_idx[i]];
1329
+ const int idx = sorted_idx[i];
1330
+ maxX += resX[idx];
1331
+ maxY += resY[idx];
1323
1332
}
1324
1333
float maxNorm = maxX * maxX + maxY * maxY;
1325
1334
@@ -1330,8 +1339,9 @@ void Compute_Main_Orientation(KeyPoint& kpt, const std::vector<Evolution>& evolu
1330
1339
1331
1340
float sumX = 0 .0f , sumY = 0 .0f ;
1332
1341
for (int i = slice[sn]; i < slice[sn + win]; i++) {
1333
- sumX += resX[sorted_idx[i]];
1334
- sumY += resY[sorted_idx[i]];
1342
+ const int idx = sorted_idx[i];
1343
+ sumX += resX[idx];
1344
+ sumY += resY[idx];
1335
1345
}
1336
1346
1337
1347
float norm = sumX * sumX + sumY * sumY;
@@ -1347,12 +1357,14 @@ void Compute_Main_Orientation(KeyPoint& kpt, const std::vector<Evolution>& evolu
1347
1357
1348
1358
float sumX = 0 .0f , sumY = 0 .0f ;
1349
1359
for (int i = slice[sn]; i < slice[slices]; i++) {
1350
- sumX += resX[sorted_idx[i]];
1351
- sumY += resY[sorted_idx[i]];
1360
+ const int idx = sorted_idx[i];
1361
+ sumX += resX[idx];
1362
+ sumY += resY[idx];
1352
1363
}
1353
1364
for (int i = slice[0 ]; i < slice[remain]; i++) {
1354
- sumX += resX[sorted_idx[i]];
1355
- sumY += resY[sorted_idx[i]];
1365
+ const int idx = sorted_idx[i];
1366
+ sumX += resX[idx];
1367
+ sumY += resY[idx];
1356
1368
}
1357
1369
1358
1370
float norm = sumX * sumX + sumY * sumY;
0 commit comments