OpenCV (操作像素)
Eva.Q Lv9

越看不下去的部分,越要逼着自己跑代码。

访问像素值

Mat 类包含多种方法,可用来访问图像的各种属性:利用公共成员变量 colsrows 可得到图像的列数和行数;利用 at(int y, int x) 方法可以访问元素,其中 y 是行号,x 是列号。使用 at 方法时,须指定图像元素的类型,例如 image.at<uchar>(j, i) = 255; 必须保证指定的类型与矩阵内的类型是一致的。

彩色图像的每个像素对应三个部分:R,G,B ,因此包含彩色图像的 Mat 类会返回一个向量,向量中包含三个8位的数值。OpenCV 为这样的短向量定义了一种类型,即 cv::Vec3d 。这个向量包含三个无符号字符类型性的数据,访问像素用如下方式:image.at<cv::Vec3b>(j, i)[channel] = value; channel 用来指明三个颜色通道中的一个。 OpenCV 存储通道的顺序是 B,G,R 也可以直接用短向量:image.at<cv::Vec3b>(j, i) = cv::Vec3b(255, 255, 255)

我们随机选择一些像素,将其颜色置成白色。

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
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <random>

// Add salt noise to an image
void salt(cv::Mat image, int n) {

// C++11 random number generator
std::default_random_engine generator;
std::uniform_int_distribution<int> randomRow(0, image.rows - 1);
std::uniform_int_distribution<int> randomCol(0, image.cols - 1);

int i,j;
for (int k=0; k<n; k++) {

// random image coordinate
i= randomCol(generator);
j= randomRow(generator);

if (image.type() == CV_8UC1) { // gray-level image

// single-channel 8-bit image
image.at<uchar>(j,i)= 255;

} else if (image.type() == CV_8UC3) { // color image

// 3-channel image
image.at<cv::Vec3b>(j,i)[0]= 255;
image.at<cv::Vec3b>(j,i)[1]= 255;
image.at<cv::Vec3b>(j,i)[2]= 255;

// or simply:
// image.at<cv::Vec3b>(j, i) = cv::Vec3b(255, 255, 255);
}
}
}

// This is an extra version of the function
// to illustrate the use of cv::Mat_
// works only for a 1-channel image
void salt2(cv::Mat image, int n) {

// must be a gray-level image
CV_Assert(image.type() == CV_8UC1);

// C++11 random number generator
std::default_random_engine generator;
std::uniform_int_distribution<int> randomRow(0, image.rows - 1);
std::uniform_int_distribution<int> randomCol(0, image.cols - 1);

// use image with a Mat_ template
cv::Mat_<uchar> img(image);

// or with references:
// cv::Mat_<uchar>& im2= reinterpret_cast<cv::Mat_<uchar>&>(image);

int i,j;
for (int k=0; k<n; k++) {

// random image coordinate
i = randomCol(generator);
j = randomRow(generator);

// add salt
img(j,i)= 255;
}
}


int main()
{
// open the image
cv::Mat image= cv::imread("D:/colleage learning/Freshman_Summer Holiday Practice/opencv learning/Resources/OpenCVBook/boldt.jpg",1);

// call function to add noise
salt(image,3000);

// display result
cv::namedWindow("Image");
cv::imshow("Image",image);

// write on disk
cv::imwrite("salted.bmp",image);

cv::waitKey();

// test second version
image= cv::imread("D:/colleage learning/Freshman_Summer Holiday Practice/opencv learning/Resources/OpenCVBook/boldt.jpg",0);

salt2(image, 500);

cv::namedWindow("Image");
cv::imshow("Image",image);

cv::waitKey();

return 0;
}

扫描像素

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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

// 1st version
// see recipe Scanning an image with pointers
void colorReduce(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line

for (int j = 0; j < nl; j++) {

// get the address of row j
uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

data[i] = data[i] / div * div + div / 2;

// end of pixel processing ----------------

} // end of line
}
}

