Skip to content

Commit 8ee8710

Browse files
remioukratmshabunin
authored andcommitted
Modifications to reduce the code through a loop
AKAZE refactoring: fixed indentation and made more minor modifications
1 parent 715b88c commit 8ee8710

File tree

1 file changed

+62
-178
lines changed

1 file changed

+62
-178
lines changed

modules/features2d/src/kaze/AKAZEFeatures.cpp

Lines changed: 62 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,17 +1117,19 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons
11171117
float di = 0.0, dx = 0.0, dy = 0.0;
11181118
float ri = 0.0, rx = 0.0, ry = 0.0, xf = 0.0, yf = 0.0;
11191119
float sample_x = 0.0, sample_y = 0.0, ratio = 0.0;
1120-
int x1 = 0, y1 = 0, sample_step = 0, pattern_size = 0;
1120+
int x1 = 0, y1 = 0;
11211121
int level = 0, nsamples = 0, scale = 0;
11221122
int dcount1 = 0, dcount2 = 0;
11231123

11241124
const AKAZEOptions & options = *options_;
11251125
const std::vector<TEvolution>& evolution = *evolution_;
11261126

11271127
// Matrices for the M-LDB descriptor
1128-
Mat values_1 = Mat::zeros(4, options.descriptor_channels, CV_32FC1);
1129-
Mat values_2 = Mat::zeros(9, options.descriptor_channels, CV_32FC1);
1130-
Mat values_3 = Mat::zeros(16, options.descriptor_channels, CV_32FC1);
1128+
Mat values[3] = {
1129+
Mat::zeros(4, options.descriptor_channels, CV_32FC1),
1130+
Mat::zeros(9, options.descriptor_channels, CV_32FC1),
1131+
Mat::zeros(16, options.descriptor_channels, CV_32FC1)
1132+
};
11311133

