#include<stdio.h> #include<math.h> #define ROW 512 #define COL 512 typedef unsigned char BYTE; double cal_psnr(BYTE origin[ROW][COL],BYTE output[ROW][COL]) { int i, j; double PSNR = 0, MSE = 0, MAXI = 255; for (i = 0; i < COL; i++) { for (j = 0; j < ROW; j++) { MSE += (origin[i][j] - output[i][j]) * (origin[i][j] - output[i][j]); } } MSE = MSE/(ROW*COL); printf("MSE: %f ", MSE); PSNR = 20*log10(MAXI) - 10*log10(MSE); return PSNR; } int main() { FILE *f = NULL; BYTE output[ROW][COL]; BYTE origin[ROW][COL]; f = fopen("D:lena512.raw","rb"); fread(origin,sizeof(BYTE),ROW*COL,f); printf("has already read "); for(int i =0;i<ROW;i++) for(int j=0;j<COL;j++) { if(origin[i][j]>=0&&origin[i][j]<=64) output[i][j] = 32; else if (origin[i][j]>64&&origin[i][j]<=128) output[i][j] = 96; else if (origin[i][j]>128&&origin[i][j]<=192) output[i][j] = 160; else output[i][j] = 224; } printf("PSNR: %lf",cal_psnr(origin,output)); fclose(f); return 0; }
lena512.raw 下载地址:https://files.cnblogs.com/files/jzcbest1016/lena512_20171219131444306.rar
.raw文件可以用photoshop打开