1. 程式人生 > >Opencv基礎: Mat類裡setTo函式詳解

Opencv基礎: Mat類裡setTo函式詳解

https://blog.csdn.net/oMoDao1/article/details/80324360

函式原型:

  /** @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;

測試及驗證:

1、mask元素全為0時

    Mat src(3, 3, CV_8UC1);
    Mat mask(3, 3, CV_8UC1, Scalar(0));
    src.setTo(100, mask);
    cout << src << endl; 

輸出結果:

[  0,   0,   0;
   0,   0,   0;
   0,   0,   0]

2、mask元素全為非0時

Mat src(3, 3, CV_8UC1);
    Mat mask(3, 3, CV_8UC1, Scalar(5));
    src.setTo(100, mask);
    cout << src << endl;

輸出結果:

[100, 100, 100;
 100, 100, 100;
 100, 100, 100]
--------------------- 
作者:LoveWeeknd 
來源:CSDN 
原文:https://blog.csdn.net/oMoDao1/article/details/80324360?utm_source=copy 
版權宣告:本文為博主原創文章,轉載請附上博文連結!