OpenCV中cvAdds和cvAdd中的mask的用法探讨

Fn :

包含 mask 参数的一些函数如下 [摘自opencv 2.3.1版本 C++]

  • voidadd(InputArraysrc1, InputArraysrc2, OutputArraydst, InputArraymask=noArray(), intdtype=-1)
  • subtract
  • bitwise_and, bitwise_not, bitwise_or, bitwise_xor
  • mean, menStdDev
  • minMaxLoc
  • norm, normalize
  • grabCut
  • calcHist

Quote :

mask – Optional operation mask, 8-bit single channel array, that specifies elements of the destination array to be changed. [From OpenCV2.3.1 Doc]

mask 是一个8位的单通道数组,它指定了目标数组哪些元素会被修改。以 add 函数为例,请见 Code


Code :

1: /* 2: Author : Ggicci 3: Date : 2012.07.20 4: Title : How to use opencv's parameter - mask ? 5: */ 6: int main() 7: { 8: Mat m(10, 10, CV_8U); 9: //RNG类对象rng, 以统一分布(Uniform Distribution)随机填充矩阵 m 和 n 10: cv::RNG rng; 11: rng.fill(m, RNG::UNIFORM, 0, 10); 12: cout << "m = " << endl << m << endl << endl; 13: 14: Mat n(10, 10, CV_8U); 15: rng.fill(n, RNG::UNIFORM, 8, 10); 16: cout << "n = " << endl << n << endl << endl; 17: 18: Mat dst; 19: //指定 mask 20: Mat mask(10, 10, CV_8U, Scalar(0)); 21: Mat specified(mask, Rect(2, 5, 4, 4)); 22: specified.setTo(1); 23: cout << "mask = " << endl << mask << endl << endl; 24: 25: cv::add(m, n, dst, mask); 26: cout << "dst = " << endl << dst << endl << endl; 27: 28: return 0; 29: }

Output :

1: m = 2: [6, 7, 9, 9, 7, 0, 6, 3, 6, 9; 3: 1, 8, 7, 8, 5, 3, 8, 1, 7, 3; 4: 3, 3, 5, 4, 8, 2, 6, 1, 2, 2; 5: 6, 1, 0, 7, 3, 5, 0, 6, 3, 3; 6: 7, 5, 0, 5, 3, 0, 2, 7, 1, 7; 7: 9, 8, 8, 3, 9, 5, 4, 1, 8, 3; 8: 8, 1, 8, 7, 7, 0, 3, 8, 8, 3; 9: 8, 9, 5, 1, 1, 3, 3, 3, 4, 7; 10: 2, 7, 6, 8, 2, 4, 9, 5, 6, 1; 11: 5, 0, 5, 7, 8, 4, 1, 0, 4, 8] 12: 13: n = 14: [9, 8, 9, 8, 8, 9, 9, 9, 9, 9; 15: 9, 9, 9, 9, 9, 8, 8, 9, 9, 9; 16: 8, 8, 9, 9, 8, 9, 9, 9, 8, 9; 17: 9, 9, 8, 9, 9, 8, 8, 8, 8, 8; 18: 8, 8, 9, 8, 9, 9, 8, 8, 8, 8; 19: 9, 9, 9, 9, 8, 8, 8, 9, 9, 8; 20: 9, 9, 8, 8, 8, 9, 9, 8, 8, 9; 21: 9, 8, 9, 9, 8, 9, 8, 9, 9, 8; 22: 9, 8, 9, 9, 9, 9, 8, 9, 8, 8; 23: 8, 8, 9, 9, 9, 8, 9, 8, 9, 9] 24: 25: mask = 26: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 27: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 28: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 29: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 30: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0; 31: 0, 0, 1, 1, 1, 1, 0, 0, 0, 0; 32: 0, 0, 1, 1, 1, 1, 0, 0, 0, 0; 33: 0, 0, 1, 1, 1, 1, 0, 0, 0, 0; 34: 0, 0, 1, 1, 1, 1, 0, 0, 0, 0; 35: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 36: 37: dst = 38: [205, 205, 205, 205, 205, 205, 205, 205, 205, 205; 39: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205; 40: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205; 41: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205; 42: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205; 43: 205, 205, 17, 12, 17, 13, 205, 205, 205, 205; 44: 205, 205, 16, 15, 15, 9, 205, 205, 205, 205; 45: 205, 205, 14, 10, 9, 12, 205, 205, 205, 205; 46: 205, 205, 15, 17, 11, 13, 205, 205, 205, 205; 47: 205, 205, 205, 205, 205, 205, 205, 205, 205, 205] 48: 49: 请按任意键继续. . .//mask的红色部分指定了dst被修改的部分(同样以红色标出),其余部分为dst的初始化值