forked from vencabkk/opencl-resizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizer.cpp
More file actions
executable file
·174 lines (139 loc) · 4.36 KB
/
Resizer.cpp
File metadata and controls
executable file
·174 lines (139 loc) · 4.36 KB
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
//
// Created by Vaclav Samec on 5/7/15 AD.
// Copyright (c) 2015 Venca. All rights reserved.
//
#include "Resizer.h"
#include "Profiler.h"
#include "OpenCL/oclManager.h"
#include "JPEGImage.h"
#include "Utils.h"
#include "OpenCL/kernels/resize_kernel.h"
void Resizer::resizeCL(const std::string& inputDir, const std::string& outputDir, float ratio, int quality, const std::string& options, std::unordered_map<std::string, float>& profiler)
{
std::vector<std::string> files;
auto outDir = outputDir;
std::string samplingAlgo;
Utils::getFilesDir(inputDir, files, ".jpg");
// check for input directory
if (files.empty())
{
std::cerr << "No input images found in: " << inputDir << std::endl;
return;
}
// check for output directory
if (outDir.empty())
{
outDir = files[0].substr(0, files[0].find_last_of("/\\") + 1) + "output";
}
if (!Utils::isDirectory(outDir))
{
Utils::createDirectory(outDir);
}
// check for re-sampling algorithm
for (const auto& e : entries)
{
if (e.find(options) != std::string::npos)
{
samplingAlgo = e;
break;
}
}
if (samplingAlgo.empty())
{
std::cerr << "Invalid resampling method, available methods: [nn, linear, bicubic]" << std::endl;
return;
}
// init OpenCL
oclManager ocl;
if (!ocl.createContext(oclManager::GPU))
{
return;
}
// compile program
if (!ocl.addKernelProgram(resizeKernel))
{
std::cerr << "Error building kernel." << std::endl;
return;
}
JPEGImage imageIn;
JPEGImage imageOut;
auto readProf = 0;
auto writeProf = 0;
auto resizeProf = 0;
for (int i=0; i<files.size(); i++)
{
profiler["op"] += 1;
auto r = Profiler::start();
if (imageIn.load(files[i]))
{
readProf += Profiler::stop(r);
auto res = Profiler::start();
ocl.resizeImage(imageIn, imageOut, ratio, samplingAlgo);
resizeProf += Profiler::stop(res);
std::string outFile = outDir + files[i].substr(files[0].find_last_of("/\\"));
auto w = Profiler::start();
imageOut.save(outFile, quality);
writeProf += Profiler::stop(w);
}
}
profiler["read"] += readProf;
profiler["write"] += writeProf;
profiler["resize"] += resizeProf;
}
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
void Resizer::resizeCV(const std::string &inputDir, const std::string &outputDir, float ratio, int quality,
const std::string &options, std::unordered_map<std::string, float>& profiler)
{
using namespace cv;
std::vector<std::string> files;
auto outDir = outputDir;
std::string samplingAlgo;
Utils::getFilesDir(inputDir, files, ".jpg");
// check for input directory
if (files.empty())
{
std::cerr << "No input images found in: " << inputDir << std::endl;
return;
}
// check for output directory
if (outDir.empty())
{
outDir = files[0].substr(0, files[0].find_last_of("/\\") + 1) + "output";
}
if (!Utils::isDirectory(outDir))
{
Utils::createDirectory(outDir);
}
Mat imageIn;
Mat imageOut;
std::vector<int> compression_params;
compression_params.push_back(IMWRITE_JPEG_QUALITY);
compression_params.push_back(quality);
auto readProf = 0;
auto writeProf = 0;
auto resizeProf = 0;
for (int i=0; i<files.size(); i++)
{
profiler["op"] += 1;
auto r = Profiler::start();
imageIn = imread(files[i]);
if(!imageIn.data)
{
std::cout << "Could not open or find the image" << std::endl ;
return;
}
readProf += Profiler::stop(r);
auto res = Profiler::start();
resize(imageIn, imageOut, Size(imageIn.cols * ratio, imageIn.rows * ratio), 0, 0, INTER_CUBIC);
resizeProf += Profiler::stop(res);
std::string outFile = outDir + files[i].substr(files[0].find_last_of("/\\"));
auto w = Profiler::start();
imwrite(outFile, imageOut, compression_params);
writeProf += Profiler::stop(w);
}
profiler["read"] += readProf;
profiler["write"] += writeProf;
profiler["resize"] += resizeProf;
}