2014-02-13 47 views
2

分配給Triclops結構,我有以下的C++ typedef結構我工作:如何圖像值從MATLAB

typedef struct TriclopsColorImage 
{ 
// The number of rows in the image. 
int    nrows; 
// The number of columns in the image. 
int    ncols; 
// The row increment of the image. 
int    rowinc; 
// The pixel data for red band of the image. 
    unsigned char* red; 
// The pixel data for green band of the image. 
    unsigned char* green; 
// The pixel data for blue band of the image. 
    unsigned char* blue; 

} TriclopsColorImage; 

我有一個形象至極被傳遞到mex功能prhs[1] 我如何正確分配紅色,綠色和藍色的字段。在這裏,我有

TriclopsColorImage colorImage; 

而且我在做這樣的事情:

colorImage.nrows=(int) mxGetN(prhs[1]); 
    colorImage.ncols=(int) mxGetM(prhs[1]); 
    colorImage.rowinc=colorImage.ncols*2; 
    colorImage.red=? 
    colorImage.green=? 
    colorImage.blue=? 
+0

什麼是「行增量」? – Shai

+0

Rowinc表示根據前面示例中的triclops庫 – valentin

+1

的描述,每行的起始位置和下一行的起始位置之間的字節數,每個像素每個列方向元素使用16位或2個字節,但在您的回答,因爲源是unit8,每行的字節數是colorImage.ncols – valentin

回答

0

您必須將圖像傳遞到您的MEX函數作爲uint8型尺寸m -by- n -by-3。
例如:

img = imread('football.jpg'); 
myMeFunction(someArg, img); % note that prhs[1] is the SECOND argument 

現在您的MEX內:

colorImage.nrows=(int) mxGetM(prhs[1]); // M is number of rows! 
colorImage.ncols=(int) mxGetN(prhs[1])/3; // assuming third dimension is three. 
// for the colors: 
unsigned char* p = (unsigned char*)mxGetData(prhs[1]);  
colorImage.red = p; 
colorImage.green = p + colorImage.nrows*colorImage.ncols; 
colorImage.blue = p + 2*colorImage.nrows*colorImage.ncols; 

請仔細閱讀仔細mxGetN描述。