学OpenCV
================================================
颜色模型之间可以互相转换
================================================
1 #include <iostream> 2 3 #include <opencv2/opencv.hpp> 4 #include <opencv2/core/utils/logger.hpp> 5 6 #include <vector> 7 #include <string> 8 9 void Test1() 10 { 11 cv::Mat img; 12 img = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)"); 13 if (img.empty()) 14 { 15 std::cout << "请确认图像文件名是否正确" << std::endl; 16 return ; 17 } 18 19 std::vector<cv::Mat> vctMatChannel; 20 cv::split(img, vctMatChannel); 21 22 int num = vctMatChannel.size(); 23 24 for (int i = 0; i < num; i++) 25 { 26 std::string strCaption = std::string("Test1 channel ") + std::to_string(i); 27 28 cv::imshow(strCaption, vctMatChannel[i]); 29 } 30 31 cv::Mat mat0 = cv::Mat::zeros(img.rows, img.cols,CV_8UC1); 32 vctMatChannel[0] = mat0;//BGR 33 vctMatChannel[1] = mat0;//BGR 34 cv::Mat mat00R; 35 cv::merge(vctMatChannel, mat00R); 36 cv::imshow("Test1 00R", mat00R); 37 38 vctMatChannel.push_back(mat0); 39 cv::Mat mat00R0; 40 cv::merge(vctMatChannel, mat00R0); 41 cv::imshow("Test1 00R0", mat00R0); 42 43 vctMatChannel.push_back(mat0); 44 cv::Mat mat00R00; 45 cv::merge(vctMatChannel, mat00R00); 46 //cv::imshow("Test1 00R00", mat00R00);//imshow崩溃。因为通常RGBA最多是4通道,因此多于4通道崩溃。 47 } 48 49 void Test2() 50 { 51 cv::Mat img; 52 img = cv::imread(R"(C:\testNoGit\learnOpenCV4-master\data\lena.png)"); 53 if (img.empty()) 54 { 55 std::cout << "请确认图像文件名是否正确" << std::endl; 56 return; 57 } 58 59 int num =img.channels(); 60 cv::Mat* arrMatChannel = new cv::Mat[num]; 61 cv::split(img, arrMatChannel); 62 63 64 for (int i = 0; i < num; i++) 65 { 66 std::string strCaption = std::string("Test2 channel ") + std::to_string(i); 67 68 cv::imshow(strCaption, arrMatChannel[i]); 69 } 70 71 cv::Mat mat0 = cv::Mat::zeros(img.rows, img.cols, CV_8UC1); 72 arrMatChannel[0] = mat0;//BGR 73 arrMatChannel[1] = mat0;//BGR 74 cv::Mat mat00R; 75 cv::merge(arrMatChannel,num, mat00R); 76 cv::imshow("Test2 00R", mat00R); 77 } 78 79 int main() 80 { 81 cv::utils::logging::setLogLevel(cv::utils::logging::LOG_LEVEL_ERROR); 82 83 Test1(); 84 Test2(); 85 86 cv::waitKey(0); 87 88 return 0; 89 }
================================================
查看效果
================================================
多于4通道无法用imshow查看,可以在image show里以数据的形式查看
标签:imshow,Mat,img,分离,合并,vctMatChannel,include,cv,通道 From: https://www.cnblogs.com/wlsandwho/p/18369737