欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

opencv笔记——分离颜色通道&多通道图像混合

程序员文章站 2022-05-16 14:59:32
...

多余的不说,详细函数解析请见

浅墨——毛星云

分离颜色通道&多通道图像混合

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;
bool Colorchannel();
int main()
{
    system("color 5E");
   if(Colorchannel())
        cout<<"operation success"<<endl;
   waitKey(0);
   return 0;
}
bool Colorchannel()
{
    //定义相关变量
    vector<Mat>channel;
    Mat logo_img;
    Mat Big_img;
    //读入图片
    logo_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/aixin.png",0);//logo灰度图
    Big_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/xinhaicheng.png");
    if((!logo_img.data)&&(!Big_img.data)) {
        cout<<"No image!"<<endl; return false;
    }
    Mat imgbluechannel,imggreenchannel,imgredchannel;//定义三个单通道图像
    split(Big_img,channel);//分离三通道
    //蓝色通道ROI区域与logo线性混合再赋值到ROI区域
    imgbluechannel=channel.at(0);
    addWeighted(imgbluechannel(Rect(0,0,logo_img.cols,logo_img.rows)),1.0,logo_img,0.5,0,
                imgbluechannel(Rect(0,0,logo_img.cols,logo_img.rows)));
                merge(channel,Big_img);//再将三个单通道再重新合并为一个三通道
                namedWindow("蓝色通道的分离与混合");
    imshow("蓝色通道的分离与混合",Big_img);
     logo_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/aixin.png",0);
    Big_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/xinhaicheng.png");
    if((!logo_img.data)&&(!Big_img.data)) {
        cout<<"No image!"<<endl; return false;
    }
    split(Big_img,channel);
    imggreenchannel=channel.at(1);
    //绿色通道ROI区域与logo线性混合再赋值到ROI区域
    addWeighted(imggreenchannel(Rect(0,0,logo_img.cols,logo_img.rows)),1.0,logo_img,0.5,0,
                imggreenchannel(Rect(0,0,logo_img.cols,logo_img.rows)));
                merge(channel,Big_img);
                namedWindow("绿色通道的分离与混合");
    imshow("绿色通道的分离与混合",Big_img);
    logo_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/aixin.png",0);
    Big_img=imread("C:/Users/xihua/Pictures/Saved Pictures/opencv操作图/xinhaicheng.png");
    if((!logo_img.data)&&(!Big_img.data)) {
        cout<<"No image!"<<endl; return false;
    }
    split(Big_img,channel);
    //红色通道ROI区域与logo线性混合再赋值到ROI区域
    imgredchannel=channel.at(2);
    addWeighted(imgredchannel(Rect(0,0,logo_img.cols,logo_img.rows)),1.0,logo_img,0.5,0,
                imgredchannel(Rect(0,0,logo_img.cols,logo_img.rows)));
                merge(channel,Big_img);
                namedWindow("红色通道的分离与混合");
    imshow("红色通道的分离与混合",Big_img);
    return true;
}