ip_lib.c
/* Crea un filtro gaussiano per la rimozione del rumore */
Crea un filtro gaussiano per la rimozione del rumore
Crea un filtro gaussiano per la rimozione del rumore
ip_mat *create_gaussian_filter(unsigned int h, unsigned int w, unsigned int k, float sigma)
{
    if (h >= 3 && w >= 3 && k > 0 && sigma > 0)
    {
        unsigned int cx, cy, row, col, ch;
        float *sums = (float *)malloc(sizeof(float) * k);
        ip_mat *out;
        out = ip_mat_create(h, w, k, 0.0);
        cx = w / 2;
        cy = h / 2;
        for (ch = 0; ch < k; ch++)
        {
            sums[ch] = 0.0;
            for (row = 0; row < h; row++)
            {
                for (col = 0; col < w; col++)
                {
                    int x, y;
                    float val;
                    x = row - cx;
                    y = col - cy;
                    val = (1. / (2. * PI * sigma * sigma)) * exp(-(x * x + y * y) / (2. * sigma * sigma)); 
                    set_val(out, row, col, ch, val);                                                       
                    sums[ch] += get_val(out, row, col, ch);
                }
            }
        }
        for (ch = 0; ch < k; ch++)
        {
            for (row = 0; row < h; row++)
            {
                for (col = 0; col < w; col++)
                {
                    float new_val;
                    new_val = get_val(out, row, col, ch) / sums[ch];
                    set_val(out, row, col, ch, new_val);
                }
            }
        }
        free(sums);
        return out;
    }
    else
    {
        printf("Invalid parameters for creating a gaussian filter\n");
        exit(1);
    }
}
ip_mat * create_gaussian_filter ( unsigned int h , unsigned int w , unsigned int k , float sigma ) { if ( h >= 3 && w >= 3 && k > 0 && sigma > 0 ) { unsigned int cx , cy , row , col , ch ; float * sums = ( float * ) malloc ( sizeof ( float ) * k ) ; ip_mat * out ; out = ip_mat_create ( h , w , k , 0.0 ) ; cx = w / 2 ; cy = h / 2 ; for ( ch = 0 ; ch < k ; ch ++ ) { sums [ ch ] = 0.0 ; for ( row = 0 ; row < h ; row ++ ) { for ( col = 0 ; col < w ; col ++ ) { int x , y ; float val ; x = row - cx ; y = col - cy ; val = ( 1. / ( 2. * PI * sigma * sigma ) ) * exp ( - ( x * x + y * y ) / ( 2. * sigma * sigma ) ) ; set_val ( out , row , col , ch , val ) ; sums [ ch ] += get_val ( out , row , col , ch ) ; } } } for ( ch = 0 ; ch < k ; ch ++ ) { for ( row = 0 ; row < h ; row ++ ) { for ( col = 0 ; col < w ; col ++ ) { float new_val ; new_val = get_val ( out , row , col , ch ) / sums [ ch ] ; set_val ( out , row , col , ch , new_val ) ; } } } free ( sums ) ; return out ; } else { printf ( " \n " ) ; exit ( 1 ) ; } }
Crea un filtro gaussiano per la rimozione del rumore
Crea un filtro gaussiano per la rimozione del rumore
/*il valore da inserire , calcolato secondo la formula data*/ /*inserisco il valore nella sua posizione*/ /* normalizzazione */