1. Image channel
  2. Image split
  3. Image merge
  4. The image mask

Image channel

Most of the images in daily use are color images, and color images are multi-channel images, respectively RGB channels
  • If you want to get a single channel image of a color image, you can use the split function
  • To merge an image matrix onto another image, use the merge function
API: Split () and merge() methods are a pair of reciprocal and inverse operations. Split is to separate image channels, while merge() is to merge channels. Grayscale images only have one channel, and generally RGB can separate three channels. Void split(const Mat& SRC, Mat* mvbegin); void split(InputArray m, OutputArrayOfArrays mv); void merge(const Mat* mv, size_t count, OutputArray dst) void merge(InputArrayOfArrays mv, The remaining parameters are all Mat type examples: // Color image splitting Mat imgSplits[3]; split(img, imgSplits); imshow("img", img); imshow("B", imgSplits[0]); imshow("G", imgSplits[1]); imshow("R", imgSplits[2]);Copy the code

The original imageBRG three channel diagram

  • It can be seen from the figure that the brightness of the face image in R channel is particularly obvious, because the face is reddish in the original picture, so the value in the red channel is relatively large
Alpha channel
  • An image with alpha channel has three color channels and one channel representing pixel transparency, known as alpha channel.
  • The alpha channel pixel has a value between 0 and 255, which defines how that pixel is treated when superimposed on another pixel.
  • 0 indicates that the pixel is completely transparent, 255 indicates that the pixel is completely opaque, and other values indicate the degree of transparency between (0 and 255)
Img = imread(path, IMREAD_UNCHANGED); //Mat img = imread(path); Cout << img.size() << endl; Cout << img.channels() << endl; imshow("png", img); // PNG Image 4-channel Splitting Of Mat pngSplits[4]; split(img, pngSplits); // merge the first three channels into a color image Mat bgrMat; merge(pngSplits, 3, bgrMat); imshow("bgrMat", bgrMat); // The last channel is the alpha channel imshow("alpha", pngSplits[3]);Copy the code

Original image with Alpha channel diagram

The figure below depicts the pixel data storage structure and channel separation/merge operation relationship of a general RGB graph in OpencV Mat

The image mask
imshow("img", img); // Generate mask Mat musk3 according to image properties; // Extract the feature values of B channel (0-100),G channel (0-100) and R channel (150-255) in the original IMG image. Then generate a new image inRange(IMG, Scalar(0, 0, 150), Scalar(100, 100, 255), MUSk3); imshow("musk3", musk3);Copy the code