OpenCV split函数与merge函数

split


Divides a multi-channel array into several single-channel arrays.

C++:void split(const Mat& src, Mat* mvbegin)

C++:void split(InputArray m, OutputArrayOfArrays mv)

Python:cv2.split(m[, mv]) → mv

C:void cvSplit(const CvArr* src, CvArr* dst0, CvArr* dst1, CvArr* dst2, CvArr* dst3)

Python:cv.Split(src, dst0, dst1, dst2, dst3) → None

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.

The functions split split a multi-channel array into separate single-channel arrays:

If you need to extract a single channel or do some other sophisticated channel permutation, use mixChannels() .


merge


Creates one multichannel array out of several single-channel ones.

C++:void merge(const Mat* mv, size_t count, OutputArray dst)

C++:void merge(InputArrayOfArrays mv, OutputArray dst)

Python:cv2.merge(mv[, dst]) → dst

C:void cvMerge(const CvArr* src0, const CvArr* src1, const CvArr* src2, const CvArr* src3, CvArr* dst)

Python:cv.Merge(src0, src1, src2, src3, dst) → None

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.
  • count – number of input matrices when mv is a plain C array; it must be greater than zero.
  • 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.

The functions merge merge several arrays to make a single multi-channel array. That is, each element of the output array will be a concatenation of the elements of the input arrays, where elements of i-th input array are treated as mv[i].channels()-element vectors.

The function split() does the reverse operation. If you need to shuffle channels in some other advanced way, use mixChannels() .


Code Example:

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

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
  Mat src = imread("/home/abb/Downloads/lena.jpg", IMREAD_COLOR);
  if (src.empty()) {
    cout << "Image is empty" << endl;
    return -1;
  }
  if(src.isContinuous()){
    cout << "Image is Continuous" <<endl;
  }
  imshow("src", src);
  std::vector<Mat> bgrChannels;
  split(src, bgrChannels);

  Mat blank_ch = Mat::zeros(Size(src.cols, src.rows), CV_8UC1);

  std::vector<cv::Mat> channels_b;
  Mat imgBFinal;
  channels_b.push_back(bgrChannels[0]);
  channels_b.push_back(blank_ch);
  channels_b.push_back(blank_ch);
  merge(channels_b,imgBFinal);
  imshow("Blue", imgBFinal);

  std::vector<cv::Mat> channels_g;
  Mat imgGFinal;
  channels_g.push_back(blank_ch);
  channels_g.push_back(bgrChannels[1]);
  channels_g.push_back(blank_ch);
  merge(channels_g,imgGFinal);
  imshow("Green", imgGFinal);

  std::vector<cv::Mat> channels_r;
  Mat imgRFinal;
  channels_r.push_back(blank_ch);
  channels_r.push_back(blank_ch);
  channels_r.push_back(bgrChannels[2]);
  merge(channels_r,imgRFinal);
  imshow("Red", imgRFinal);

  waitKey(0);

  return 0;
}

 


版权声明:本文为u011608180原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。