Create uint8/char/uchar buffer for storing the structure into matlab memory

  1. 将Matlab Struct 转换为 C++ Struct,然后以二进制的形式存储在.dat文 件中。
  2. 读取二进制形式的.dat文件,存储在C++ Struct中,并将其转换为uchar Buffer存储在matlab memory中。
  3. 读取Matlab传递过来的Buffer,并将其转换为Struct,来使用。

main.m

%2016.7.14 
%@author xlh
clc;clearvars;close all;
%% 创建模型数据
img=imread('lena.jpg');
grayImg=rgb2gray(img);

resImg=reshape(grayImg,[size(grayImg,1)*size(grayImg,2) 1]);

%% 对图像进行的操作
options.reSize=[40 40];

model.imgs=resImg;
model.imgName='lena.jpg';
model.imgSize=size(grayImg);
model.nImgs=1;
model.options=options;

%% 将options转换为二进制文件存储在.dat中
fileName='options.dat';
convertStruct2Binary(fileName,model);
disp('create binary file :options.data ,over .....');
%%.dat-struct Model-uchar
res_model=readBinAndConv2Uchar(fileName);

%% 使用res_model
useModelToCompute(res_model);

convertStruct2Binary.cpp

#include "mex.h"
#include<string.h>
typedef unsigned char  uchar ;

typedef struct options
{
    int reSize[2];
}Options;

typedef struct model
{
    uchar *imgs;
    char *imgName;
    int imgSize[2];
    int nImg;
    //将Options op;改成为Options *op;
    Options op;
}Model;

void writeModel2Binary(char * fileName,Model * pModel)
{
    int imgSize[2];
    imgSize[0]=pModel->imgSize[0];
    imgSize[1]=pModel->imgSize[1];
    FILE  *fout=fopen(fileName,"wb");//以二进制的形式
    //for imgs
    //fwrite vs fread;fprintf vs fscan
    fprintf(fout," %d %d ",pModel->imgSize[0],pModel->imgSize[1]);
    fprintf(fout," %d ",pModel->nImg);
    fwrite(pModel->imgs,imgSize[0]*imgSize[1]*sizeof(uchar),1,fout);
    //mexPrintf("length of imgName:%d\n",strlen(pModel->imgName));
    //when we read imgName,but we can't know the lengh of it,so wo save the length to file
    //for strlen(fileName)
    fprintf(fout," %d ",strlen(pModel->imgName));
    fwrite(pModel->imgName,strlen(pModel->imgName)*sizeof(char),1,fout);
    //for reSize
    int reSize[2];
    //将op.reSize 改成op->reSize
    reSize[0]=pModel->op.reSize[0];
    reSize[1]=pModel->op.reSize[1];
    fprintf(fout," %d %d ",reSize[0],reSize[1]);
    fclose(fout);
}


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    //检测是否有两个输入参数
    if(nrhs!=2)
        mexErrMsgTxt("请输2个参数");
    char *fileName=mxArrayToString(prhs[0]);
    mxArray * ptrImg=mxGetField(prhs[1],0,"imgs");
    const mwSize * dims=mxGetDimensions(ptrImg);
    mxArray *ptrImgName=mxGetField(prhs[1],0,"imgName");
    mxArray *ptrImgSize=mxGetField(prhs[1],0,"imgSize");
    mxArray *ptrnImgs=mxGetField(prhs[1],0,"nImgs");
    mxArray *ptrOptions=mxGetField(prhs[1],0,"options");
    mxArray *ptrReSize=mxGetField(ptrOptions,0,"reSize");
    // 为Model 分配空间
    Model *pModel=(Model *)malloc(sizeof(Model));
    //for imgs;
    //alloc memory 
    //***********************************************
    //pModel->imgs=(uchar*)malloc(dims[0]*dims[1]*sizeof(uchar));
    //uchar *tpImgs=(uchar*)mxGetPr(ptrImg);
    //for(int i=0;i<dims[0]*dims[1];i++)
        //pModel->imgs[i]=tpImgs[i];
    //************************************************
    pModel->imgs=(uchar*)mxGetPr(ptrImg);
    //for imgName;
    char* imgName=mxArrayToString(ptrImgName);
    pModel->imgName=imgName;
    //printf name
    mexPrintf("imgName:%s\n",pModel->imgName);
    double *pTemp;
    pTemp=mxGetPr(ptrImgSize);
    pModel->imgSize[0]=(int)*pTemp;
    pModel->imgSize[1]=(int)*(pTemp+1);
    mexPrintf("imgSize:%d,%d\n",pModel->imgSize[0],pModel->imgSize[1]);
    //for nImg
    pModel->nImg=(int)mxGetScalar(ptrnImgs);
    //for options
    pTemp=mxGetPr(ptrReSize);
    //将op.reSize改成op->reSize,并将pModel->op->reSize加上()
    pModel->op.reSize[0]=(int)*pTemp;
    pModel->op.reSize[1]=(int)*(pTemp+1);

    // call function
    writeModel2Binary(fileName,pModel);

    //cout: not use
    plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
    double* pRes = mxGetPr(plhs[0]);
    *pRes=10;
    return;
}

readBinAndConv2Uchar.cpp

#include"mex.h"
#include<string.h>
typedef unsigned char  uchar ;

typedef struct options
{
    int reSize[2];
}Options;

