double mat_distance_norm (mat m1, mat m2, double norm) { double s = 0; idx_t i, j; assert (mat_width (m1) == mat_width (m2)); assert (mat_height (m1) == mat_height (m2)); for (i = 0; i < mat_height (m1); i++) for (j = 0; j < mat_width (m1); j++) s += pow (fabs (m1[i][j] - m2[i][j]), norm); return pow (s, 1. / (double) norm); }
void print_dist(mat Y, mat Y_attack, double wcr) { uint i; for (i=0; i < mat_width(Y); i++) { vec y = mat_get_col(Y,i), ya = mat_get_col(Y_attack,i); printf("%f, %f;\n", wcr,vec_norm(ya,1.0)/vec_norm(y,1.0)); vec_delete( y ); vec_delete( ya ); } }
int main() { int i, j; int w, h; int levels, ct_levels, wt_levels,level_init; double rate,rate_init; ivec dfb_levels; mat source, dest; contourlet_t *contourlet; mat wavelet; int length; unsigned char *buffer; //³õʼ»¯²ÎÊý int argc=6; rate_init=2; level_init=5; #define LEVELS 5 #define IMPULSE 100. source = mat_pgm_read("1.pgm"); h = mat_height(source); w = mat_width(source); dest = mat_new(w, h); rate = rate_init * w * h; levels = level_init; ct_levels = argc - 4; /* contourlet levels */ wt_levels = levels - ct_levels; /* wavelet levels */ dfb_levels = ivec_new(ct_levels); for(i = 0; i < ct_levels; i++) dfb_levels[i] = 4+i; buffer = bvec_new_zeros(BUFFER_SIZE); contourlet = contourlet_new(ct_levels, dfb_levels); contourlet->wt_levels = wt_levels; contourlet_transform(contourlet, source); wavelet = it_dwt2D(contourlet->low, it_wavelet_lifting_97, wt_levels); contourlet->dwt = it_wavelet2D_split(wavelet, wt_levels); /* normalize the subbands */ for(i = 0; i < ct_levels; i++) for(j = 0; j < (1 << dfb_levels[i]); j++) mat_mul_by(contourlet->high[i][j], norm_high[1+i][dfb_levels[i]][j]); mat_mul_by(contourlet->low, norm_low[ct_levels]); /* make flat images */ mat_pgm_write("dwt.pgm", wavelet); for(i = 0; i < ct_levels; i++) { char filename[256]; mat dfb_rec = mat_new((h >> i) + 1, (w >> i) + 1); if(dfb_levels[i]) dfb_flatten(contourlet->high[i], dfb_rec, dfb_levels[i]); else mat_set_submatrix(dfb_rec, contourlet->high[i][0], 0, 0); mat_incr(dfb_rec, 128); sprintf(filename, "dfb%d.pgm", i); mat_pgm_write(filename, dfb_rec); mat_decr(dfb_rec, 128); mat_delete(dfb_rec); } /* EZBC encoding */ length = ezbc_encode(contourlet, buffer, BUFFER_SIZE, rate); /* EZBC decoding */ ezbc_decode(contourlet, buffer, BUFFER_SIZE, rate); mat_pgm_write("rec_low.pgm", contourlet->dwt[0]); /* make flat images */ for(i = 0; i < ct_levels; i++) { char filename[256]; mat dfb_rec = mat_new((h >> i) + 1, (w >> i) + 1); if(dfb_levels[i]) dfb_flatten(contourlet->high[i], dfb_rec, dfb_levels[i]); else mat_set_submatrix(dfb_rec, contourlet->high[i][0], 0, 0); mat_incr(dfb_rec, 128); sprintf(filename, "rec_dfb%d.pgm", i); mat_pgm_write(filename, dfb_rec); mat_decr(dfb_rec, 128); mat_delete(dfb_rec); } /* normalize the subbands */ for(i = 0; i < ct_levels; i++) for(j = 0; j < (1 << dfb_levels[i]); j++) mat_div_by(contourlet->high[i][j], norm_high[1+i][dfb_levels[i]][j]); mat_div_by(contourlet->low, norm_low[ct_levels]); // mat_pgm_write("rec_low.pgm", contourlet->dwt[0]); /* TODO: fix this in libit */ if(wt_levels) wavelet = it_wavelet2D_merge(contourlet->dwt, wt_levels); else mat_copy(wavelet, contourlet->dwt[0]); mat_pgm_write("rec_dwt.pgm", wavelet); contourlet->low = it_idwt2D(wavelet, it_wavelet_lifting_97, wt_levels); contourlet_itransform(contourlet, dest); contourlet_delete(contourlet); mat_pgm_write("rec.pgm", dest); printf("rate = %f PSNR = %f\n", length * 8. / (w*h), 10*log10(255*255/mat_distance_mse(source, dest, 0))); ivec_delete(dfb_levels); mat_delete(dest); mat_delete(source); bvec_delete(buffer); return(0); }
int main () { it_wavelet_t *wavelet; it_wavelet2D_t *wavelet2D; it_separable2D_t *separable; const char *image_in = "../data/test.pgm"; const char *image_out = "out.pgm"; const char *sound_in = "../data/test.wav"; const char *sound_out = "out.wav"; char pnm_type, comments[1000]; int width, height, maxval; int levels; int length, channels, srate, depth; mat m, mt; vec v, vt; levels = 4; /* Read the sound */ if (!wav_info (sound_in, &channels, &srate, &depth, &length)) { fprintf (stderr, "unable to open file %s\n", sound_in); return (1); } printf ("file name = %s\nchannels = %d\nsampling rate = %d\ndepth = %d\nlength = %d samples/channel\n", sound_in, channels, srate, depth, length); m = mat_wav_read (sound_in); v = m[0]; /* consider only the first channel */ /* Transform the sound */ vt = it_dwt (v, it_wavelet_lifting_53, levels); vec_delete (v); /* Write down the coefficients */ v = vec_new_eval (vt, rescale_sound, NULL); m[0] = v; mat_wav_write ("wavelet.wav", m, srate, depth); vec_delete (v); /* Reconstruct the sound */ v = it_idwt (vt, it_wavelet_lifting_53, levels); vec_delete (vt); m[0] = v; mat_wav_write (sound_out, m, srate, depth); mat_delete (m); /* Test the separable transform */ /* Warning: note that for the wavelet with more than 1 level of */ /* decomposition, the transform obtained by applying the wavelet */ /* on the rows, then on the columns of the image is *not* what is */ /* usually called the separable wavelet. See this example: */ /* separable(wavelet1D) wavelet2D */ /* */ /* +------+------+------+ +------+------+------+ */ /* | L1L1 | L1H1 | L1H0 | | L1L1 | LH1 | | */ /* +------+------+------+ +------+------+ LH0 | */ /* | H1L1 | H1H1 | H1H0 | | HL1 | HH1 | | */ /* +------+------+------+ +------+------+------+ */ /* | | | | | | | */ /* | H0L1 | H0H1 | H0H0 | | HL0 | HH0 | */ /* +------+------+------+ +-------------+------+ */ m = mat_pgm_read (image_in); printf ("PGM %dx%d\n", mat_height (m), mat_width (m)); levels = 1; /* create a 1D wavelet object */ wavelet = it_wavelet_new (it_wavelet_lifting_97, levels); /* create a separable transform out of the wavelet */ separable = it_separable2D_new (wavelet); /* Transform the image */ mt = (mat) it_transform2D (separable, m); mat_delete (m); /* Write down the coefficients (shifted & saturated) */ m = mat_new_eval (mt, rescale_image, NULL); mat_pgm_write ("wavelet_separable.pgm", m); mat_delete (m); /* Reconstruct the image */ m = (mat) it_itransform2D (separable, mt); mat_delete (mt); it_delete (separable); it_delete (wavelet); mat_pgm_write ("separable.pgm", m); mat_delete (m); /* Read the image */ pnm_info (image_in, &pnm_type, &width, &height, &maxval, comments, 1000); printf ("file name = %s\npnm type = %c\n%dx%d -> maxval=%d\ncomments=%s\n", image_in, pnm_type, width, height, maxval, ""); m = mat_pgm_read (image_in); printf ("height(m) = %d\tmaxheight(m) = %d\nwidth(m) = %d\tmaxwidth(m) = %d\n", mat_height (m), mat_height_max (m), mat_width (m), mat_width_max (m)); levels = 5; /* create a 2D wavelet object */ wavelet2D = it_wavelet2D_new (it_wavelet_lifting_97, levels); /* Transform the image */ mt = it_wavelet2D_transform (wavelet2D, m); mat_delete (m); /* Write down the coefficients (shifted & saturated) */ m = mat_new_eval (mt, rescale_image, NULL); mat_pgm_write ("wavelet.pgm", m); mat_delete (m); /* Reconstruct the image */ m = it_wavelet2D_itransform (wavelet2D, mt); mat_delete (mt); it_delete (wavelet2D); mat_pgm_write (image_out, m); mat_delete (m); return 0; }