Skip to content

Commit 11fa009

Browse files
committed
Improve the documentation.
Add demo code for cv::reduce, cv::merge and cv::split.
1 parent 63cd581 commit 11fa009

File tree

5 files changed

+192
-4
lines changed

5 files changed

+192
-4
lines changed

modules/core/include/opencv2/core.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,13 @@ obtained. For example, the function can be used to compute horizontal and vertic
847847
raster image. In case of REDUCE_MAX and REDUCE_MIN , the output image should have the same type as the source one.
848848
In case of REDUCE_SUM and REDUCE_AVG , the output may have a larger element bit-depth to preserve accuracy.
849849
And multi-channel arrays are also supported in these two reduction modes.
850+
851+
The following code demonstrates its usage for a single channel matrix.
852+
@snippet snippets/core_reduce.cpp example
853+
854+
And the following code demonstrates its usage for a two-channel matrix.
855+
@snippet snippets/core_reduce.cpp example2
856+
850857
@param src input 2D matrix.
851858
@param dst output vector. Its size and type is defined by dim and dtype parameters.
852859
@param dim dimension index along which the matrix is reduced. 0 means that the matrix is reduced to
@@ -866,6 +873,10 @@ elements of i-th input array are treated as mv[i].channels()-element vectors.
866873
867874
The function cv::split does the reverse operation. If you need to shuffle channels in some other
868875
advanced way, use cv::mixChannels.
876+
877+
The following example shows how to merge 3 single channel matrices into a single 3-channel matrix.
878+
@snippet snippets/core_merge.cpp example
879+
869880
@param mv input array of matrices to be merged; all the matrices in mv must have the same
870881
size and the same depth.
871882
@param count number of input matrices when mv is a plain C array; it must be greater than zero.
@@ -889,6 +900,10 @@ The function cv::split splits a multi-channel array into separate single-channel
889900
\f[\texttt{mv} [c](I) = \texttt{src} (I)_c\f]
890901
If you need to extract a single channel or do some other sophisticated channel permutation, use
891902
mixChannels .
903+
904+
The following example demonstrates how to split a 3-channel matrix into 3 single channel matrices.
905+
@snippet snippets/core_split.cpp example
906+
892907
@param src input multi-channel array.
893908
@param mvbegin output array; the number of arrays must match src.channels(); the arrays themselves are
894909
reallocated, if needed.

modules/ts/include/opencv2/ts.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,22 +392,22 @@ class CV_EXPORTS TS
392392
FAIL_MEMORY_CORRUPTION_BEGIN=-7,
393393
FAIL_MEMORY_CORRUPTION_END=-8,
394394

395-
// the tested function (or test ifself) do not deallocate some memory
395+
// the tested function (or test itself) do not deallocate some memory
396396
FAIL_MEMORY_LEAK=-9,
397397

398398
// the tested function returned invalid object, e.g. matrix, containing NaNs,
399399
// structure with NULL or out-of-range fields (while it should not)
400400
FAIL_INVALID_OUTPUT=-10,
401401

402-
// the tested function returned valid object, but it does not match to
402+
// the tested function returned valid object, but it does not match
403403
// the original (or produced by the test) object
404404
FAIL_MISMATCH=-11,
405405

406406
// the tested function returned valid object (a single number or numerical array),
407407
// but it differs too much from the original (or produced by the test) object
408408
FAIL_BAD_ACCURACY=-12,
409409

410-
// the tested function hung. Sometimes, can be determined by unexpectedly long
410+
// the tested function hung. Sometimes, it can be determined by unexpectedly long
411411
// processing time (in this case there should be possibility to interrupt such a function
412412
FAIL_HANG=-13,
413413

@@ -448,7 +448,7 @@ class CV_EXPORTS TS
448448
std::vector<std::string> data_search_subdir;
449449
protected:
450450

451-
// these are allocated within a test to try keep them valid in case of stack corruption
451+
// these are allocated within a test to try to keep them valid in case of stack corruption
452452
RNG rng;
453453