11321134
// Get the information from the keypoint
11331135
ratio = (float)(1 << kpt.octave);
@@ -1136,190 +1138,72 @@ void Upright_MLDB_Full_Descriptor_Invoker::Get_Upright_MLDB_Full_Descriptor(cons
11361138
yf = kpt.pt.y / ratio;
11371139
xf = kpt.pt.x / ratio;
11381140

1139-
// First 2x2 grid
1140-
pattern_size = options_->descriptor_pattern_size;
1141-
sample_step = pattern_size;
1142-
1143-
for (int i = -pattern_size; i < pattern_size; i += sample_step) {
1144-
for (int j = -pattern_size; j < pattern_size; j += sample_step) {
1145-
di = dx = dy = 0.0;
1146-
nsamples = 0;
1147-
1148-
for (int k = i; k < i + sample_step; k++) {
1149-
for (int l = j; l < j + sample_step; l++) {
1150-
1151-
// Get the coordinates of the sample point
1152-
sample_y = yf + l*scale;
1153-
sample_x = xf + k*scale;
1154-
1155-
y1 = fRound(sample_y);
1156-
x1 = fRound(sample_x);
1157-
1158-
ri = *(evolution[level].Lt.ptr<float>(y1)+x1);
1159-
rx = *(evolution[level].Lx.ptr<float>(y1)+x1);
1160-
ry = *(evolution[level].Ly.ptr<float>(y1)+x1);
1161-
1162-
di += ri;
1163-
dx += rx;
1164-
dy += ry;
1165-
nsamples++;
1166-
}
1167-
}
1168-
1169-
di /= nsamples;
1170-
dx /= nsamples;
1171-
dy /= nsamples;
1172-
1173-
*(values_1.ptr<float>(dcount2)) = di;
1174-
*(values_1.ptr<float>(dcount2)+1) = dx;
1175-
*(values_1.ptr<float>(dcount2)+2) = dy;
1176-
dcount2++;
1177-
}
1178-
}
1179-
1180-
// Do binary comparison first level
1181-
for (int i = 0; i < 4; i++) {
1182-
for (int j = i + 1; j < 4; j++) {
1183-
if (*(values_1.ptr<float>(i)) > *(values_1.ptr<float>(j))) {
1184-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1185-
}
1186-
dcount1++;
1187-
1188-
if (*(values_1.ptr<float>(i)+1) > *(values_1.ptr<float>(j)+1)) {
1189-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1190-
}
1191-
dcount1++;
1192-
1193-
if (*(values_1.ptr<float>(i)+2) > *(values_1.ptr<float>(j)+2)) {
1194-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1195-
}
1196-
dcount1++;
1197-
}
1198-
}
1199-
1200-
// Second 3x3 grid
1201-
sample_step = static_cast<int>(ceil(pattern_size*2. / 3.));
1202-
dcount2 = 0;
1203-
1204-
for (int i = -pattern_size; i < pattern_size; i += sample_step) {
1205-
for (int j = -pattern_size; j < pattern_size; j += sample_step) {
1206-
di = dx = dy = 0.0;
1207-
nsamples = 0;
1208-
1209-
for (int k = i; k < i + sample_step; k++) {
1210-
for (int l = j; l < j + sample_step; l++) {
1211-
1212-
// Get the coordinates of the sample point
1213-
sample_y = yf + l*scale;
1214-
sample_x = xf + k*scale;
1215-
1216-
y1 = fRound(sample_y);
1217-
x1 = fRound(sample_x);
1218-
1219-
ri = *(evolution[level].Lt.ptr<float>(y1)+x1);
1220-
rx = *(evolution[level].Lx.ptr<float>(y1)+x1);
1221-
ry = *(evolution[level].Ly.ptr<float>(y1)+x1);
1222-
1223-
di += ri;
1224-
dx += rx;
1225-
dy += ry;
1226-
nsamples++;
1141+
// For 2x2 grid, 3x3 grid and 4x4 grid
1142+
const int pattern_size = options_->descriptor_pattern_size;
1143+
int sample_step[3] = {
1144+
pattern_size,
1145+
static_cast<int>(ceil(pattern_size*2./3.)),
1146+
pattern_size / 2
1147+
};
1148+
1149+
// For the three grids
1150+
for (int z = 0; z < 3; z++) {
1151+
dcount2 = 0;
1152+
const int step = sample_step[z];
1153+
for (int i = -pattern_size; i < pattern_size; i += step) {
1154+
for (int j = -pattern_size; j < pattern_size; j += step) {
1155+
di = dx = dy = 0.0;
1156+
nsamples = 0;
1157+
1158+
for (int k = i; k < i + step; k++) {
1159+
for (int l = j; l < j + step; l++) {
1160+
1161+
// Get the coordinates of the sample point
1162+
sample_y = yf + l*scale;
1163+
sample_x = xf + k*scale;
1164+
1165+
y1 = fRound(sample_y);
1166+
x1 = fRound(sample_x);
1167+
1168+
ri = *(evolution[level].Lt.ptr<float>(y1)+x1);
1169+
rx = *(evolution[level].Lx.ptr<float>(y1)+x1);
1170+
ry = *(evolution[level].Ly.ptr<float>(y1)+x1);
1171+
1172+
di += ri;
1173+
dx += rx;
1174+
dy += ry;
1175+
nsamples++;
1176+
}
12271177
}
1228-
}
1229-
1230-
di /= nsamples;
1231-
dx /= nsamples;
1232-
dy /= nsamples;
1233-
1234-
*(values_2.ptr<float>(dcount2)) = di;
1235-
*(values_2.ptr<float>(dcount2)+1) = dx;
1236-
*(values_2.ptr<float>(dcount2)+2) = dy;
1237-
dcount2++;
1238-
}
1239-
}
12401178

1241-
//Do binary comparison second level
1242-
dcount2 = 0;
1243-
for (int i = 0; i < 9; i++) {
1244-
for (int j = i + 1; j < 9; j++) {
1245-
if (*(values_2.ptr<float>(i)) > *(values_2.ptr<float>(j))) {
1246-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1247-
}
1248-
dcount1++;
1179+
di /= nsamples;
1180+
dx /= nsamples;
1181+
dy /= nsamples;
12491182

1250-
if (*(values_2.ptr<float>(i)+1) > *(values_2.ptr<float>(j)+1)) {
1251-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1183+
float *val = values[z].ptr<float>(dcount2);
1184+
*(val) = di;
1185+
*(val+1) = dx;
1186+
*(val+2) = dy;
1187+
dcount2++;
12521188
}
1253-
dcount1++;
1254-
1255-
if (*(values_2.ptr<float>(i)+2) > *(values_2.ptr<float>(j)+2)) {
1256-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1257-
}
1258-
dcount1++;
12591189
}
1260-
}
1261-
1262-
// Third 4x4 grid
1263-
sample_step = pattern_size / 2;
1264-
dcount2 = 0;
1265-
1266-
for (int i = -pattern_size; i < pattern_size; i += sample_step) {
1267-
for (int j = -pattern_size; j < pattern_size; j += sample_step) {
1268-
di = dx = dy = 0.0;
1269-
nsamples = 0;
1270-
1271-
for (int k = i; k < i + sample_step; k++) {
1272-
for (int l = j; l < j + sample_step; l++) {
1273-
1274-
// Get the coordinates of the sample point
1275-
sample_y = yf + l*scale;
1276-
sample_x = xf + k*scale;
12771190

1278-
y1 = fRound(sample_y);
1279-
x1 = fRound(sample_x);
1280-
1281-
ri = *(evolution[level].Lt.ptr<float>(y1)+x1);
1282-
rx = *(evolution[level].Lx.ptr<float>(y1)+x1);
1283-
ry = *(evolution[level].Ly.ptr<float>(y1)+x1);
1284-
1285-
di += ri;
1286-
dx += rx;
1287-
dy += ry;
1288-
nsamples++;
1191+
// Do binary comparison
1192+
const int num = (z + 2) * (z + 2);
1193+
for (int i = 0; i < num; i++) {
1194+
for (int j = i + 1; j < num; j++) {
1195+
const float * valI = values[z].ptr<float>(i);
1196+
const float * valJ = values[z].ptr<float>(j);
1197+
for (int k = 0; k < 3; ++k) {
1198+
if (*(valI + k) > *(valJ + k)) {
1199+
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1200+
}
1201+
dcount1++;
12891202
}
12901203
}
1291-
1292-
di /= nsamples;
1293-
dx /= nsamples;
1294-
dy /= nsamples;
1295-
1296-
*(values_3.ptr<float>(dcount2)) = di;
1297-
*(values_3.ptr<float>(dcount2)+1) = dx;
1298-
*(values_3.ptr<float>(dcount2)+2) = dy;
1299-
dcount2++;
13001204
}
1301-
}
1302-
1303-
//Do binary comparison third level
1304-
dcount2 = 0;
1305-
for (int i = 0; i < 16; i++) {
1306-
for (int j = i + 1; j < 16; j++) {
1307-
if (*(values_3.ptr<float>(i)) > *(values_3.ptr<float>(j))) {
1308-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1309-
}
1310-
dcount1++;
1311-
1312-
if (*(values_3.ptr<float>(i)+1) > *(values_3.ptr<float>(j)+1)) {
1313-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1314-
}
1315-
dcount1++;
13161205

1317-
if (*(values_3.ptr<float>(i)+2) > *(values_3.ptr<float>(j)+2)) {
1318-
desc[dcount1 / 8] |= (1 << (dcount1 % 8));
1319-
}
1320-
dcount1++;
1321-
}
1322-
}
1206+
} // for (int z = 0; z < 3; z++)
13231207
}
13241208

13251209
void MLDB_Full_Descriptor_Invoker::MLDB_Fill_Values(float* values, int sample_step, int level,

0 commit comments

Comments
 (0)