OpenCV学习笔记(三):多通道图像分离、混合算子:split(),merge()
#include <opencv2/opencv.hpp>#define BRG_BLUE_CHANNEL 0
#define BRG_GREEN_CHANNEL 1
#define BRG_RED_CHANNEL 2using namespace cv;int main(int argc, char *argv[])
{QCoreApplication a(argc, argv);if(MultiChannelBlending(BRG_BLUE_CHANNEL)&MultiChannelBlending(BRG_GREEN_CHANNEL)&MultiChannelBlending(BRG_RED_CHANNEL)){cout << endl << "\n运行成功,得出了需要的图像~! ";}waitKey(0);return a.exec();
}
多通道混合函数:
bool MultiChannelBlending(int what_channel )
{if(what_channel>=0&&what_channel<3){// 0、定义相关变量Mat srcImage;Mat logoImage;vector<Mat> channels; // 定义一个 Mat向量容器Mat image_Channel;// 1、读入图片logoImage = imread("F:/C++/2. OPENCV 3.1.0/TEST/dota_logo.jpg", 0); //单通道灰度图读取srcImage = imread("F:/C++/2. OPENCV 3.1.0/TEST/dota_jugg.jpg");if (!logoImage.data) { printf("Oh,no,读取logoImage错误~! \n"); return false; }if (!srcImage.data) { printf("Oh,no,读取srcImage错误~! \n"); return false; }// 2、把一个3通道图像转换成3个单通道图像split(srcImage, channels);//分离色彩通道// 3、将原图的通道引用返回给image_Channel,注意是引用,相当于两者等价,修改其中一个另一个跟着变image_Channel = channels.at(what_channel); // BGR 通道选择// 4、将原图的蓝色通道的(500,250)坐标处右下方的一块区域和logo图进行加权操作,将得到的混合结果存到image_Channel中addWeighted(image_Channel(Rect(500, 250, logoImage.cols, logoImage.rows)), 1.0,logoImage, 0.5, 0, image_Channel(Rect(500, 250, logoImage.cols, logoImage.rows)));// 5、将三个单通道重新合并成一个三通道merge(channels, srcImage);// 6、显示效果图switch(what_channel){case BRG_BLUE_CHANNEL:imshow(" <1>游戏原画+logo_BLUE通道", srcImage);break;case BRG_GREEN_CHANNEL:imshow(" <1>游戏原画+logo_GREEN通道", srcImage);break;case BRG_RED_CHANNEL:imshow(" <1>游戏原画+logo_RED通道", srcImage);break;}return true;}else{return false;}
}