// Copyright 2011 Google Inc. All Rights Reserved. // // Use of this source code is governed by a BSD-style license // that can be found in the COPYING file in the root of the source // tree. An additional intellectual property rights grant can be found // in the file PATENTS. All contributing project authors may // be found in the AUTHORS file in the root of the source tree. // ----------------------------------------------------------------------------- // // Quantize levels for specified number of quantization-levels ([2, 256]). // Min and max values are preserved (usual 0 and 255 for alpha plane). // // Author: Skal (pascal.massimino@gmail.com)
#include <assert.h>
#include"src/utils/quant_levels_utils.h"
#define NUM_SYMBOLS 256
#define MAX_ITER 6 // Maximum number of convergence steps. #define ERROR_THRESHOLD 1e-4 // MSE stopping criterion.
// k-Means iterations. for (iter = 0; iter < MAX_ITER; ++iter) { double q_sum[NUM_SYMBOLS] = { 0 }; double q_count[NUM_SYMBOLS] = { 0 }; int s, slot = 0;
// Assign classes to representatives. for (s = min_s; s <= max_s; ++s) { // Keep track of the nearest neighbour 'slot' while (slot < num_levels - 1 &&
2 * s > inv_q_level[slot] + inv_q_level[slot + 1]) {
++slot;
} if (freq[s] > 0) {
q_sum[slot] += s * freq[s];
q_count[slot] += freq[s];
}
q_level[s] = slot;
}
// Assign new representatives to classes. if (num_levels > 2) { for (slot = 1; slot < num_levels - 1; ++slot) { constdouble count = q_count[slot]; if (count > 0.) {
inv_q_level[slot] = q_sum[slot] / count;
}
}
}
// Compute convergence error.
err = 0.; for (s = min_s; s <= max_s; ++s) { constdouble error = s - inv_q_level[q_level[s]];
err += freq[s] * error * error;
}
// Check for convergence: we stop as soon as the error is no // longer improving. if (last_err - err < err_threshold) break;
last_err = err;
}
// Remap the alpha plane to quantized values.
{ // double->int rounding operation can be costly, so we do it // once for all before remapping. We also perform the data[] -> slot // mapping, while at it (avoid one indirection in the final loop).
uint8_t map[NUM_SYMBOLS]; int s;
size_t n; for (s = min_s; s <= max_s; ++s) { constint slot = q_level[s];
map[s] = (uint8_t)(inv_q_level[slot] + .5);
} // Final pass. for (n = 0; n < data_size; ++n) {
data[n] = map[data[n]];
}
}
End: // Store sum of squared error if needed. if (sse != NULL) *sse = (uint64_t)err;
return 1;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.