#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("colors.jpg");
if (img.empty()) {
cout << "图像读取失败" << endl;
return -1;
}
//一、数组类型的分离与合并
Mat HSV;
cvtColor(img, HSV, COLOR_BGR2HSV);
Mat img_b, img_g, img_r; //存放数组类型结果
Mat imgs[3]; //对单通道数组进行赋值
split(img, imgs);
img_b = imgs[0];
img_g = imgs[1];
img_r = imgs[2];
imshow("B通道", img_b); //分别展示分离后的R,G,B通道
imshow("G通道", img_g);
imshow("R通道", img_r);
Mat merged[3];
Mat result;
merged[0] = img_b;
merged[1] = img_g;
merged[2] = img_r;
merge(merged, 3, result);
imshow("merged", result);
//二、vector类型的分离与合并
vector<Mat> img_hsv;
vector<Mat> merged_hsv;
Mat result2;
split(HSV, img_hsv);
Mat img_h, img_s, img_v;
img_h = img_hsv.at(0);
img_s = img_hsv.at(1);
img_v = img_hsv.at(2);
imshow("hsv", HSV); //展示原始的HSV图像
imshow("H通道", img_h); //分别展示分离后的H,S,V通道
imshow("S通道", img_s);
imshow("V通道", img_v);
merged_hsv.push_back(img_h);
merged_hsv.push_back(img_s);
merged_hsv.push_back(img_v);
merge(merged_hsv, result2);
imshow("result2", result2); //合并后的hsv通道图像
waitKey(0);
return 0;
}
参考资料:
《OpenCV4快速入门》