-
Notifications
You must be signed in to change notification settings - Fork 1
/
PSNR.cpp
71 lines (46 loc) · 1.2 KB
/
PSNR.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
#define Size 256
int main(int argc, char *argv[])
{
// file pointer
FILE *file;
int BytesPerPixel = atoi(argv[3]);
// image data array
unsigned char Imagedata_quantised[Size][Size][BytesPerPixel];
unsigned char Imagedata[Size][Size][BytesPerPixel];
// read image "ride.raw" into image data matrix
if (!(file=fopen(argv[1],"rb")))
{
cout << "Cannot open file: " << argv[1] <<endl;
exit(1);
}
fread(Imagedata, sizeof(unsigned char), Size*Size*BytesPerPixel, file);
fclose(file);
if (!(file=fopen(argv[2],"rb")))
{
cout << "Cannot open file: " << argv[2] <<endl;
exit(1);
}
fread(Imagedata_quantised, sizeof(unsigned char), Size*Size*BytesPerPixel, file);
fclose(file);
double MSE = 0;
for(int k = 0; k < BytesPerPixel; k++)
{
for(int i = 0; i < Size; i++)
{
for (int j = 0; j < Size; j++)
{
MSE = MSE + (Imagedata[i][j][k] - Imagedata_quantised[i][j][k]) * (Imagedata[i][j][k] - Imagedata_quantised[i][j][k]);
}
}
}
// cout << MSE;
MSE = (1.0 * MSE)/(Size*Size*BytesPerPixel);
double PSNR = 10*log((255.0*255.0)/MSE);
cout << "PSNR: " << PSNR;
return 0;
}