2015-11-20 38 views
1

一般我使用以下函數來爲3頻帶和單色圖像如何在Mex網關功能中爲3波段或單色圖像分配內存?

/* For 3 band image */  
plhs[0] = mxCreateNumericArray(3, dim_array, mxDOUBLE_CLASS, mxREAL); 

/*For monochrome image */ 
plhs[0] = mxCreateDoubleMatrix(r,c,mxREAL); 

,將是這種情況時,我們知道行(r)的值,列(c)和dim_array分配內存。如果我們不知道r,c和dim_array的價值怎麼辦?這聽起來很愚蠢......但我想要做的是我想從文件位置讀取圖像。我的Matlab的功能會像

outputImage = imageRead('C:\abc\def\ghi.bmp'); 

我只是路過字符串作爲輸入我不能讓R,從輸入c和dim_array的價值,但我們必須在主網關輸出圖像分配內存功能。我們怎樣才能爲這個輸出圖像分配內存?

+0

從Matlab,你的函數將如何被調用?例如。 'result = myMexFunction(outputImage)'其中outputImage是imageRead的結果,result是一個與outputImage具有相同尺寸的雙數組? –

+0

或者,您的mex函數的目標是從文件中讀取圖像本身,並返回一個表示圖像的三維數組返回給MATLAB? 'outputImage = myMexFunction(path)';我不完全按照你要求的... –

+0

是的,在matlab中,該函數將被稱爲outputImage = myMexFunction(path)。在我的情況下,mymexfunction是imageRead。你明白了,我想從文件中讀取圖像本身,並返回代表圖像回到MATLAB的三維數組。並且結果應該與輸出圖像的尺寸相同。 –

回答

1

至少在R2015b,這是我認爲你這樣做。你可能需要做的第一次穿過你的文件,以確定n_rowsn_cols等等

mwSize dims[3];  // for the dimensions of your numeric array 
dims[0] = n_rows; 
dims[1] = n_cols; 
dims[2] = n_depth; // 3 in case of 3-band, 1 in the case of monochrome 

// call mxCreateNumeric to construct your array. 
// as I understand mex works in 2015b, this should allocate memory for your array 
mxArray *my_array = mxCreateNumericArray(3, dims, mxDOUBLE_CLASS, mxREAL); 

// get a pointer to your array. Requires a cast to (double *) 
double *array_data = (double *) mxGetData(my_array); 

記住在MATLAB矩陣列爲主!也就是說,my_array(i,j,k)項將被訪問array_data[i + j*n_rows + k * (n_rows*n_cols)]