The super-resolution module contains sample codes for benchmarking, in order to compare different models and algorithms. Here is presented a sample code for performing benchmarking, and then a few benchmarking results are collected. It was performed on an Intel i7-9700K CPU on an Ubuntu 18.04.02 OS.
1
2
3
4
5#include <iostream>
6#include <opencv2/opencv_modules.hpp>
7
8#ifdef HAVE_OPENCV_QUALITY
13
17
18static void showBenchmark(vector<Mat> images,
string title,
Size imageSize,
19 const vector<String> imageTitles,
20 const vector<double> psnrValues,
21 const vector<double> ssimValues)
22{
24 int fontScale = 1;
26
27 int len = static_cast<int>(images.size());
28
29 int cols = 2, rows = 2;
30
31 Mat fullImage =
Mat::zeros(
Size((cols * 10) + imageSize.width * cols, (rows * 10) + imageSize.height * rows),
32 images[0].type());
33
34 stringstream ss;
35 int h_ = -1;
36 for (int i = 0; i < len; i++) {
37
38 int fontStart = 15;
39 int w_ = i % cols;
40 if (i % cols == 0)
41 h_++;
42
43 Rect ROI((w_ * (10 + imageSize.width)), (h_ * (10 + imageSize.height)), imageSize.width, imageSize.height);
45 resize(images[i], tmp,
Size(ROI.width, ROI.height));
46
47 ss << imageTitles[i];
49 ss.str(),
51 fontFace,
52 fontScale,
53 fontColor,
54 1,
55 16);
56
57 ss.str("");
58 fontStart += 20;
59
60 ss << "PSNR: " << psnrValues[i];
62 ss.str(),
64 fontFace,
65 fontScale,
66 fontColor,
67 1,
68 16);
69
70 ss.str("");
71 fontStart += 20;
72
73 ss << "SSIM: " << ssimValues[i];
75 ss.str(),
77 fontFace,
78 fontScale,
79 fontColor,
80 1,
81 16);
82
83 ss.str("");
84 fontStart += 20;
85
86 tmp.
copyTo(fullImage(ROI));
87 }
88
92}
93
94static Vec2d getQualityValues(
Mat orig,
Mat upsampled)
95{
96 double psnr =
PSNR(upsampled, orig);
98 double ssim =
mean(
Vec3d((q[0]), q[1], q[2]))[0];
99 return Vec2d(psnr, ssim);
100}
101
102int main(
int argc,
char *argv[])
103{
104
105
106 if (argc < 4) {
107 cout << "usage: Arg 1: image path | Path to image" << endl;
108 cout << "\t Arg 2: algorithm | edsr, espcn, fsrcnn or lapsrn" << endl;
109 cout << "\t Arg 3: path to model file 2 \n";
110 cout << "\t Arg 4: scale | 2, 3, 4 or 8 \n";
111 return -1;
112 }
113
114 string path = string(argv[1]);
115 string algorithm = string(argv[2]);
116 string model = string(argv[3]);
117 int scale = atoi(argv[4]);
118
121 cerr << "Couldn't load image: " << img << "\n";
122 return -2;
123 }
124
125
128 Mat cropped = img(
Rect(0, 0, width, height));
129
130
132 resize(cropped, img_downscaled,
Size(), 1.0 / scale, 1.0 / scale);
133
134
135 DnnSuperResImpl sr;
136
137 vector <Mat> allImages;
139
140
141 sr.readModel(model);
142 sr.setModel(algorithm, scale);
143 sr.upsample(img_downscaled, img_new);
144
145 vector<double> psnrValues = vector<double>();
146 vector<double> ssimValues = vector<double>();
147
148
150
151 psnrValues.push_back(
quality[0]);
152 ssimValues.push_back(
quality[1]);
153
154 cout << sr.getAlgorithm() << ":" << endl;
155 cout <<
"PSNR: " <<
quality[0] <<
" SSIM: " <<
quality[1] << endl;
156 cout << "----------------------" << endl;
157
158
161 quality = getQualityValues(cropped, bicubic);
162
163 psnrValues.push_back(
quality[0]);
164 ssimValues.push_back(
quality[1]);
165
166 cout << "Bicubic " << endl;
167 cout <<
"PSNR: " <<
quality[0] <<
" SSIM: " <<
quality[1] << endl;
168 cout << "----------------------" << endl;
169
170
173 quality = getQualityValues(cropped, nearest);
174
175 psnrValues.push_back(
quality[0]);
176 ssimValues.push_back(
quality[1]);
177
178 cout << "Nearest neighbor" << endl;
179 cout <<
"PSNR: " <<
quality[0] <<
" SSIM: " <<
quality[1] << endl;
180 cout << "----------------------" << endl;
181
182
185 quality = getQualityValues(cropped, lanczos);
186
187 psnrValues.push_back(
quality[0]);
188 ssimValues.push_back(
quality[1]);
189
190 cout << "Lanczos" << endl;
191 cout <<
"PSNR: " <<
quality[0] <<
" SSIM: " <<
quality[1] << endl;
192 cout << "-----------------------------------------------" << endl;
193
194 vector <Mat> imgs{img_new, bicubic, nearest, lanczos};
195 vector <String> titles{sr.getAlgorithm(), "Bicubic", "Nearest neighbor", "Lanczos"};
196 showBenchmark(imgs,
"Quality benchmark",
Size(bicubic.
cols, bicubic.
rows), titles, psnrValues, ssimValues);
197
199
200 return 0;
201}
202#else
204{
205 std::cout << "This sample requires the OpenCV Quality module." << std::endl;
206 return 0;
207}
208#endif
n-dimensional dense array class
Definition mat.hpp:812
void copyTo(OutputArray m) const
Copies the matrix to another one.
static CV_NODISCARD_STD MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
int cols
Definition mat.hpp:2138
bool empty() const
Returns true if the array has no elements.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2138
cv::Scalar compute(InputArray cmp) CV_OVERRIDE
Computes SSIM.
Scalar mean(InputArray src, InputArray mask=noArray())
Calculates an average (mean) of array elements.
double PSNR(InputArray src1, InputArray src2, double R=255.)
Computes the Peak Signal-to-Noise Ratio (PSNR) image quality metric.
Rect2i Rect
Definition types.hpp:489
Point2i Point
Definition types.hpp:209
Size2i Size
Definition types.hpp:370
Vec< double, 3 > Vec3d
Definition matx.hpp:464
Vec< float, 2 > Vec2f
Definition matx.hpp:458
Scalar_< double > Scalar
Definition types.hpp:702
InputOutputArray noArray()
Vec< double, 2 > Vec2d
Definition matx.hpp:463
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
Draws a text string.
@ FONT_HERSHEY_COMPLEX_SMALL
smaller version of FONT_HERSHEY_COMPLEX
Definition imgproc.hpp:906
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
Definition dnn_superres.hpp:26
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition quality_utils.hpp:90
Definition quality_utils.hpp:13
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102