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

OpenCV分离颜色通道+多通道颜色图像混合

程序员文章站 2022-05-16 16:17:33
...

1、API

    Mat srcImage;
    Mat logoImage;
    vector<Mat> channels;
    Mat imageBlueChannel;

    logoImage = imread("dota_logo.jpg",0);
    srcImage = imread("dota_jugg.jpg");
    if(!logoImage.data){cout<<"Error"<<endl; return -1;}
    if(!srcImage.data){cout<<"Error"<<endl; return -1;}

    split(srcImage,channels);
    imageBlueChannel = channels.at(0);

    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);

主要是  1、颜色通道分离,2、以某一个通道加权混合,3、合并通道

2、完整代码

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;




bool MultiChannelBlending();


int main() {
    std::cout << "Hello, World!" << std::endl;

    if(MultiChannelBlending())
    {
        cout<<endl<<"嗯。好了,得出了你需要的混合值图像~";
    }


    waitKey(0);
    return 0;
}

bool MultiChannelBlending()
{
    Mat srcImage;
    Mat logoImage;
    vector<Mat> channels;
    Mat imageBlueChannel;

    logoImage = imread("dota_logo.jpg",0);
    srcImage = imread("dota_jugg.jpg");
    if(!logoImage.data){cout<<"Error"<<endl; return -1;}
    if(!srcImage.data){cout<<"Error"<<endl; return -1;}

    split(srcImage,channels);
    imageBlueChannel = channels.at(0);

    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("<1>游戏原画+logo蓝色通道 by浅墨");
    imshow("<1>游戏原画+logo蓝色通道 by浅墨",srcImage);

    //绿色通道
    Mat imageGreenChannel;
    srcImage = imread("dota_jugg.jpg");
    split(srcImage,channels);
    imageGreenChannel = channels[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("<2>游戏原画+G通道 by浅墨");
    imshow("<2>游戏原画+G通道 by浅墨",srcImage);

    //红色通道
    Mat imageRedChannel;
    srcImage = imread("dota_jugg.jpg");
    split(srcImage,channels);
    imageRedChannel = channels[2];
    addWeighted(imageRedChannel(Rect(500,250,logoImage.cols,logoImage.rows)),
                1.0,logoImage,0.5,0,
                channels[2](Rect(500,250,logoImage.cols,logoImage.rows)));
    merge(channels,srcImage);
    namedWindow("<3>游戏原画+R通道 by浅墨");
    imshow("<3>游戏原画+R通道 by浅墨",srcImage);



    return 1;
}
cmake_minimum_required(VERSION 3.14)
project(5_3_1)
find_package(OpenCV 3 REQUIRED)

include_directories(${OpenCV_DIRS})

set(CMAKE_CXX_STANDARD 11)

add_executable(5_3_1 main.cpp)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})