Skip to content

Instantly share code, notes, and snippets.

@brunoperezm
Created April 29, 2022 12:24
Show Gist options
  • Save brunoperezm/31724b4256c18a79da111dd13b07dca5 to your computer and use it in GitHub Desktop.
Save brunoperezm/31724b4256c18a79da111dd13b07dca5 to your computer and use it in GitHub Desktop.
void process_file(ImageData *image, ImageData *kernel) {
const u_int32_t window_center_x = kernel->width / 2;
const u_int32_t window_center_y = kernel->height / 2;
const u_int32_t window_center_max_x = image->width - (kernel->width / 2);
const u_int32_t window_center_max_y = image->height - (kernel->height / 2);
u_int32_t kernel_bytes_per_pixel = kernel->rowbytes / kernel->width;
u_int32_t image_bytes_per_pixel = image->rowbytes / image->width;
u_int32_t min_sum = -1;
u_int32_t min_w_y = 0;
u_int32_t min_w_x = 0;
for (u_int32_t w_y = window_center_y; w_y < window_center_max_y; w_y++) {
for (u_int32_t w_x = window_center_x; w_x < window_center_max_x; w_x++) {
// Acá estoy iterando el centro de la ventana (w_x, w_y).
// Ahora hago mi matriz ventana
png_byte matriz_ventana[kernel->width][kernel->height];
for (u_int32_t x = 0; x < kernel->width; x++) {
for (u_int32_t y = 0; y < kernel->width; y++) {
matriz_ventana[x][y] = image->rows[y + w_y - window_center_y][(x + w_x - window_center_x) *
image_bytes_per_pixel];
}
}
// Calculo la distancia euclidiana de esta ventana en particular
u_int32_t sum = 0;
for (u_int32_t v = 0; v < kernel->height - 1; v++) {
for (u_int32_t u = 0; u < kernel->width - 1; u++) {
u_int32_t before_pow = kernel->rows[v][u*kernel_bytes_per_pixel] - matriz_ventana[u][v];
sum += before_pow * before_pow;
}
}
// Si es menor al mínimo lo reemplazo
if (sum < min_sum) {
min_sum = sum;
min_w_y = w_y;
min_w_x = w_x;
}
}
}
printf("Encontre mi mínimo: center es [%d %d]\n", min_w_x, min_w_y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment