Split and Merge functions

void split(InputArray m, OutputArrayOfArrays mv)
Divides a multi-channel array into several single-channel arrays.
Parameters:
  • src – input multi-channel array.
  • mv – output array or vector of arrays; in the first variant of the function the number of arrays must match src.channels(); the arrays themselves are reallocated, if needed.
void merge(InputArrayOfArrays mv, OutputArray dst)
Creates one multichannel array out of several single-channel ones.
Parameters:
  • mv – input array or vector of matrices to be merged; all the matrices in mv must have the same size and the same depth.
  • dst – output array of the same size and the same depth as mv[0]; The number of channels will be the total number of channels in the matrix array.

Example:

-----------
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

int main()
{
    Mat src=imread("image.jpg",1);    
    namedWindow("src",1);imshow("src",src);

    // Split the image into different channels
    vector<Mat> rgbChannels(3);
    split(src, rgbChannels);

    // Show individual channels
    Mat g, fin_img;
    g = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);
    
    // Showing Red Channel
    // G and B channels are kept as zero matrix for visual perception
    {
    vector<Mat> channels;
    channels.push_back(g);
    channels.push_back(g);
    channels.push_back(rgbChannels[2]);

    /// Merge the three channels
    merge(channels, fin_img);
    namedWindow("R",1);imshow("R", fin_img);
    }

    // Showing Green Channel
    {
    vector<Mat> channels;
    channels.push_back(g);
    channels.push_back(rgbChannels[1]);
    channels.push_back(g);    
    merge(channels, fin_img);
    namedWindow("G",1);imshow("G", fin_img);
    }

    // Showing Blue Channel
    {
    vector<Mat> channels;
    channels.push_back(rgbChannels[0]);
    channels.push_back(g);
    channels.push_back(g);
    merge(channels, fin_img);
    namedWindow("B",1);imshow("B", fin_img);
    }

    waitKey(0);
    return 0;
}

-----------

8 comments:

  1. http://kemppro.blogspot.com/2014/05/splitting-image-in-to-three-channels.html has c implementation

    ReplyDelete
  2. error:no instance of overloaded function "split" matches the argument list

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Why don't you just directly draw the channel??
    as in     namedWindow("B",1);imshow("B", rgbChannels[0]);

    ReplyDelete
  5. #include
    #include


    using namespace cv;
    using namespace std;

    int main(int argc, char** argv )
    {
    if ( argc != 2 )
    {
    printf("usage: DisplayImage.out \n");
    return -1;
    }


    Mat img,chans[3];
    img = imread(argv[1],1); //make sure its loaded with an image


    if(!img.data)
    {

    printf("La imagen no existe\n");
    return -1;

    }


    //split the channels in order to manipulate them
    split(img,chans);

    //by default opencv put channels in BGR order , so in your situation you want to copy the first channel which is blue. Set green and red channels elements to zero.
    chans[1]=Mat::zeros(img.rows, img.cols, CV_8UC1); // green channel is set to 0
    chans[2]=Mat::zeros(img.rows, img.cols, CV_8UC1);// red channel is set to 0


    // matrices por separado
    imshow("canal azul solo",chans[0]);

    //then merge them back
    merge(chans,3,img);

    //display
    imshow("BLUE CHAN",img);
    cvWaitKey();

    ReplyDelete