void TencStats::calcPSNR(double *psnrY,double *psnrU,double *psnrV) const { double f=dx*dy*255.0*255.0*count; if (f!=0) { *psnrY=psnr(sumPsnrY*mult[0]/f); *psnrU=psnr(sumPsnrU*mult[1]/f); *psnrV=psnr(sumPsnrV*mult[2]/f); } else { *psnrY=*psnrU=*psnrV=0; } }
/* ----------------------------------------------------------------------------------------------------------- */ void routine_median_filter(char *src_path, char *noise_path, char *filename0, char *filename, char *denoise_path) /* ----------------------------------------------------------------------------------------------------------- */ { char complete_filename[1024]; char *sep; int radius; int k; int ndigit = 1; long i0, i1, j0, j1; uint8 **X0, **X, **Y; float32 p; // chargement de l'image sans bruit dans X0 et de l'image bruitee dans X // Y doit contenir l'image traitee generate_path_filename_extension(src_path, filename0, "pgm", complete_filename); X0 = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); generate_path_filename_extension(noise_path, filename, "pgm", complete_filename); X = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); Y = ui8matrix(i0, i1, j0, j1); // ------------------- // -- median kernel -- // ------------------- sep = "_M_"; radius = 1; // parametre du bloc de code k = radius; median_ui8matrix(X, i0, i1, j0, j1, radius, Y); p = psnr(X0, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(denoise_path, filename, sep, k, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); // ajouter ici d'autre instances du bloc de code avec d'autres valeurs de radius }
double getdistortion(jas_matrix_t *orig, jas_matrix_t *recon, int depth, int metric) { double d; switch (metric) { case metricid_psnr: default: d = psnr(orig, recon, depth); break; case metricid_mae: d = msen(orig, recon, 1); break; case metricid_mse: d = msen(orig, recon, 2); break; case metricid_rmse: d = sqrt(msen(orig, recon, 2)); break; case metricid_pae: d = pae(orig, recon); break; case metricid_equal: d = (pae(orig, recon) == 0) ? 0 : 1; break; } return d; }
/* ---------------------------------------------------------------------- */ void routine_impulse_noise(char *src_path, char *filename, char *noise_path) /* ---------------------------------------------------------------------- */ { char complete_filename[1024]; char *sep = "_IN_"; // Impulse Noise float32 percent; int ndigit = 3; // codage du 3 chiffres du pourcentage de bruit impulsionnel dans le nom du fichier long i0, i1, j0, j1; uint8 **X, **Y; float32 p; // chargement de l'image et allocation d'une image avec des bords generate_path_filename_extension(src_path, filename, "pgm", complete_filename); puts(complete_filename); X = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); Y = ui8matrix(i0, i1, j0, j1); percent = 0.5f; // parametre du bloc de code //printf("%4d : ",(int)(10.0f*percent)); impulse_noise_ui8matrix(X, i0, i1, j0, j1, percent/100.0f, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(noise_path, filename, sep, (int)(10.0f*percent), ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); // ajouter ici d'autre instances du bloc de code avec d'autres valeurs de percent }
int main(int argc, const char **argv) { const char *ref_path; const char *dis_path; const char *fmt; int w; int h; int ret; if (argc < 6) { usage(); return 2; } fmt = argv[1]; ref_path = argv[2]; dis_path = argv[3]; w = atoi(argv[4]); h = atoi(argv[5]); if (w <= 0 || h <= 0) return 2; ret = psnr(ref_path, dis_path, w, h, fmt); if (ret) return ret; return 0; }
int main(int argc, char* argv[]) { //原画像と処理画像を読み込む. cv::Mat src = cv::imread(argv[1]); cv::Mat dst = cv::imread(argv[2]); std::cout << psnr(src, dst) << std::endl; return 0; }
static double imgerr(char *m, float *x, float *y, int n) { double r; if (false); else if (0 == strcmp(m, "MSE")) r = mean_square_error(x, y, n); else if (0 == strcmp(m, "RMSE")) r = root_mean_square_error(x, y, n); else if (0 == strcmp(m, "MAE")) r = mean_absolute_error(x, y, n); else if (0 == strcmp(m, "UIQI")) r = uiqi(x, y, n); else if (0 == strcmp(m, "SSIM")) r = ssim(x, y, n); else if (0 == strcmp(m, "PSNR")) r = psnr(x, y, n); else if (0 == strcmp(m, "NCC")) r = ncc(x, y, n); else if (string_is_lp(m, &r)) r = ell_pee_distance(x, y, n, r); else fail("unrecognized metric \"%s\"", m); return r; }
int main(int argc, char *argv[]) { if (argc != 3) { std::cerr << "Usage: " << argv[0] << " <image_1> <image_2>" << std::endl; return -1; } cv::Mat src_img = cv::imread(argv[1]); cv::Mat cmp_img = cv::imread(argv[2]); std::cout << "PSNR=" << psnr(src_img, cmp_img) << "[dB]" << std::endl; return 0; }
int main() { int selection=0; int conti=0; //continue? do { printf("============\n" "1) BTC\n" "2) psnr\n" "============\n"); scanf("%d",&selection); switch(selection) { case 1: set(); break; case 2: psnr(); break; default: printf("ERROR!\n"); } printf("continue:\n" "1.YES 2.NO\n"); scanf("%d",&selection); if(selection==1) conti=1; else conti=0; }while(conti==1); return 0; }
static int encode_frame(struct vf_instance_s* vf, AVFrame *pic, double pts){ const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'}; int out_size; double dts; if(pts == MP_NOPTS_VALUE) pts= lavc_venc_context->frame_number * av_q2d(lavc_venc_context->time_base); if(pic){ #if 0 pic->opaque= malloc(sizeof(pts)); memcpy(pic->opaque, &pts, sizeof(pts)); #else if(pts != MP_NOPTS_VALUE) pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5); else pic->pts= MP_NOPTS_VALUE; #endif } out_size = avcodec_encode_video(lavc_venc_context, mux_v->buffer, mux_v->buffer_size, pic); if(pts != MP_NOPTS_VALUE) dts= pts - lavc_venc_context->delay * av_q2d(lavc_venc_context->time_base); else dts= MP_NOPTS_VALUE; #if 0 pts= lavc_venc_context->coded_frame->opaque ? *(double*)lavc_venc_context->coded_frame->opaque : MP_NOPTS_VALUE; #else if(lavc_venc_context->coded_frame->pts != MP_NOPTS_VALUE) pts= lavc_venc_context->coded_frame->pts * av_q2d(lavc_venc_context->time_base); else pts= MP_NOPTS_VALUE; assert(MP_NOPTS_VALUE == AV_NOPTS_VALUE); #endif //fprintf(stderr, "ve_lavc %f/%f\n", dts, pts); if(out_size == 0 && lavc_param_skip_threshold==0 && lavc_param_skip_factor==0){ ++mux_v->encoder_delay; return 0; } muxer_write_chunk(mux_v,out_size,lavc_venc_context->coded_frame->key_frame?0x10:0, dts, pts); free(lavc_venc_context->coded_frame->opaque); lavc_venc_context->coded_frame->opaque= NULL; /* store psnr / pict size / type / qscale */ if(lavc_param_psnr){ static FILE *fvstats=NULL; char filename[20]; double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0; double quality=0.0; int8_t *q; if(!fvstats) { time_t today2; struct tm *today; today2 = time(NULL); today = localtime(&today2); sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour, today->tm_min, today->tm_sec); fvstats = fopen(filename,"w"); if(!fvstats) { perror("fopen"); lavc_param_psnr=0; // disable block mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename); return -1; /*exit(1);*/ } } // average MB quantizer q = lavc_venc_context->coded_frame->qscale_table; if(q) { int x, y; int w = (lavc_venc_context->width+15) >> 4; int h = (lavc_venc_context->height+15) >> 4; for( y = 0; y < h; y++ ) { for( x = 0; x < w; x++ ) quality += (double)*(q+x); q += lavc_venc_context->coded_frame->qstride; } quality /= w * h; } else quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA; fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n", lavc_venc_context->coded_frame->coded_picture_number, quality, out_size, psnr(lavc_venc_context->coded_frame->error[0]/f), psnr(lavc_venc_context->coded_frame->error[1]*4/f), psnr(lavc_venc_context->coded_frame->error[2]*4/f), psnr((lavc_venc_context->coded_frame->error[0]+lavc_venc_context->coded_frame->error[1]+lavc_venc_context->coded_frame->error[2])/(f*1.5)), pict_type_char[lavc_venc_context->coded_frame->pict_type] ); }
static int encode_frame(struct vf_instance *vf, AVFrame *pic, double pts){ const char pict_type_char[5]= {'?', 'I', 'P', 'B', 'S'}; double dts; AVPacket pkt; int res, got_pkt; if(pts == MP_NOPTS_VALUE) pts= lavc_venc_context->frame_number * av_q2d(lavc_venc_context->time_base); if(pic){ #if 0 pic->opaque= malloc(sizeof(pts)); memcpy(pic->opaque, &pts, sizeof(pts)); #else if(pts != MP_NOPTS_VALUE) pic->pts= floor(pts / av_q2d(lavc_venc_context->time_base) + 0.5); else pic->pts= MP_NOPTS_VALUE; #endif } av_init_packet(&pkt); pkt.data = mux_v->buffer; pkt.size = mux_v->buffer_size; res = avcodec_encode_video2(lavc_venc_context, &pkt, pic, &got_pkt); /* store stats if there are any */ if(lavc_venc_context->stats_out && stats_file) { fprintf(stats_file, "%s", lavc_venc_context->stats_out); /* make sure we can't accidentally store the same stats twice */ lavc_venc_context->stats_out[0] = 0; } if (res < 0) return 0; if(!got_pkt && lavc_param_skip_threshold==0 && lavc_param_skip_factor==0){ ++mux_v->encoder_delay; return 0; } dts = pts = MP_NOPTS_VALUE; if (pkt.pts != AV_NOPTS_VALUE) pts = pkt.pts * av_q2d(lavc_venc_context->time_base); if (pkt.dts != AV_NOPTS_VALUE) dts = pkt.dts * av_q2d(lavc_venc_context->time_base); muxer_write_chunk(mux_v,pkt.size,pkt.flags & AV_PKT_FLAG_KEY ?0x10:0, dts, pts); /* store psnr / pict size / type / qscale */ if(lavc_param_psnr){ static FILE *fvstats=NULL; char filename[20]; double f= lavc_venc_context->width*lavc_venc_context->height*255.0*255.0; double quality=0.0; int8_t *q; if(!fvstats) { time_t today2; struct tm *today; today2 = time(NULL); today = localtime(&today2); sprintf(filename, "psnr_%02d%02d%02d.log", today->tm_hour, today->tm_min, today->tm_sec); fvstats = fopen(filename,"w"); if(!fvstats) { perror("fopen"); lavc_param_psnr=0; // disable block mp_msg(MSGT_MENCODER,MSGL_ERR,"Can't open %s for writing. Check its permissions.\n",filename); return -1; /*exit(1);*/ } } // average MB quantizer q = lavc_venc_context->coded_frame->qscale_table; if(q) { int x, y; int w = (lavc_venc_context->width+15) >> 4; int h = (lavc_venc_context->height+15) >> 4; for( y = 0; y < h; y++ ) { for( x = 0; x < w; x++ ) quality += (double)*(q+x); q += lavc_venc_context->coded_frame->qstride; } quality /= w * h; } else quality = lavc_venc_context->coded_frame->quality / (float)FF_QP2LAMBDA; fprintf(fvstats, "%6d, %2.2f, %6d, %2.2f, %2.2f, %2.2f, %2.2f %c\n", lavc_venc_context->coded_frame->coded_picture_number, quality, pkt.size, psnr(lavc_venc_context->coded_frame->error[0]/f), psnr(lavc_venc_context->coded_frame->error[1]*4/f), psnr(lavc_venc_context->coded_frame->error[2]*4/f), psnr((lavc_venc_context->coded_frame->error[0]+lavc_venc_context->coded_frame->error[1]+lavc_venc_context->coded_frame->error[2])/(f*1.5)), pict_type_char[lavc_venc_context->coded_frame->pict_type] ); }
/* ----------------------------------------------------------------------------------------------------------- */ void routine_smooth_filter(char *src_path, char *noise_path, char *filename0, char *filename, char *denoise_path) /* ----------------------------------------------------------------------------------------------------------- */ { char complete_filename[1024]; char *sep; int k; float32 sigma; int radius; int ndigit; long i0, i1, j0, j1; uint8 **X0, **X, **Y; float32 **K; float32 p; // chargement de l'image bruitee dans X, et de l'image sans bruit dans X0 // Y doit contenir l'image traitee generate_path_filename_extension(src_path, filename0, "pgm", complete_filename); puts(complete_filename); X0 = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); generate_path_filename_extension(noise_path, filename, "pgm", complete_filename); puts(complete_filename); X = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); Y = ui8matrix(i0, i1, j0, j1); // --------------------- // -- gaussian kernel -- // --------------------- sep = "_G_"; ndigit = 2; // codage sur 2 chiffre de la valeur de sigma dans le nom du fichier sigma = 0.5; // parametre du bloc de code k = (int) (10*sigma); // code radius = (int) ceil(2*sigma+1); printf("sigma = %.1f -> radius = %d\n", sigma, radius); K = alloc_kernel(radius); init_gaussian_kernel(K, radius, sigma); convolve_kernel_ui8matrix(X, i0, i1, j0, j1, K, radius, Y); p = psnr(X0, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(denoise_path, filename, sep, k, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); free_kernel(K, radius); // ----------------- // -- average kernel // ----------------- sep = "_A_"; ndigit = 1; // codage sur 1 chiffre de la valeur de radius dans le nom du fichier radius = 1; // parametre du bloc de code k = radius; // code K = alloc_kernel(radius); init_average_kernel(K, radius); convolve_kernel_ui8matrix(X, i0, i1, j0, j1, K, radius, Y); p = psnr(X0, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(denoise_path, filename, sep, k, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); free_kernel(K, radius); // liberation de la memoire free_ui8matrix(X0, i0, i1, j0, j1); free_ui8matrix(X, i0, i1, j0, j1); free_ui8matrix(Y, i0, i1, j0, j1); }
/* --------------------------------------------------------------------- */ void routine_gaussian_noise(char *src_path, char *filename, char *dst_path) /* --------------------------------------------------------------------- */ { char complete_filename[1024]; char *sep = "_GN_"; // Gaussian Noise int sigma; int ndigit = 2; // codage sur 2 chiffres de la valeur de sigma dans le nom du fichier long i0, i1, j0, j1; uint8 **X, **Y; float32 p; // chargement de l'image sans bruit dans X // Y doit contenir l'image traitee generate_path_filename_extension(src_path, filename, "pgm", complete_filename); puts(complete_filename); X = LoadPGM_ui8matrix(complete_filename, &i0, &i1, &j0, &j1); Y = ui8matrix(i0, i1, j0, j1); sigma = 1; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); // ajouter ici d'autre instances du bloc de code avec d'autres valeurs de sigma sigma = 2; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 3; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 4; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 5; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 10; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 20; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); sigma = 30; // parametre du bloc de code gaussian_noise_ui8matrix(X, i0, i1, j0, j1, (float32) sigma, Y); p = psnr(X, i0, i1, j0, j1, Y); generate_path_filename_sep_k_ndigit_extension(dst_path, filename, sep, sigma, ndigit, "pgm", complete_filename); printf("%s -> PSNR = %8.2f db\n", complete_filename, p); SavePGM_ui8matrix(Y, i0, i1, j0, j1, complete_filename); // liberation de la memoire free_ui8matrix(X, i0, i1, j0, j1); free_ui8matrix(Y, i0, i1, j0, j1); }