opencv中的mask參數



轉自:http://wenku.baidu.com/link?url=C9qCWSWqWp-6M1O_Nm351XGBHT574bcopb0UyzUjofEIHVrfU--FF_aMOos4YauOQcAPsDkDz8qtvPYxWLmmNK2ZMID2rFlJxKHwtGKlbMa


其實opencv 裏面很多函數都是會帶有一個mask 參數的,很多同學都不知道它到底有什麼用,好像在實際運用中忽略它也沒有什麼問題  我在這裏就拋磚引玉,詳細分析一個常用函數cvcopy裏面的mask ,希望可以給大家一點點指引。 
以下內容來子opencv安裝文件夾中自帶的pdf文檔。  Copies one array to another.  //複製一個數組到另外一個數組 
void cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL);  src The source array  //源數組,要複製誰? 
// opencv裏面提到的數組不是通常意義上的數組,它是矩陣、圖像等結構體……
dst The destination array 
//目標數組,複製以後的內容給誰?  mask 
——下面這就是重點。鄙人認爲很多人都沒有深刻理解這個mask的作用 
Operation mask, 8-bit single channel array; specifies elements of the destination array to be changed 
//掩碼操作,mask是一個8位單通道的數組;mask指定了目標數組(dst)中那些元素是可以改變的
上面這句話還不是非常重點,重點是以下的那個公式,這個公式有多少人理解哈,就網上的資料來看很少有人理解,又或者高手都不屑於網上寫東西,所以我們只能做等真相……孩子,你被mask mask住了嗎 (第一個mask是opencv裏面的mask數組,第二個mask是這個英文詞本身的意思,不懂就google去)??真相來了!!! 


  wk_ad_begin({pid : 21});wk_ad_after(21, function(){$('.ad-hidden').hide();}, function(){$('.ad-hidden').show();});   
 


 
The function copies selected elements from an input array to an output array:
dst(I) = src(I) if mask(I) = 0. 
//該函數把輸入數組(src數組)中選中的元素(可以認爲是做了標記的,不過這些標誌是誰來做的呢??對,就是mask)拷貝到dst數組 dst(I) = src(I) if mask(I) != 0. 
就是說,如果mask不是NULL,也就是說mask是一個數組,並且是一個和dst or src大小完全一致的數組。  這時遍歷src的每一個元素, 
(1)在位置i時候如果mask對應的值爲不爲0,那麼把src (i) 的值複製給dst (i) 。 
(2)如果mask(i) 爲0,那麼不管src(i)是什麼,dst(i)都設置爲0. 
舉例說明,這裏不舉太複雜的就來一個一維的就夠啦。
 
 
If any of the passed arrays is of IplImage type, then its ROI and COI fields are used. Both
arrays must have the same type, the same number of dimensions, and the same size. The function
can also copy sparse arrays (mask is not supported in this case).  //如果傳遞給src的數組是圖像類型的,那麼將使用ROI或者COI。src數組和dst數組必須具有相同的數據類型、一致的數組維數、一樣


 


的大小。該函數也可以用於拷貝稀疏矩陣(但是此種情況下,mask不起作用)。 
好吧,到這裏了終於寫完了。你現在是不是明白爲什麼要起mask這個名字了吧,掩碼就是:不爲0的時候就可一操作(具體是什麼操作就看具體的函數了)、爲0就掩蓋住了無法操作。 
補充一點 :mask = NULL  意思就是沒有模板、不使用掩碼操作,函數該幹啥就幹啥…… 來吧,再來一個例子 : 
1.
   #include "cv.h"  
2. #include "cxcore.h"   3. #include "highgui.h"   4. #include <stdio.h>   5. int main()   6. {  
7.    float arr1[] = {1,2,3,4,5,6,};  
8.    float arr2[] = {-4,78,12,154,786,-0.154};   9.    float arr3[6] = {119}    10.    int mask_arr[6] = {1};   11.    CvMat src1;  
12.    cvInitMatHeader(&src1,2,3,CV_32FC1,arr1,CV_AUTOSTEP);   13.    CvMat src2   
14.    cvInitMatHeader(&src2,2,3,CV_32FC1,arr2,CV_AUTOSTEP);   15.    CvMat dst   
16.    cvInitMatHeader(&dst,2,3,CV_32FC1,arr3,CV_AUTOSTEP);   17.    CvMat *mask = cvCreateMat( 2,3,CV_32FC1 );  
18.    cvInitMatHeader(mask,2,3,CV_8UC1,mask_arr,CV_AUTOSTEP);   19.    cvAdd(&src1, &src2, &dst, mask);   20.    int x    21.    int y   
22.    for( y = 0  y < 2;++y )   23.    {  
24.       float* ptr = (float*) ( dst.data.ptr + y * dst.step )   


 


25.       for ( x = 0; x < 3  x++ )   26.       {  
27.          printf("/t%f",ptr[x]);   28.       }  
29.       printf("/n");   30.    }  
31.    printf("/n");   32.    return 0    33. }   
結果如下:
-3.000000 0.000000 0.000000 0.000000 0.000000 0.000000

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章