mixChannels () function appreciated
mixChannels () is a function related to image processing channels can be implemented as a combination of complex channel
Article Directory
1. function prototype
Function Prototype 1.1 (a)
void cv::mixChannels (
InputArrayOfArrays src,
InputOutputArrayOfArrays dst,
const std::vector< int > & fromTo
)
The first parameter: input matrix
the second argument: an output matrix of
the third parameter: the replication list, showing several channels of the first copy of the input matrix to the second matrix output several channels
such as {0,2,1,1, represents 2,0}:
the src to dst copy color channels 0 2 color channels
the src to dst color channel color channel 1 1 copy
the src to dst copy color channel color channel 2 0
FIG:
It should be noted that the structure of the input matrix and an output matrix must be predefined.
1.2 Function Prototype (II)
void cv::mixChannels
(
const Mat * src,
size_t nsrcs,
Mat * dst,
size_t ndsts,
const int * fromTo,
size_t npairs
)
The first parameter: input matrix
the second argument: the number of the input matrix of
the third parameter: the output matrix
fourth parameter: the number of output matrix
fifth parameter: replication list
copy number list: sixth parameter
In this function prototype, if the input matrix and output matrix are written to 1, just like the first prototype is consistent with the function.
If not 1, can be achieved and matrix assembly, or a plurality of matrix into multiple matrix complex functions.
2 illustrates
2.1 color channel exchange
Here a function prototype using the first example described, since a three-channel matrix is defined, and the channels 0 and 2 are swapped
Mat m(500, 500, CV_8UC3, Scalar(255, 255, 0));
Mat m2(500, 500, CV_8UC3);
vector<Mat> channels;
split(m, channels);
imshow("m", m);
imshow("0", channels.at(0));
imshow("1", channels.at(1));
imshow("2", channels.at(2));
vector<int> fromTo = {0,2,1,1,2,0};
mixChannels(m, m2, fromTo);
vector<Mat> channels2;
split(m2, channels2);
imshow("/m", m2);
imshow("/0", channels2.at(0));
imshow("/1", channels2.at(1));
imshow("/2", channels2.at(2));
waitKey(0);
Before the exchange:
After the exchange:
2.2 Quad segmentation matrix
To a four-channel matrix using mixChannels () function is divided into a three-channel single-channel matrix and a matrix, and has a correspondence relationship:
// bgra[0] -> bgr[2], bgra[1] -> bgr[1],
// bgra[2] -> bgr[0], bgra[3] -> alpha[0]
Mat bgra(500, 500, CV_8UC4, Scalar(255, 0, 0, 255));
Mat bgr(bgra.rows, bgra.cols, CV_8UC3);
Mat alpha(bgra.rows, bgra.cols, CV_8UC1);
Mat out[] = { bgr,alpha };
int fromTo[] = { 0, 2, 1, 1, 2, 0, 3, 3 };
mixChannels(&bgra, 1, out, 2, fromTo, 4);
imshow("bgra", bgra);
imshow("bgr", bgr);
imshow("alpha", alpha);
operation result:
references
[1] opencv mixChannels () Explanation: 4-channel image segmentation, HSV acquisition channel http://www.pianshen.com/article/4148149685/