-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.cpp
275 lines (259 loc) · 8.16 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include <iostream>
#include <assert.h>
#include <atomic>
#include "opencv2/core/core.hpp"
using namespace std;
void method1(cv::Mat);
void method2(cv::Mat);
void method3(cv::Mat);
void method4(cv::Mat);
void method5(cv::Mat);
void method5_2(cv::Mat);
void method5_3(cv::Mat); // 最快的
void method6(cv::Mat);
void method7(cv::Mat);
void method8(cv::Mat);
int main(int argc, char* argv[])
{
cv::Size imgSize(6400,4800);
cv::Mat image = cv::Mat(imgSize, CV_8UC3, cv::Scalar(1,1,1));
method1(image);
method2(image);
method3(image);
method4(image);
method5(image);
method5_2(image);
method5_3(image);
method6(image);
method7(image);
method8(image);
}
void method1(cv::Mat img){
// at access with Vec3b Vector
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum = 0;
for(int row=0; row < height; row++){
for(int col=0; col < width; col++){
cv::Vec3b uc_pixel = img.at<cv::Vec3b>(row, col);
int a = uc_pixel[0];
int b = uc_pixel[1];
int c = uc_pixel[2];
sum += a + b + c;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method1: " << time << std::endl;
}
void method2(cv::Mat img){
// direct at access
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum = 0;
for(int row=0; row < height; row++){
for(int col=0; col < width; col++){
int a = img.at<cv::Vec3b>(row, col)[0];
int b = img.at<cv::Vec3b>(row, col)[1];
int c = img.at<cv::Vec3b>(row, col)[2];
sum += a + b + c;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method2: " << time << std::endl;
}
void method3(cv::Mat img){
// pointer + Vec3b vector
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum = 0;
for(int row=0; row < height; row++){
cv::Vec3b *ptr = img.ptr<cv::Vec3b>(row);
for(int col=0; col < width; col++){
cv::Vec3b pixel = ptr[col];
int a = pixel[0];
int b = pixel[1];
int c = pixel[2];
sum += a + b + c;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method3: " << time << std::endl;
}
void method4(cv::Mat img){
// pointer
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum = 0;
for(int row=0; row < height; row++){
cv::Vec3b *ptr = img.ptr<cv::Vec3b>(row);
for(int col=0; col < width; col++){
int a = ptr[col][0];
int b = ptr[col][1];
int c = ptr[col][2];
sum += a + b + c;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method4: " << time << std::endl;
}
void method5(cv::Mat img){
// raw pointer
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum=0;
for(int row=0; row < height; row++){
const uchar *ptr = img.ptr(row);
for(int col=0; col < width; col++){
const uchar *uc_pixel = ptr;
int a = uc_pixel[0];
int b = uc_pixel[1];
int c = uc_pixel[2];
// int a = ptr[0];
// int b = ptr[1];
// int c = ptr[2];
sum += a + b + c;
ptr += 3;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method5: " << time << std::endl;
}
void method5_2(cv::Mat img){
// raw pointer
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum=0;
for(int row=0; row < height; row++){
const uchar *ptr = img.ptr(row);
for(int col=0; col < width; col++){
// const uchar *uc_pixel = ptr;
// int a = uc_pixel[0];
// int b = uc_pixel[1];
// int c = uc_pixel[2];
// 不使用中间指针
int a = ptr[0];
int b = ptr[1];
int c = ptr[2];
sum += a + b + c;
ptr += 3;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method5_2: " << time << std::endl;
}
void method5_3(cv::Mat img){
// raw pointer
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
// 如果内存连续排列,当做单行处理
if(img.isContinuous()) {
width = width * height;
height = 1;
} else {
printf("img.isContinuous() is false\n");
}
int sum = 0;
for(int row = 0; row < height; row++){
const uchar *ptr = img.ptr(row);
for(int col = 0; col < width; col++){
sum += ptr[0] + ptr[1] + ptr[2];
ptr += 3;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method5_3: " << time << std::endl;
}
void method6(cv::Mat img){
// raw pointer + raw step
// 必须保证Mat在内存的存储是连续的
double t0 = (double) cv::getTickCount();
int height = img.rows;
int width = img.cols;
int sum = 0;
const uchar *uc_pixel = img.data;
for(int row=0; row < height; row++){
uc_pixel = img.data + row*img.step;
for(int col=0; col < width; col++){
int a = uc_pixel[0];
int b = uc_pixel[1];
int c = uc_pixel[2];
sum += a + b + c;
uc_pixel += 3;
}
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method6: " << time << std::endl;
}
void method7(cv::Mat image){
double t0 = (double) cv::getTickCount();
int height = image.rows;
int width = image.cols;
cv::MatConstIterator_<cv::Vec3b> it = image.begin<cv::Vec3b>(), it_end = image.end<cv::Vec3b>();
int sum = 0;
for(; it != it_end; ++it){
int a = (*it)[0];
int b = (*it)[1];
int c = (*it)[2];
sum += a + b + c;
}
assert(sum==3*height*width);
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method7: " << time << std::endl;
}
// C++11 Lambda
//typedef cv::Point3_<uint8_t> Pixel;
typedef cv::Vec3b Pixel;
void method8(cv::Mat image){
if(image.isContinuous()) {
printf("img.isContinuous() is true\n");
} else {
printf("img.isContinuous() is false\n");
}
double t0 = (double) cv::getTickCount();
int height = image.rows;
int width = image.cols;
// 用forEach,要小心Data Race!所以不一定高效的,反而有可能是最慢的
#if 1
std::atomic<int> num;
std::atomic<int> sum;
#else
// 这个是不能用的,会有Data Race!
int num, sum;
#endif
num = 0;
sum = 0;
image.forEach<Pixel>(
[&](Pixel &pixel, const int * position) -> void {
if(pixel[0] != 1 or pixel[1] != 1 or pixel[2] != 1) {
printf("pixel[0] = %d, pixel[1] = %d, pixel[2] = %d\n", pixel[0], pixel[1], pixel[2]);
}
//sum += pixel.x + pixel.y + pixel.z;
sum += pixel[0] + pixel[1] + pixel[2];
num++;
}
);
#if 1
printf("num = %d, sum = %d, sum/3 = %d, 3*height*width = %d\n", num.load(), sum.load(), sum.load()/3, 3*height*width);
assert(sum.load()==3*height*width);
#else
printf("num = %d, sum = %d, sum/3 = %d, 3*height*width = %d\n", num, sum, sum/3, 3*height*width);
assert(sum==3*height*width);
#endif
double time = ((double) cv::getTickCount() - t0) / cv::getTickFrequency();
std::cout << "Time for method8: " << time << std::endl;
}