基于下面两个例子的理解:
示例一:4通道图像分割
1 #include<opencv2/opencv.hpp> 2 using namespace cv; 3 4 int main() 5 { 6 Mat bgra(500, 500, CV_8UC4, Scalar(255, 255, 0, 255)); 7 Mat bgr(bgra.rows, bgra.cols, CV_8UC3); 8 Mat alpha(bgra.rows, bgra.cols, CV_8UC1); 9 10 Mat out[] = { bgr, alpha }; 11 12 int from_to[] = { 0, 2, 1, 1, 2, 0, 3, 3 }; 13 mixChannels(&bgra, 1, out, 2, from_to, 4); 14 15 imshow("bgra", bgra); 16 imshow("bgr", bgr); 17 waitKey(0); 18 return 0; 19 }
示例二:HSV通道获取
1 #include<opencv2/opencv.hpp> 2 3 using namespace cv; 4 5 int main() 6 { 7 Mat src, hsv, dst; 8 src = imread("D:\hsv.jpg"); 9 if (src.empty()) 10 { 11 printf("can not load image "); 12 return -1; 13 } 14 namedWindow("input", WINDOW_AUTOSIZE); 15 imshow("input", src); 16 cvtColor(src, hsv, COLOR_BGR2HSV); 17 dst.create(hsv.size(), hsv.depth()); 18 //分离Hue/色相通道 19 int ch[] = { 0, 0 }; 20 mixChannels(&hsv, 1, &dst, 1, ch, 1); 21 imshow("H channel", dst); 22 //分离Saturation/饱和度通道 23 int ch1[] = { 1, 0 }; 24 mixChannels(&hsv, 1, &dst, 1, ch1, 1); 25 imshow("S channel", dst); 26 //分离Value/色调通道 27 int ch2[] = { 2, 0 }; 28 mixChannels(&hsv, 1, &dst, 1, ch2, 1); 29 imshow("V channel", dst); 30 31 waitKey(0); 32 return 0; 33 }