OpenCV 4 Computer Vision Application Programming Cookbook(Fourth Edition)
上QQ阅读APP看书,第一时间看更新

Splitting the image channels

You'll sometimes want to process the different channels of an image independently. For example, you might want to perform an operation only on one channel of the image. You can, of course, achieve this in an image-scanning loop. However, you can also use the cv::split function that will copy the three channels of a color image into three distinct cv::Mat instances. Suppose we want to add our rain image to the blue channel only. The following is how we would proceed:

   // create vector of 3 images 
   std::vector<cv::Mat> planes; 
   // split 1 3-channel image into 3 1-channel images 
   cv::split(image1,planes); 
   // add to blue channel 
   planes[0]+= image2; 
   // merge the 3 1-channel images into 1 3-channel image 
   cv::merge(planes,result);

The cv::merge function performs the inverse operation; that is, it creates a color image from three one-channel images.

We've successfully learned how to perform simple image arithmetic. Now let's move on to the next recipe!