超分辨率模块包含用于基准测试的示例代码,以比较不同的模型和算法。此处提供了一个执行基准测试的示例代码,并收集了一些基准测试结果。测试是在运行 Ubuntu 18.04.02 操作系统的 Intel i7-9700K CPU 上进行的。
6#include <opencv2/opencv_modules.hpp>
8#ifdef HAVE_OPENCV_QUALITY
16using namespace dnn_superres;
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)
23 int fontFace = FONT_HERSHEY_COMPLEX_SMALL;
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));
89 namedWindow(title, 1);
90 imshow(title, fullImage);
94static 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);
102int main(
int argc,
char *argv[])
107 cout <<
"用法:参数 1:图像路径 | 图像路径" << endl;
108 cout <<
"\t 参数 2:算法 | edsr, espcn, fsrcnn 或 lapsrn" << endl;
109 cout <<
"\t 参数 3:模型文件路径 2 \n";
110 cout <<
"\t 参数 4:缩放比例 | 2, 3, 4 或 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 <<
"无法加载图像:" << 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;
160 resize(img_downscaled, bicubic,
Size(), scale, scale, INTER_CUBIC);
161 quality = getQualityValues(cropped, bicubic);
163 psnrValues.push_back(quality[0]);
164 ssimValues.push_back(quality[1]);
166 cout <<
"双三次插值" << endl;
167 cout <<
"PSNR:" << quality[0] <<
" SSIM:" << quality[1] << endl;
168 cout <<
"----------------------" << endl;
172 resize(img_downscaled, nearest,
Size(), scale, scale, INTER_NEAREST);
173 quality = getQualityValues(cropped, nearest);
175 psnrValues.push_back(quality[0]);
176 ssimValues.push_back(quality[1]);
178 cout <<
"最近邻插值" << endl;
179 cout <<
"PSNR:" << quality[0] <<
" SSIM:" << quality[1] << endl;
180 cout <<
"----------------------" << endl;
184 resize(img_downscaled, lanczos,
Size(), scale, scale, INTER_LANCZOS4);
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,
"质量基准测试",
Size(bicubic.
cols, bicubic.
rows), titles, psnrValues, ssimValues);
205 std::cout <<
"此示例需要 OpenCV Quality 模块。" << std::endl;
void copyTo(OutputArray m) const
将矩阵复制到另一个矩阵。
cv::getTickFrequency
double getTickFrequency()
int rows
行数和列数,或者当矩阵维度超过 2 时为 (-1, -1)
定义 mat.hpp:2165
2D 矩形的模板类。
定义 types.hpp:444
用于指定图像或矩形大小的模板类。
Definition types.hpp:335
_Tp height
高度
Definition types.hpp:363
_Tp width
宽度
Definition types.hpp:362
int waitKey(int delay=0)
等待按键按下。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件加载图像。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)
Definition quality_utils.hpp:90