-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.cpp
174 lines (153 loc) · 5.93 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
/*******************************************************************
* main.cpp
* RGB2Y
*
* Author: Kareem Omar
* https://github.com/komrad36
*
* Last updated Sep 29, 2018
*******************************************************************/
//
// Fastest CPU (AVX/SSE) implementation of RGB to grayscale.
// Roughly 3x faster than OpenCV's implementation with AVX2, or 2x faster
// than OpenCV's implementation if using SSE only.
//
// Converts an RGB color image to grayscale.
//
// You can use equal weighting by calling the templated
// function with weight set to 'false', or you
// can specify non-equal weights (only slightly slower).
//
// The default non-equal weights match OpenCV's default.
//
// For even more speed see the CUDA version:
// github.com/komrad36/CUDARGB2Y
//
// If you do not have AVX2, uncomment the #define NO_AVX_PLEASE below to
// use only SSE isntructions. NOTE THAT THIS IS ABOUT 50% SLOWER.
// A processor with full AVX2 support is highly recommended.
//
// All functionality is contained in RGB2Y.h.
// 'main.cpp' is a demo and test harness.
//
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN
#include <chrono>
#include <iomanip>
#include <iostream>
#include <opencv2/opencv.hpp>
#include "RGB2Y.h"
using namespace std::chrono;
void printReport(const std::string& name, const nanoseconds& dur, const nanoseconds& comp = nanoseconds(0)) {
std::cout << std::left << std::setprecision(6) << std::setw(10) << name << " took " << std::setw(7) << static_cast<double>(dur.count()) * 1e-3 << " us";
if (comp.count() && comp < dur) {
std::cout << " (" << std::setprecision(4) << std::setw(4) << static_cast<double>(dur.count()) / comp.count() << "x slower than RGB2Y)." << std::endl;
}
else {
std::cout << '.' << std::endl;
}
}
int main() {
// ------------- Configuration ------------
constexpr auto warmups = 50;
constexpr auto runs = 100;
// OpenCV's impl is multithreaded so set to true for fair comparison
constexpr bool multithread = true;
constexpr bool weighted_averaging = true;
constexpr char name[] = "test.jpg";
constexpr bool generate_test_pixels = false;
// --------------------------------
// ------------- Image Read ------------
cv::Mat image;
if (!generate_test_pixels) {
image = cv::imread(name);
if (!image.data) {
std::cerr << "ERROR: failed to open image. Aborting." << std::endl;
return EXIT_FAILURE;
}
}
else {
constexpr int patch_size = 3 * 256 * 256 * 256;
image = cv::Mat(256, 256 * 256, CV_8UC3, new uint8_t[patch_size + 128]);
int x = 0;
for (int i = 0; i < 256; ++i) {
for (int j = 0; j < 256; ++j) {
for (int k = 0; k < 256; ++k) {
image.data[x++] = i;
image.data[x++] = j;
image.data[x++] = k;
}
}
}
}
// --------------------------------
// ------------- Ref ------------
nanoseconds RGB2Y_ref_ns;
uint8_t* refimage = new uint8_t[image.cols*image.rows];
std::cout << std::endl << "------------- Ref ------------" << std::endl << "Warming up..." << std::endl;
for (int i = 0; i < 0; ++i) RGB2Y_ref<weighted_averaging>(image.data, image.cols, image.rows, image.cols, refimage);
std::cout << "Testing..." << std::endl;
{
const high_resolution_clock::time_point start = high_resolution_clock::now();
for (int32_t i = 0; i < 1; ++i) {
RGB2Y_ref<weighted_averaging>(image.data, image.cols, image.rows, image.cols, refimage);
}
const high_resolution_clock::time_point end = high_resolution_clock::now();
RGB2Y_ref_ns = end - start;
}
// --------------------------------
// ------------- RGB2Y ------------
nanoseconds RGB2Y_ns;
uint8_t* rgb2y_image = new uint8_t[image.cols*image.rows];
std::cout << "------------- RGB2Y ------------" << std::endl << "Warming up..." << std::endl;
for (int i = 0; i < warmups; ++i) RGB2Y<multithread, weighted_averaging>(image.data, image.cols, image.rows, image.cols, rgb2y_image);
std::cout << "Testing..." << std::endl;
{
const high_resolution_clock::time_point start = high_resolution_clock::now();
for (int32_t i = 0; i < runs; ++i) {
RGB2Y<multithread, weighted_averaging>(image.data, image.cols, image.rows, image.cols, rgb2y_image);
}
const high_resolution_clock::time_point end = high_resolution_clock::now();
RGB2Y_ns = (end - start) / runs;
}
// --------------------------------
// ------------- OpenCV ------------
nanoseconds CV_ns;
cv::Mat newimage_cv;
if (weighted_averaging) {
std::cout << "------------ OpenCV ------------" << std::endl << "Warming up..." << std::endl;
for (int i = 0; i < warmups; ++i) cv::cvtColor(image, newimage_cv, CV_BGR2GRAY);
std::cout << "Testing..." << std::endl;
{
const high_resolution_clock::time_point start = high_resolution_clock::now();
for (int32_t i = 0; i < runs; ++i) {
cv::cvtColor(image, newimage_cv, CV_BGR2GRAY);
}
const high_resolution_clock::time_point end = high_resolution_clock::now();
CV_ns = (end - start) / runs;
}
}
// --------------------------------
// ------------- Verification ------------
int i = 0;
size_t count = 0;
std::cout << std::endl;
for (; i < image.cols*image.rows; ++i) {
if ((int)refimage[i] != (int)rgb2y_image[i]) {
//std::cerr << "ERROR! One or more pixels disagree!" << std::endl;
std::cerr << i << ": got " << +rgb2y_image[i] << ", should be " << /*(weighted_averaging ? */+refimage[i]/* : +refresult[i])*/ << std::endl;
++count;
//break;
}
}
//if (i == image.cols*image.rows) std::cout << "All pixels agree! Test valid." << std::endl << std::endl;
/*else */std::cout << count << " pixels disagree." << std::endl << std::endl;
// --------------------------------
// ------------- Output ------------
printReport("Ref", RGB2Y_ref_ns);
printReport("RGB2Y", RGB2Y_ns);
if (weighted_averaging) printReport("OpenCV", CV_ns, RGB2Y_ns);
std::cout << std::endl;
// --------------------------------
}