-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage-upscale.cpp
174 lines (138 loc) · 4.38 KB
/
image-upscale.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// image-upscale.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <lbfgs.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <exception>
enum { SCALE = 2 };
static int progress(
void *instance,
const lbfgsfloatval_t *x,
const lbfgsfloatval_t *g,
const lbfgsfloatval_t fx,
const lbfgsfloatval_t xnorm,
const lbfgsfloatval_t gnorm,
const lbfgsfloatval_t step,
int n,
int k,
int ls
)
{
return 0;
}
static lbfgsfloatval_t evaluate(
void *instance,
const lbfgsfloatval_t *x,
lbfgsfloatval_t *g,
const int n,
const lbfgsfloatval_t step
)
{
auto src = static_cast<cv::Mat*>(instance);
const cv::Mat x2(src->rows * SCALE,
src->cols * SCALE,
CV_64FC1,
const_cast<void*>(static_cast<const void*>(x)));
cv::Mat Ax2;
cv::idct(x2, Ax2);
double fx = 0;
for (int y = 0; y < src->rows; ++y)
for (int x = 0; x < src->cols; ++x)
{
double v = 0;
for (int yy = 0; yy < SCALE; ++yy)
for (int xx = 0; xx < SCALE; ++xx)
v += Ax2.at<double>(y * SCALE + yy, x * SCALE + xx);
v /= SCALE * SCALE;
auto Ax = v - src->at<float>(y, x);
if (std::abs(Ax) < 0.5)
Ax = 0;
for (int yy = 0; yy < SCALE; ++yy)
for (int xx = 0; xx < SCALE; ++xx)
Ax2.at<double>(y * SCALE + yy, x * SCALE + xx) = Ax;
fx += Ax * Ax * SCALE * SCALE;
}
cv::Mat AtAxb2(src->rows * SCALE,
src->cols * SCALE,
CV_64FC1,
g);
cv::dct(Ax2, AtAxb2);
AtAxb2 *= 2;
return fx;
};
int main(int argc, char** argv)
{
try {
cv::String filename;
if (argc >= 2)
filename = argv[1];
else {
try {
filename = cv::samples::findFile("lena.jpg");
}
catch (const std::exception& ex) {
std::string s(ex.what());
s = s.substr(s.find(") ") + 2);
s = s.substr(0, s.find("modules"));
filename = s + "samples/data/lena.jpg";
}
}
cv::Mat img = cv::imread(filename);
imshow("Original", img);
std::vector<cv::Mat> bgr;
split(img, bgr);
for (auto& src : bgr)
{
src.convertTo(src, CV_32F);
const double param_c = 5;
const int numImgPixels = src.rows * src.cols * SCALE * SCALE;
const auto mean = cv::mean(src);
src -= mean;
// Initialize solution vector
lbfgsfloatval_t fx;
lbfgsfloatval_t *x = lbfgs_malloc(numImgPixels);
if (x == nullptr) {
return EXIT_FAILURE;
}
for (int i = 0; i < numImgPixels; i++) {
x[i] = 1;
}
// Initialize the parameters for the optimization.
lbfgs_parameter_t param;
lbfgs_parameter_init(¶m);
param.orthantwise_c = param_c; // this tells lbfgs to do OWL-QN
param.linesearch = LBFGS_LINESEARCH_BACKTRACKING;
int lbfgs_ret = lbfgs(numImgPixels, x, &fx, evaluate, progress, &src, ¶m);
cv::Mat Xat2(src.rows * SCALE, src.cols * SCALE, CV_64FC1, x);
cv::Mat Xa;
idct(Xat2, Xa);
lbfgs_free(x);
Xa += mean;
const cv::Mat sharpening_kernel = (cv::Mat_<double>(3, 3)
<< 0, -1, 0,
-1, 4, -1,
0, -1, 0);
cv::Mat sharpened;
filter2D(Xa, sharpened, -1, sharpening_kernel);
cv::Mat contrastMask = abs(sharpened);
contrastMask.convertTo(contrastMask, CV_8U);
cv::threshold(contrastMask, contrastMask, 0, 255, cv::THRESH_BINARY | cv::THRESH_TRIANGLE);
sharpened += Xa;
sharpened.copyTo(Xa, contrastMask);
Xa.convertTo(src, CV_8U);
}
cv::Mat dst;
merge(bgr, dst);
imshow("Result", dst);
cv::waitKey();
if (argc >= 3)
cv::imwrite(argv[2], dst);
}
catch (const std::exception& ex) {
std::cerr << typeid(ex).name() << ": " << ex.what() << '\n';
}
}