opencv(Python/c++):分离颜色通道split、多通道图像混合merge

效果图:

在这里插入图片描述

c++版本

  • 分离颜色通道split

源代码如下:

#include <iostream>
#include <opencv/cv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

//主函数
int main(void)
{
    vector<Mat> channels;//定义容器
    Mat imageBlueChannel;
    Mat imageGreenChannel;
    Mat imageRedChannel;
    Mat srcImage=imread("/home/liuxin/桌面/opencv/hammer.jpg");

    //把3通道图像转换成3个单通道图像
    split(srcImage,channels);

    imageBlueChannel=channels.at(0);
    imageGreenChannel=channels.at(1);
    imageRedChannel=channels.at(2);

    //显示
    namedWindow("original");
    moveWindow("original",20,20);
    namedWindow("blue");
    moveWindow("blue",100,60);
    namedWindow("green");
    moveWindow("green",20,100);
    namedWindow("red");
    moveWindow("red",100,100);
    imshow("original",srcImage);
    imshow("blue",imageBlueChannel);
    imshow("green",imageGreenChannel);
    imshow("red",imageRedChannel);
    while(1)
    {
        int key=cvWaitKey(10);
    if (key==27)
    {
        break;
    }
    }
    return(0);
}

  • 多通道图像混合merge
    源码如下:(也就是将上面分离的通道再合在一起,所以就不展示效果了)
#include <iostream>
#include <opencv/cv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

//主函数
int main(void)
{
    vector<Mat> channels;//定义容器
    Mat imageBlueChannel;
    Mat imageGreenChannel;
    Mat imageRedChannel;
    Mat srcImage=imread("/home/liuxin/桌面/opencv/hammer.jpg");

    //把3通道图像转换成3个单通道图像
    split(srcImage,channels);

    imageBlueChannel=channels.at(0);
    imageGreenChannel=channels.at(1);
    imageRedChannel=channels.at(2);

    //对分离的通道再进行合并
    Mat mergeImage;
    merge(channels,mergeImage);

    imshow("mergeImage",mergeImage);
    while(1)
    {
        int key=cvWaitKey(10);
    if (key==27)
    {
        break;
    }
    }
    return(0);
}

猜你喜欢

转载自blog.csdn.net/weixin_42755384/article/details/88209635