typedef struct model
{
    uchar *imgs;
    char *imgName;
    int imgSize[2];
    int nImg;
    //将Options op;改成为Options *op;
    Options op;
}Model;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if(nrhs!=1)
    {
        mexPrintf("请输入一个参数");
        return;
    }

    char *fileName=mxArrayToString(prhs[0]);
    FILE *fin=fopen(fileName,"rb");
    //for imgs;
    Model *pModel=(Model *)malloc(sizeof(Model));
    fscanf(fin," %d %d ",&pModel->imgSize[0],&pModel->imgSize[1]);
    fscanf(fin," %d ",&pModel->nImg);
    //fread(start,len,1,fin):start address,
    //alloc for imgs
    int rows,cols;
    rows=pModel->imgSize[0];
    cols=pModel->imgSize[1];
    pModel->imgs=(uchar*)malloc(rows*cols*sizeof(uchar));
    fread(pModel->imgs,rows*cols*sizeof(uchar),1,fin);
    //for imgName
    int tlen;
    fscanf(fin," %d ",&tlen);
    //alloc for imgName;
    pModel->imgName=(char*)malloc((tlen+1)*sizeof(char));
    fread(pModel->imgName,tlen*sizeof(char),1,fin);
    pModel->imgName[tlen]='\0';
    //for reSize
    fscanf(fin," %d %d ",&pModel->op.reSize[0],&pModel->op.reSize[1]);
    fclose(fin);
    //check the result;
    //mexPrintf("checking data....\n");
    //mexPrintf("imgSize: %d*%d\n",pModel->imgSize[0],pModel->imgSize[1]);
    //mexPrintf("imgName:%s\n",pModel->imgName);
    //mexPrintf("lenName:\n",strlen(pModel->imgName));
    //const int N=5;
    //for(int i=0;i<N;i++)
    //{
    //  for(int j=0;j<N;j++)
    //  {
    //      mexPrintf("%d ",pModel->imgs[j*pModel->imgSize[0]+i]);
    //  }
    //  mexPrintf("\n");
    //}
    //
    //computer size of the Model
    int model_size=0;
    //for static
    model_size+=sizeof(Model);
    //for dynamic
    model_size+=pModel->imgSize[0]*pModel->imgSize[1]*sizeof(uchar);
    int len=strlen(pModel->imgName);
    model_size+=(len+1)*sizeof(uchar);
    //Create uint8 buffer for storing the structure into matlab memory
    plhs[0] = mxCreateNumericMatrix(model_size, 1, mxUINT8_CLASS, mxREAL);
    uchar *buffer = (uchar*)mxGetPr(plhs[0]);
    //for static
    int address_offset;
    address_offset=0;
    memcpy(&buffer[address_offset],pModel,sizeof(Model));
    //for options
    //address_offset=(uchar*)&(pModel->op)-(uchar*)pModel;
    //memcpy(&buffer[address_offset],&pModel->op,sizeof(Options));
    //for dynamic
    // address_offset+=sizeof(Model);
    //for imgs;
    // memcpy(&buffer[address_offset],pModel->imgs,pModel->imgSize[0]*pModel->imgSize[1]*sizeof(uchar));
    // &pModel->imgs,表示取出指针对应的地址。
    // *(uchar**)&buffer[(uchar*)&pModel->imgs-(uchar*)pModel]=(uchar*)&buffer[address_offset];
    //for imgNames;
    //address_offset+=pModel->imgSize[0]*pModel->imgSize[1]*sizeof(uchar);
    //memcpy(&buffer[address_offset],pModel->imgName,(len+1)*sizeof(uchar));
    // *(char**)&buffer[(uchar*)&pModel->imgName-(uchar*)pModel]=(char*)&buffer[address_offset];
    return;
}

useModelToCompute.cpp

#include"mex.h"
#include<string.h>
typedef unsigned char  uchar ;

typedef struct options
{
    int reSize[2];
}Options;

typedef struct model
{
    uchar *imgs;
    char *imgName;
    int imgSize[2];
    int nImg;
    Options op;
}Model;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    if(nrhs!=1)

    {
        mexPrintf("请输出一个参数");
        return ;
    }
    Model *pModel=(Model*)mxGetPr(prhs[0]);
    //for nImg
    mexPrintf("nImgs:%d\n",*(int*)&pModel->nImg);
    //for imgName
    int slen=strlen((char*)pModel->imgName);
    mexPrintf("slen:%d\n",slen);
    mexPrintf("imgName:%s\n",(char*)pModel->imgName);
    //for op.resize
    //Options *op=(Options *)&pModel->op;
    //mexPrintf("reSize:%d,%d\n",*(int*)&op->reSize[0],*(int*)op->reSize[1]);
    mexPrintf("reSize:%d,%d\n",*(int*)&pModel->op.reSize[0],*(int*)&pModel->op.reSize[1]);

    const int N=5;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<N;j++)
        {
            mexPrintf("%d ",pModel->imgs[i*pModel->imgSize[1]+j]);
        }
        mexPrintf("\n");
    }
    return;
}

参考文献:
1、flandmark:http://cmp.felk.cvut.cz/~uricamic/flandmark/
2、测试代码:http://download.youkuaiyun.com/detail/xuluhui123/9578168

转载于:https://www.cnblogs.com/raby/p/5886700.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值