3

What is the best way to set a channel of a mat to a value while letting the other channels to their current value?

For example, if I have a 4 channel Mat and for some reason I need to set one of the channels to a value, but with the others retaining their current values, what operations are the best?

Thanks!

Ricardo Alves
  • 1,071
  • 18
  • 36

1 Answers1

7

As pseudo code you can write a function that takes input an image this way:

  1. Split image into channels
  2. Modify channel of interest
  3. Merge them again

for example

Mat img(5,5,CV_64FC3); // declare three channels image 
Mat ch1, ch2, ch3; // declare three matrices 
// "channels" is a vector of 3 Mat arrays:
vector<Mat> channels(3);
// split img:
split(img, channels);
// get the channels (follow BGR order in OpenCV)
ch1 = channels[0];
ch2 = channels[1];
ch3 = channels[2]; 
// modify channel// then merge

merge(channels, img);
Samer
  • 1,923
  • 3
  • 34
  • 54