OpenCV将通道设置为值C++

Ric*_*ves 3 c++ opencv

将席子的通道设置为值,同时让其他通道达到其当前值的最佳方法是什么?

例如,如果我有一个4通道Mat并且出于某种原因我需要将其中一个通道设置为一个值,但是其他通道保留其当前值,哪些操作最好?

谢谢!

Sam*_*mer 6

作为伪代码,您可以编写一个以这种方式输入图像的函数:

  1. 将图像拆分为通道
  2. 修改感兴趣的渠道
  3. 再次合并他们

例如

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);
Run Code Online (Sandbox Code Playgroud)