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.
6 #include <opencv2/opencv_modules.hpp> 8 #ifdef HAVE_OPENCV_QUALITY 16 using namespace dnn_superres;
18 static void showBenchmark(vector<Mat> images,
string title,
Size imageSize,
19 const vector<String> imageTitles,
20 const vector<double> psnrValues,
21 const vector<double> ssimValues)
27 int len =
static_cast<int>(images.size());
29 int cols = 2, rows = 2;
31 Mat fullImage = Mat::zeros(
Size((cols * 10) + imageSize.
width * cols, (rows * 10) + imageSize.
height * rows),
36 for (
int i = 0; i < len; i++) {
45 resize(images[i], tmp,
Size(ROI.width, ROI.height));
60 ss <<
"PSNR: " << psnrValues[i];
73 ss <<
"SSIM: " << ssimValues[i];
86 tmp.
copyTo(fullImage(ROI));
94 static Vec2d getQualityValues(
Mat orig,
Mat upsampled)
96 double psnr =
PSNR(upsampled, orig);
97 Scalar q = quality::QualitySSIM::compute(upsampled, orig,
noArray());
98 double ssim =
mean(
Vec3d((q[0]), q[1], q[2]))[0];
99 return Vec2d(psnr, ssim);
102 int main(
int argc,
char *argv[])
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";
114 string path = string(argv[1]);
115 string algorithm = string(argv[2]);
116 string model = string(argv[3]);
117 int scale = atoi(argv[4]);
121 cerr <<
"Couldn't load image: " << img <<
"\n";
128 Mat cropped = img(
Rect(0, 0, width, height));
132 resize(cropped, img_downscaled,
Size(), 1.0 / scale, 1.0 / scale);
137 vector <Mat> allImages;
142 sr.setModel(algorithm, scale);
143 sr.upsample(img_downscaled, img_new);
145 vector<double> psnrValues = vector<double>();
146 vector<double> ssimValues = vector<double>();
149 Vec2f quality = getQualityValues(cropped, img_new);
151 psnrValues.push_back(quality[0]);
152 ssimValues.push_back(quality[1]);
154 cout << sr.getAlgorithm() <<
":" << endl;
155 cout <<
"PSNR: " << quality[0] <<
" SSIM: " << quality[1] << endl;
156 cout <<
"----------------------" << endl;
161 quality = getQualityValues(cropped, bicubic);
163 psnrValues.push_back(quality[0]);
164 ssimValues.push_back(quality[1]);
166 cout <<
"Bicubic " << endl;
167 cout <<
"PSNR: " << quality[0] <<
" SSIM: " << quality[1] << endl;
168 cout <<
"----------------------" << endl;
173 quality = getQualityValues(cropped, nearest);
175 psnrValues.push_back(quality[0]);
176 ssimValues.push_back(quality[1]);
178 cout <<
"Nearest neighbor" << endl;
179 cout <<
"PSNR: " << quality[0] <<
" SSIM: " << quality[1] << endl;
180 cout <<
"----------------------" << endl;
185 quality = getQualityValues(cropped, lanczos);
187 psnrValues.push_back(quality[0]);
188 ssimValues.push_back(quality[1]);
190 cout <<
"Lanczos" << endl;
191 cout <<
"PSNR: " << quality[0] <<
" SSIM: " << quality[1] << endl;
192 cout <<
"-----------------------------------------------" << endl;
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);
205 std::cout <<
"This sample requires the OpenCV Quality module." << std::endl;
Scalar_< double > Scalar
Definition: types.hpp:669
Scalar mean(InputArray src, InputArray mask=noArray())
Calculates an average (mean) of array elements.
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: mat.hpp:2092
void copyTo(OutputArray m) const
Copies the matrix to another one.
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
_Tp width
the width
Definition: types.hpp:339
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
smaller version of FONT_HERSHEY_COMPLEX
Definition: imgproc.hpp:823
double PSNR(InputArray src1, InputArray src2, double R=255.)
Computes the Peak Signal-to-Noise Ratio (PSNR) image quality metric.
Template class for specifying the size of an image or rectangle.
Definition: types.hpp:315
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition: quality_utils.hpp:90
InputOutputArray noArray()
int cols
Definition: mat.hpp:2092
Rect2i Rect
Definition: types.hpp:462
Template class for 2D rectangles.
Definition: types.hpp:420
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
_Tp height
the height
Definition: types.hpp:340
Size2i Size
Definition: types.hpp:347
Vec< double, 2 > Vec2d
Definition: matx.hpp:432
void resize(int src_type, const uchar *src_data, size_t src_step, int src_width, int src_height, uchar *dst_data, size_t dst_step, int dst_width, int dst_height, double inv_scale_x, double inv_scale_y, int interpolation)
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.
Vec< double, 3 > Vec3d
Definition: matx.hpp:433
n-dimensional dense array class
Definition: mat.hpp:797
bool empty() const
Returns true if the array has no elements.
Point2i Point
Definition: types.hpp:194
int waitKey(int delay=0)
Waits for a pressed key.