Add helper function for kernel normalization
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
parent
a5e3837f6d
commit
ed9c7064de
20
src/kernel.c
20
src/kernel.c
|
@ -121,4 +121,24 @@ void sum_kernel_preprocess(conv *map) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a convolution kernel.
|
||||||
|
*
|
||||||
|
* @param[in,out] kern the kernel
|
||||||
|
*/
|
||||||
|
void normalize_conv_kern(conv *kern) {
|
||||||
|
double sum = 0.0;
|
||||||
|
for (int i = 0; i < kern->w * kern->h; i++) {
|
||||||
|
sum += kern->data[i];
|
||||||
|
}
|
||||||
|
double factor = 1.0 / sum;
|
||||||
|
for (int i = 0; i < kern->w * kern->h; i++) {
|
||||||
|
kern->data[i] *= factor;
|
||||||
|
}
|
||||||
|
if (kern->rsum) {
|
||||||
|
free(kern->rsum);
|
||||||
|
kern->rsum = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// vim: set noet sw=8 ts=8 :
|
// vim: set noet sw=8 ts=8 :
|
||||||
|
|
|
@ -25,6 +25,13 @@ conv *gaussian_kernel(double r);
|
||||||
/// shadow_sum[x*d+y] is the sum of the kernel from (0, 0) to (x, y), inclusive
|
/// shadow_sum[x*d+y] is the sum of the kernel from (0, 0) to (x, y), inclusive
|
||||||
void sum_kernel_preprocess(conv *map);
|
void sum_kernel_preprocess(conv *map);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalize a convolution kernel.
|
||||||
|
*
|
||||||
|
* @param[in,out] kern the kernel
|
||||||
|
*/
|
||||||
|
void normalize_conv_kern(conv *kern);
|
||||||
|
|
||||||
static inline void free_conv(conv *k) {
|
static inline void free_conv(conv *k) {
|
||||||
free(k->rsum);
|
free(k->rsum);
|
||||||
free(k);
|
free(k);
|
||||||
|
|
Loading…
Reference in New Issue