// version with input/ouput images
// see recipe Scanning an image with pointers
void colorReduceIO(const cv::Mat& image, // input image
cv::Mat& result, // output image
int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols; // number of columns
int nchannels = image.channels(); // number of channels

// allocate output image if necessary
result.create(image.rows, image.cols, image.type());

for (int j = 0; j < nl; j++) {

// get the addresses of input and output row j
const uchar* data_in = image.ptr<uchar>(j);
uchar* data_out = result.ptr<uchar>(j);

for (int i = 0; i < nc * nchannels; i++) {

// process each pixel ---------------------

data_out[i] = data_in[i] / div * div + div / 2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 1
// this version uses the dereference operator *
void colorReduce1(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line
uchar div2 = div >> 1; // div2 = div/2

for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {


// process each pixel ---------------------

*data++ = *data / div * div + div2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 2
// this version uses the modulo operator
void colorReduce2(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line
uchar div2 = div >> 1; // div2 = div/2

for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

int v = *data;
*data++ = v - v % div + div2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 3
// this version uses a binary mask
void colorReduce3(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0
uchar div2 = 1 << (n - 1); // div2 = div/2

for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

*data &= mask; // masking
*data++ |= div2; // add div/2

// end of pixel processing ----------------

} // end of line
}
}


// Test 4
// this version uses direct pointer arithmetic with a binary mask
void colorReduce4(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
int step = image.step; // effective width
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0
uchar div2 = div >> 1; // div2 = div/2

// get the pointer to the image buffer
uchar* data = image.data;

for (int j = 0; j < nl; j++) {

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

*(data + i) &= mask;
*(data + i) += div2;

// end of pixel processing ----------------

} // end of line

data += step; // next line
}
}

// Test 5
// this version recomputes row size each time
void colorReduce5(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0

for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < image.cols * image.channels(); i++) {

// process each pixel ---------------------

*data &= mask;
*data++ += div / 2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 6
// this version optimizes the case of continuous image
void colorReduce6(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // total number of elements per line

if (image.isContinuous()) {
// then no padded pixels
nc = nc * nl;
nl = 1; // it is now a 1D array
}

int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0
uchar div2 = div >> 1; // div2 = div/2

// this loop is executed only once
// in case of continuous images
for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

*data &= mask;
*data++ += div2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 7
// this versions applies reshape on continuous image
void colorReduce7(cv::Mat image, int div = 64) {

if (image.isContinuous()) {
// no padded pixels
image.reshape(1, // new number of channels
1); // new number of rows
}
// number of columns set accordingly

int nl = image.rows; // number of lines
int nc = image.cols * image.channels(); // number of columns

int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0
uchar div2 = div >> 1; // div2 = div/2

for (int j = 0; j < nl; j++) {

uchar* data = image.ptr<uchar>(j);

for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

*data &= mask;
*data++ += div2;

// end of pixel processing ----------------

} // end of line
}
}

// Test 8
// this version processes the 3 channels inside the loop with Mat_ iterators
void colorReduce8(cv::Mat image, int div = 64) {

// get iterators
cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>();
uchar div2 = div >> 1; // div2 = div/2

for (; it != itend; ++it) {

// process each pixel ---------------------

(*it)[0] = (*it)[0] / div * div + div2;
(*it)[1] = (*it)[1] / div * div + div2;
(*it)[2] = (*it)[2] / div * div + div2;

// end of pixel processing ----------------
}
}

// Test 9
// this version uses iterators on Vec3b
void colorReduce9(cv::Mat image, int div = 64) {

// get iterators
cv::MatIterator_<cv::Vec3b> it = image.begin<cv::Vec3b>();
cv::MatIterator_<cv::Vec3b> itend = image.end<cv::Vec3b>();

const cv::Vec3b offset(div / 2, div / 2, div / 2);

for (; it != itend; ++it) {

// process each pixel ---------------------

*it = *it / div * div + offset;
// end of pixel processing ----------------
}
}

// Test 10
// this version uses iterators with a binary mask
void colorReduce10(cv::Mat image, int div = 64) {

// div must be a power of 2
int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0
uchar div2 = div >> 1; // div2 = div/2

// get iterators
cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>();
cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>();

// scan all pixels
for (; it != itend; ++it) {

// process each pixel ---------------------

(*it)[0] &= mask;
(*it)[0] += div2;
(*it)[1] &= mask;
(*it)[1] += div2;
(*it)[2] &= mask;
(*it)[2] += div2;

// end of pixel processing ----------------
}
}

// Test 11
// this versions uses ierators from Mat_
void colorReduce11(cv::Mat image, int div = 64) {

// get iterators
cv::Mat_<cv::Vec3b> cimage = image;
cv::Mat_<cv::Vec3b>::iterator it = cimage.begin();
cv::Mat_<cv::Vec3b>::iterator itend = cimage.end();
uchar div2 = div >> 1; // div2 = div/2

for (; it != itend; it++) {

// process each pixel ---------------------

(*it)[0] = (*it)[0] / div * div + div2;
(*it)[1] = (*it)[1] / div * div + div2;
(*it)[2] = (*it)[2] / div * div + div2;

// end of pixel processing ----------------
}
}


// Test 12
// this version uses the at method
void colorReduce12(cv::Mat image, int div = 64) {

int nl = image.rows; // number of lines
int nc = image.cols; // number of columns
uchar div2 = div >> 1; // div2 = div/2

for (int j = 0; j < nl; j++) {
for (int i = 0; i < nc; i++) {

// process each pixel ---------------------

image.at<cv::Vec3b>(j, i)[0] = image.at<cv::Vec3b>(j, i)[0] / div * div + div2;
image.at<cv::Vec3b>(j, i)[1] = image.at<cv::Vec3b>(j, i)[1] / div * div + div2;
image.at<cv::Vec3b>(j, i)[2] = image.at<cv::Vec3b>(j, i)[2] / div * div + div2;

// end of pixel processing ----------------

} // end of line
}
}


// Test 13
// this version uses Mat overloaded operators
void colorReduce13(cv::Mat image, int div = 64) {

int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0) + 0.5);
// mask used to round the pixel value
uchar mask = 0xFF << n; // e.g. for div=16, mask= 0xF0

// perform color reduction
image = (image & cv::Scalar(mask, mask, mask)) + cv::Scalar(div / 2, div / 2, div / 2);
}

// Test 14
// this version uses a look up table
void colorReduce14(cv::Mat image, int div = 64) {

cv::Mat lookup(1, 256, CV_8U);

for (int i = 0; i < 256; i++) {

lookup.at<uchar>(i) = i / div * div + div / 2;
}

cv::LUT(image, lookup, image);
}

#define NTESTS 15
#define NITERATIONS 10

int main()
{
// read the image
cv::Mat image = cv::imread("D:/colleage learning/Freshman_Summer Holiday Practice/opencv learning/Resources/OpenCVBook/boldt.jpg");

// time and process the image
const int64 start = cv::getTickCount();
colorReduce(image, 64);
//Elapsed time in seconds
double duration = (cv::getTickCount() - start) / cv::getTickFrequency();

// display the image
std::cout << "Duration= " << duration << "secs" << std::endl;
cv::namedWindow("Image");
cv::imshow("Image", image);

cv::waitKey();

// test different versions of the function

int64 t[NTESTS], tinit;
// timer values set to 0
for (int i = 0; i < NTESTS; i++)
t[i] = 0;

cv::Mat images[NTESTS];
cv::Mat result;

// the versions to be tested
typedef void(*FunctionPointer)(cv::Mat, int);
FunctionPointer functions[NTESTS] = { colorReduce, colorReduce1, colorReduce2, colorReduce3, colorReduce4,
colorReduce5, colorReduce6, colorReduce7, colorReduce8, colorReduce9,
colorReduce10, colorReduce11, colorReduce12, colorReduce13, colorReduce14 };
// repeat the tests several times
int n = NITERATIONS;
for (int k = 0; k < n; k++) {

std::cout << k << " of " << n << std::endl;

// test each version
for (int c = 0; c < NTESTS; c++) {

images[c] = cv::imread("boldt.jpg");

// set timer and call function
tinit = cv::getTickCount();
functions[c](images[c], 64);
t[c] += cv::getTickCount() - tinit;

std::cout << ".";
}

std::cout << std::endl;
}

// short description of each function
std::string descriptions[NTESTS] = {
"original version:",
"with dereference operator:",
"using modulo operator:",
"using a binary mask:",
"direct ptr arithmetic:",
"row size recomputation:",
"continuous image:",
"reshape continuous image:",
"with iterators:",
"Vec3b iterators:",
"iterators and mask:",
"iterators from Mat_:",
"at method:",
"overloaded operators:",
"look-up table:",
};

for (int i = 0; i < NTESTS; i++) {

cv::namedWindow(descriptions[i]);
cv::imshow(descriptions[i], images[i]);
}

// print average execution time
std::cout << std::endl << "-------------------------------------------" << std::endl << std::endl;
for (int i = 0; i < NTESTS; i++) {

std::cout << i << ". " << descriptions[i] << 1000. * t[i] / cv::getTickFrequency() / n << "ms" << std::endl;
}

cv::waitKey();
return 0;
}

为了说明图像扫描的过程,我们通过减少图像中颜色的数量来体现。

用指针扫描图像
用迭代器扫描图像
图像扫描循环

扫描并访问相邻像素

  • Post title:OpenCV (操作像素)
  • Post author:Eva.Q
  • Create time:2021-08-24 16:11:24
  • Post link:https://qyy/2021/08/24/OPENCV/OPENCV2-4/
  • Copyright Notice:All articles in this blog are licensed under BY-NC-SA unless stating additionally.