opencv 图像通道操作

分割

vector<Mat> channels;

split(image1, channels);//分割image1的通道

split(imageROI2, channels);

Mat channels1 = channels[0];//获取通道1

Mat channels2 = channels[1];//获取通道2

Mat channels3 = channels[2];//获取通道3

合并

Mat MultiImage;

merge(channels, MultiImage);

 

#include "opencv2/opencv.hpp"
using namespace cv;

int main()
{

    Mat original = imread("girl.jpg",CV_LOAD_IMAGE_COLOR);

    Mat splitChannels[3];  // 声明Mat数组,共3个元素,存放分离后的通道

    split(original, splitChannels);  // 分离original

    // 分别显示B,G,R,
    imshow("B", splitChannels[0]);
    imshow("G", splitChannels[1]);
    imshow("R", splitChannels[2]);
    waitKey();

    // 把 R 通道像素值全部清零,那么这个通道就显示为黑色
    splitChannels[2] = Mat::zeros(splitChannels[2].size(), CV_8UC1);

    Mat output;
    
    // 把3个通道合并,保存到output中, 然后显示
    merge(splitChannels, 3, output);
    imshow("Merged", output);
    waitKey();

    return 0;
}

 

posted @ 2020-10-28 09:57  玥茹苟  阅读(517)  评论(0编辑  收藏  举报