-
Notifications
You must be signed in to change notification settings - Fork 197
/
main.cpp
195 lines (156 loc) · 6.08 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* main.cpp
* (ここにファイルの簡易説明を記入)
*
* Created on: 2015/05/24
* Author: wlamigo
*
* (ここにファイルの説明を記入)
*/
#include <opencv2/opencv.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <cmath>
#include "picojson.h"
#include "tclap/CmdLine.h"
#include "modelHandler.hpp"
#include "convertRoutine.hpp"
int main(int argc, char** argv) {
// definition of command line arguments
TCLAP::CmdLine cmd("waifu2x reimplementation using OpenCV", ' ', "1.0.0");
TCLAP::ValueArg<std::string> cmdInputFile("i", "input_file",
"path to input image file (you should input full path)", true, "",
"string", cmd);
TCLAP::ValueArg<std::string> cmdOutputFile("o", "output_file",
"path to output image file (you should input full path)", false,
"(auto)", "string", cmd);
std::vector<std::string> cmdModeConstraintV;
cmdModeConstraintV.push_back("noise");
cmdModeConstraintV.push_back("scale");
cmdModeConstraintV.push_back("noise_scale");
TCLAP::ValuesConstraint<std::string> cmdModeConstraint(cmdModeConstraintV);
TCLAP::ValueArg<std::string> cmdMode("m", "mode", "image processing mode",
false, "noise_scale", &cmdModeConstraint, cmd);
std::vector<int> cmdNRLConstraintV;
cmdNRLConstraintV.push_back(1);
cmdNRLConstraintV.push_back(2);
TCLAP::ValuesConstraint<int> cmdNRLConstraint(cmdNRLConstraintV);
TCLAP::ValueArg<int> cmdNRLevel("", "noise_level", "noise reduction level",
false, 1, &cmdNRLConstraint, cmd);
TCLAP::ValueArg<double> cmdScaleRatio("", "scale_ratio",
"custom scale ratio", false, 2.0, "double", cmd);
TCLAP::ValueArg<std::string> cmdModelPath("", "model_dir",
"path to custom model directory (don't append last / )", false,
"models", "string", cmd);
TCLAP::ValueArg<int> cmdNumberOfJobs("j", "jobs",
"number of threads launching at the same time", false, 4, "integer",
cmd);
// definition of command line argument : end
// parse command line arguments
try {
cmd.parse(argc, argv);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
std::cerr << "Error : cmd.parse() threw exception" << std::endl;
std::exit(-1);
}
// load image file
cv::Mat image = cv::imread(cmdInputFile.getValue(), cv::IMREAD_COLOR);
image.convertTo(image, CV_32F, 1.0 / 255.0);
cv::cvtColor(image, image, cv::COLOR_RGB2YUV);
// set number of jobs for processing models
w2xc::modelUtility::getInstance().setNumberOfJobs(cmdNumberOfJobs.getValue());
// ===== Noise Reduction Phase =====
if (cmdMode.getValue() == "noise" || cmdMode.getValue() == "noise_scale") {
std::string modelFileName(cmdModelPath.getValue());
modelFileName = modelFileName + "/noise"
+ std::to_string(cmdNRLevel.getValue()) + "_model.json";
std::vector<std::unique_ptr<w2xc::Model> > models;
if (!w2xc::modelUtility::generateModelFromJSON(modelFileName, models))
std::exit(-1);
std::vector<cv::Mat> imageSplit;
cv::Mat imageY;
cv::split(image, imageSplit);
imageSplit[0].copyTo(imageY);
w2xc::convertWithModels(imageY, imageSplit[0], models);
cv::merge(imageSplit, image);
} // noise reduction phase : end
// ===== scaling phase =====
if (cmdMode.getValue() == "scale" || cmdMode.getValue() == "noise_scale") {
// calculate iteration times of 2x scaling and shrink ratio which will use at last
int iterTimesTwiceScaling = static_cast<int>(std::ceil(
std::log2(cmdScaleRatio.getValue())));
double shrinkRatio = 0.0;
if (static_cast<int>(cmdScaleRatio.getValue())
!= std::pow(2, iterTimesTwiceScaling)) {
shrinkRatio = cmdScaleRatio.getValue()
/ std::pow(2.0, static_cast<double>(iterTimesTwiceScaling));
}
std::string modelFileName(cmdModelPath.getValue());
modelFileName = modelFileName + "/scale2.0x_model.json";
std::vector<std::unique_ptr<w2xc::Model> > models;
if (!w2xc::modelUtility::generateModelFromJSON(modelFileName, models))
std::exit(-1);
std::cout << "start scaling" << std::endl;
// 2x scaling
for (int nIteration = 0; nIteration < iterTimesTwiceScaling;
nIteration++) {
std::cout << "#" << std::to_string(nIteration + 1)
<< " 2x scaling..." << std::endl;
cv::Size imageSize = image.size();
imageSize.width *= 2;
imageSize.height *= 2;
cv::Mat image2xNearest;
cv::resize(image, image2xNearest, imageSize, 0, 0, cv::INTER_NEAREST);
std::vector<cv::Mat> imageSplit;
cv::Mat imageY;
cv::split(image2xNearest, imageSplit);
imageSplit[0].copyTo(imageY);
// generate bicubic scaled image and split
imageSplit.clear();
cv::Mat image2xBicubic;
cv::resize(image,image2xBicubic,imageSize,0,0,cv::INTER_CUBIC);
cv::split(image2xBicubic, imageSplit);
if(!w2xc::convertWithModels(imageY, imageSplit[0], models)){
std::cerr << "w2xc::convertWithModels : something error has occured.\n"
"stop." << std::endl;
std::exit(1);
};
cv::merge(imageSplit, image);
} // 2x scaling : end
if (shrinkRatio != 0.0) {
cv::Size lastImageSize = image.size();
lastImageSize.width =
static_cast<int>(static_cast<double>(lastImageSize.width
* shrinkRatio));
lastImageSize.height =
static_cast<int>(static_cast<double>(lastImageSize.height
* shrinkRatio));
cv::resize(image, image, lastImageSize, 0, 0, cv::INTER_LINEAR);
}
}
cv::cvtColor(image, image, cv::COLOR_YUV2RGB);
image.convertTo(image, CV_8U, 255.0);
std::string outputFileName = cmdOutputFile.getValue();
if (outputFileName == "(auto)") {
outputFileName = cmdInputFile.getValue();
int tailDot = outputFileName.find_last_of('.');
outputFileName.erase(tailDot, outputFileName.length());
outputFileName = outputFileName + "(" + cmdMode.getValue() + ")";
std::string &mode = cmdMode.getValue();
if(mode.find("noise") != mode.npos){
outputFileName = outputFileName + "(Level" + std::to_string(cmdNRLevel.getValue())
+ ")";
}
if(mode.find("scale") != mode.npos){
outputFileName = outputFileName + "(x" + std::to_string(cmdScaleRatio.getValue())
+ ")";
}
outputFileName += ".png";
}
cv::imwrite(outputFileName, image);
std::cout << "process successfully done!" << std::endl;
return 0;
}