454454
// information about the current test
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file core_merge.cpp
3+
* @brief It demonstrates the usage of cv::merge.
4+
*
5+
* It shows how to merge 3 single channel matrices into a 3-channel matrix.
6+
*
7+
* @author KUANG Fangjun
8+
* @date August 2017
9+
*/
10+
11+
#include <iostream>
12+
#include <opencv2/core.hpp>
13+
14+
using namespace std;
15+
using namespace cv;
16+
17+
int main()
18+
{
19+
//! [example]
20+
Mat m1 = (Mat_<uchar>(2,2) << 1,4,7,10);
21+
Mat m2 = (Mat_<uchar>(2,2) << 2,5,8,11);
22+
Mat m3 = (Mat_<uchar>(2,2) << 3,6,9,12);
23+
24+
Mat channels[3] = {m1, m2, m3};
25+
Mat m;
26+
merge(channels, 3, m);
27+
/*
28+
m =
29+
[ 1, 2, 3, 4, 5, 6;
30+
7, 8, 9, 10, 11, 12]
31+
m.channels() = 3
32+
*/
33+
//! [example]
34+
35+
return 0;
36+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @file core_reduce.cpp
3+
* @brief It demonstrates the usage of cv::reduce .
4+
*
5+
* It shows how to compute the row sum, column sum, row average,
6+
* column average, row minimum, column minimum, row maximum
7+
* and column maximum of a cv::Mat.
8+
*
9+
* @author KUANG Fangjun
10+
* @date August 2017
11+
*/
12+
13+
#include <iostream>
14+
#include <opencv2/core.hpp>
15+
16+
using namespace std;
17+
using namespace cv;
18+
19+
int main()
20+
{
21+
{
22+
//! [example]
23+
Mat m = (Mat_<uchar>(3,2) << 1,2,3,4,5,6);
24+
Mat col_sum, row_sum;
25+
26+
reduce(m, col_sum, 0, CV_REDUCE_SUM, CV_32F);
27+
reduce(m, row_sum, 1, CV_REDUCE_SUM, CV_32F);
28+
/*
29+
m =
30+
[ 1, 2;
31+
3, 4;
32+
5, 6]
33+
col_sum =
34+
[9, 12]
35+
row_sum =
36+
[3;
37+
7;
38+
11]
39+
*/
40+
//! [example]
41+
42+
Mat col_average, row_average, col_min, col_max, row_min, row_max;
43+
reduce(m, col_average, 0, CV_REDUCE_AVG, CV_32F);
44+
cout << "col_average =\n" << col_average << endl;
45+
46+
reduce(m, row_average, 1, CV_REDUCE_AVG, CV_32F);
47+
cout << "row_average =\n" << row_average << endl;
48+
49+
reduce(m, col_min, 0, CV_REDUCE_MIN, CV_8U);
50+
cout << "col_min =\n" << col_min << endl;
51+
52+
reduce(m, row_min, 1, CV_REDUCE_MIN, CV_8U);
53+
cout << "row_min =\n" << row_min << endl;
54+
55+
reduce(m, col_max, 0, CV_REDUCE_MAX, CV_8U);
56+
cout << "col_max =\n" << col_max << endl;
57+
58+
reduce(m, row_max, 1, CV_REDUCE_MAX, CV_8U);
59+
cout << "row_max =\n" << row_max << endl;
60+
61+
/*
62+
col_average =
63+
[3, 4]
64+
row_average =
65+
[1.5;
66+
3.5;
67+
5.5]
68+
col_min =
69+
[ 1, 2]
70+
row_min =
71+
[ 1;
72+
3;
73+
5]
74+
col_max =
75+
[ 5, 6]
76+
row_max =
77+
[ 2;
78+
4;
79+
6]
80+
*/
81+
}
82+
83+
{
84+
//! [example2]
85+
// two channels
86+
char d[] = {1,2,3,4,5,6};
87+
Mat m(3, 1, CV_8UC2, d);
88+
Mat col_sum_per_channel;
89+
reduce(m, col_sum_per_channel, 0, CV_REDUCE_SUM, CV_32F);
90+
/*
91+
col_sum_per_channel =
92+
[9, 12]
93+
*/
94+
//! [example2]
95+
}
96+
97+
return 0;
98+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file core_split.cpp
3+
* @brief It demonstrates the usage of cv::split .
4+
*
5+
* It shows how to split a 3-channel matrix into a 3 single channel matrices.
6+
*
7+
* @author KUANG Fangjun
8+
* @date August 2017
9+
*/
10+
11+
#include <iostream>
12+
#include <opencv2/core.hpp>
13+
14+
using namespace std;
15+
using namespace cv;
16+
17+
int main()
18+
{
19+
//! [example]
20+
char d[] = {1,2,3,4,5,6,7,8,9,10,11,12};
21+
Mat m(2, 2, CV_8UC3, d);
22+
Mat channels[3];
23+
split(m, channels);
24+
25+
/*
26+
channels[0] =
27+
[ 1, 4;
28+
7, 10]
29+
channels[1] =
30+
[ 2, 5;
31+
8, 11]
32+
channels[2] =
33+
[ 3, 6;
34+
9, 12]
35+
*/
36+
//! [example]
37+
38+
return 0;
39+
}

0 commit comments

Comments
 (0)