[Opencv初探之五]:图像通道分离与混合
示例代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
bool MultiChannelBlending();
int main( )
{
system("color5E");
if(MultiChannelBlending())
{
cout<<endl<<"finish preocess";
}
waitKey(0);
return 0;
}
bool MultiChannelBlending()
{
Mat srcImage;
Mat logoImage;
vector<Mat>channels;
Mat imageBlueChannel;
//=================【蓝色通道部分】=================
logoImage=imread("../logo.jpg",0);
srcImage=imread("../test2.jpg");
//把一个3通道图像转换成3个单通道图像
split(srcImage,channels);//分离色彩通道
imageBlueChannel=channels.at(0);
//将原图的蓝色通道的(500,250)坐标处右下方的一块区域和logo图进行加权操作,将得到的混合结果存到imageBlueChannel中
addWeighted(imageBlueChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,
logoImage,0.5,0,imageBlueChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
//将三个单通道重新合并成一个三通道
merge(channels,srcImage);
//显示效果图
namedWindow("blue channel");
imshow("blue channel",srcImage);
imwrite("blue.jpg",srcImage);
//=================【绿色通道部分】=================
Mat imageGreenChannel;
//重新读入图片
logoImage=imread("../logo.jpg",0);
srcImage=imread("../test2.jpg");
split(srcImage,channels);
imageGreenChannel=channels.at(1);
addWeighted(imageGreenChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,
logoImage,0.5,0.,imageGreenChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
//将三个独立的单通道重新合并成一个三通道
merge(channels,srcImage);
namedWindow("green channel");
imshow("green channel",srcImage);
imwrite("green.jpg",srcImage);
//=================【红色通道部分】=================
Mat imageRedChannel;
logoImage=imread("../logo.jpg",0);
srcImage=imread("../test2.jpg");
split(srcImage,channels);
imageRedChannel=channels.at(2);
addWeighted(imageRedChannel(Rect(500,250,logoImage.cols,logoImage.rows)),1.0,logoImage,0.5,0.,imageRedChannel(Rect(500,250,logoImage.cols,logoImage.rows)));
merge(channels,srcImage);
//显示效果图
namedWindow("red channel");
imshow("red channel",srcImage);
imwrite("red.jpg",srcImage);
return true;
}
效果如下: