-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpsnr.cpp
More file actions
58 lines (48 loc) · 1.42 KB
/
Copy pathpsnr.cpp
File metadata and controls
58 lines (48 loc) · 1.42 KB
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
//
// Created by Nicola Pierazzo on 27/04/16.
//
#include <utility>
#include <iostream>
#include <cmath>
#include "Image.hpp"
#include "utils.hpp"
using namespace imgutils;
using std::cerr;
using std::endl;
using std::cout;
using std::sqrt;
using std::log10;
using std::min;
using std::max;
double mse(const Image &im1, const Image &im2) {
double ans = 0.;
for (auto it1 = im1.begin(), it2 = im2.begin(); it1 != im1.end(); ++it1, ++it2) {
float v1 = min(max(*it1, 0.f), 255.f);
float v2 = min(max(*it2, 0.f), 255.f);
ans += (v1 - v2) * (v1 - v2);
}
ans /= im1.samples();
return ans;
}
double rmse(const Image &im1, const Image &im2) {
return sqrt(mse(im1, im2));
}
double psnr(const Image &im1, const Image &im2) {
return -20. * log10(rmse(im1, im2) / 255.);
}
int main(int argc, char** argv) {
bool usage = static_cast<bool>(pick_option(&argc, argv, "h", nullptr));
bool show_rmse = static_cast<bool>(pick_option(&argc, argv, "m", nullptr));
if (usage || argc < 2) {
cerr << "usage: " << argv[0] << " image1 [image2] [-m]" << endl;
return EXIT_SUCCESS;
}
Image in1 = read_image(argv[1]);
Image in2 = read_image(argc > 2 ? argv[2] : "-");
if (in1.shape() != in2.shape() || in1.channels() != in2.channels()) {
cerr << "The two images should have the same dimension." << endl;
return EXIT_FAILURE;
}
cout << (show_rmse ? rmse(in1, in2) : psnr(in1, in2)) << endl;
return EXIT_SUCCESS;
}