超分辨率模块包含用于基准测试的示例代码,目的是比较不同的模型和算法。这里提供了一个执行基准测试的示例代码,然后收集一些基准测试的结果。该测试在 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;
25 标量 字体颜色 =
标量(255, 255, 255);
27 int 长度 =
static_cast<int>(图片.大小());
31 矩阵 整幅图像 = 矩阵::零(
尺寸((列数 * 10) + 图片尺寸.
宽度 * 列数, (行数 * 10) + 图片尺寸.
高度 * 行数),
36 for (
int i = 0; i < 长度; i++) {
43 矩形 ROI((w_ * (10 + 图片尺寸.
宽度)), (h_ * (10 + 图片尺寸.
高度)), 图片尺寸.
宽度, 图片尺寸.
高度);
45 改变尺寸(图片[i], tmp,
尺寸(ROI.宽度, ROI.高度));
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 <<
"用法:Arg 1:图像路径 | 图像路径" << endl;
108 cout <<
"\t Arg 2:算法 | edsr、espcn、fsrcnn 或 lapsrn" << endl;
109 cout <<
"\t Arg 3:模型文件 2 的路径 \n";
110 cout <<
"\t Arg 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 <<
"Nearest neighbor" << 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 质量模块。" << std::endl;
void copyTo(OutputArray m) const
将矩阵复制到另一个矩阵。
bool empty() const
如果阵列没有元素则返回 true。
int rows
行数和列数,或 (-1, -1),当矩阵具有 2 个以上的维度时
定义 mat.hpp:2138
用于指定 2D 矩形的模板类。
定义 types.hpp:444
用于指定图像或矩形大小的模板类。
定义 types.hpp:335
_Tp height
高度
定义 types.hpp:363
_Tp width
宽度
定义 types.hpp:362
int waitKey(int delay=0)
等待按下某个键。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
从文件中加载图像。
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)
定义 quality_utils.hpp:90
与磁盘上某个文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102