opencv3.3 基础:Mat类里setTo函数

 /** @brief Sets all or some of the array elements to the specified value.
    This is an advanced variant of the Mat::operator=(const Scalar& s) operator.
    @param value Assigned scalar converted to the actual array type.
    @param mask Operation mask of the same size as \*this. Its non-zero elements indicate which matrix
    elements need to be copied. The mask has to be of type CV_8U and can have 1 or multiple channels
*/
   
Mat& setTo(InputArray value, InputArray mask=noArray());

说明:

1、功能:把矩阵mask中元素不为0的点全部变为value值;

2、当默认不添加mask的时候,表明mask是一个与原图尺寸大小一致的且元素值全为非0的矩阵,因此不加mask的时候,

会将原矩阵的像素值全部赋值为value

3、 setTo还有更为高级的用法,比如,对于一个已知的src,我们要将其中大于或者小于某个值的像素值设置为指定的值,则可以如下:
src.setTo(0,src < 10);//当src中的某个像素值小于10的时候,就将该值设置成0.

不过,并不支持src.setTo(dst, src<10);

示例:

	Mat srcx(3, 3, CV_8UC1, Scalar(0));
	Mat mask1(3, 3, CV_8UC1, Scalar(0));
	Mat mask2(3, 3, CV_8UC1, Scalar(255));

	srcx.setTo(100, mask1);
	cout << "1.\n";
	cout << srcx << endl;

	srcx.setTo(100, mask2);
	cout << "2.\n";
	cout << srcx << endl;

	srcx.setTo(155);
	cout << "3.\n";
	cout << srcx << endl;

	srcx.setTo(0, srcx < 188);
	cout << "4.\n";
	cout << srcx << endl;

结果: 

1.
[  0,   0,   0;
   0,   0,   0;
   0,   0,   0]
2.
[100, 100, 100;
 100, 100, 100;
 100, 100, 100]
3.
[155, 155, 155;
 155, 155, 155;
 155, 155, 155]
4.
[  0,   0,   0;
   0,   0,   0;
   0,   0,   0]

猜你喜欢

转载自blog.csdn.net/zfjBIT/article/details/85124510
今日推荐