如何为C函数构建mex-file matlab [英] How can I build mex-file matlab for C function

查看:79
本文介绍了如何为C函数构建mex-file matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数的输入由matlab代码计算。如何使用matlab代码加入此函数





this function's input calculate by matlab code . how can I join this function with matlab code


void  Foreground_SCGMM_movcam::graphcut_fg_decission(ImageRGB & in, ImageGray & out,
                Mat_2D_float64 & Prob_fg, Mat_2D_float64 & Prob_bg, float64 lambda)
{
    uint64 WIDTH = in.cols;
    uint64 HEIGHT = in.rows;

    Graph::node_id *pnodes = new Graph::node_id[WIDTH*HEIGHT];


    Graph *g = new Graph();

    uint64 x,y;

    Graph::node_id var;
    Graph::node_id qvar;

    Graph::captype E0,E1;

    uint64 iPixel = 0;
    uint64 qPixel =0;


//      cv::Point  p,q;

    array<int32,2> p,q;

    uint64  k;

//      uint8 * pR = in(0).data();
//      uint8 * pG = in(1).data();
//      uint8 * pB = in(2).data();


//      uint8 * pR = (uint8*)in.data +  in.channels()*in.cols*2;
//      uint8 * pG = (uint8*)in.data +  in.channels()*in.cols*1;
//      uint8 * pB = (uint8*)in.data +  in.channels()*in.cols*0;

    uint8 pR;
    uint8 pG;
    uint8 pB;


    float64 var_intensity=0;
    float64 distance_pq=0;
    float64 expon, V_pq;

    uint8 qR, qG, qB;

    for(y=0;y< HEIGHT;y++)
    {
        for(x=0;x<width;x++)>
        {

//                      p = cv::Point (x,y);
            p[0] = x;
            p[1] = y;

            pR=in.data[in.step*(y) + in.channels()* (x) + 2];
            pG=in.data[in.step*(y) + in.channels()* (x) + 1];
            pB=in.data[in.step*(y) + in.channels()* (x) + 0];

            for (k=0; k<neighbor_num;>                        
            {

                q[0] = p[0] + NEIGHBORS[k][0];
                q[1] = p[1] + NEIGHBORS[k][1];

                if ((q[0] >= 0) && (q[1] >=0) && (q[0] < (int64)WIDTH)
                                && (q[1] < (int64)HEIGHT) )
                {
                    qR=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 2];
                    qG=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 1];
                    qB=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 0];



//                                      qR=in(RED_CHANNEL)[q.x][q.y];
//                                      qG=in(GREEN_CHANNEL,q);
//                                      qB=in(BLUE_CHANNEL,q);

                    distance_pq=(pR-qR)*(pR-qR) + (pG-qG)*(pG-qG) + (pB-qB)*(pB-qB);

                }

            }

            var_intensity += distance_pq/NEIGHBOR_NUM;

//                      pR++;
//                      pG++;
//                      pB++;
//                      cont++;

        }
    }

    var_intensity= var_intensity/(HEIGHT*WIDTH);
    printf("\n var_intensity:%E \n", var_intensity);

//      pR = in(0).data();
//      pG = in(1).data();
//      pB = in(2).data();

//      pR = (float64*)in.data + in.cols*0;
//      pG = (float64*)in.data + in.cols*1;
//      pB = (float64*)in.data + in.cols*2;



    distance_pq=0;

    //BUILD GRAPH NODE PER PIXEL
    for(y=0;y< HEIGHT;y++)
        for(x=0;x<width;x++)>
        {
                pnodes[iPixel]= g -> add_node();
                iPixel++;
        }

    iPixel =0;
    for(y=0;y<height;y++)>
    {
        float64* ptr_Prob_bg = (float64*)Prob_bg.data + Prob_bg.cols*uint32(y);
        float64* ptr_Prob_fg = (float64*)Prob_fg.data + Prob_fg.cols*uint32(y);

        for(x=0;x<width;x++)>
        {
                /*data term*/
//                      p =    cv::Point (x,y);
                p[0] = x;
                p[1] = y;

                pR=in.data[in.step*(y) + in.channels()* (x) + 2];
                pG=in.data[in.step*(y) + in.channels()* (x) + 1];
                pB=in.data[in.step*(y) + in.channels()* (x) + 0];

                var = pnodes[iPixel];

//                      if(in(0)[x][y] == 0)  // IF PIXEL IS BACKGROUND  /// NO IF si probabilitats
                if(pR == 0)  // IF PIXEL IS BACKGROUND  /// NO IF si probabilitats
                {
                        E0=0;
                        E1=1;
                }
                else
                {
                        E0=1;
                        E1=0;
                }



//                      /*E0 = E0;*/ E0 = (-1)* log(Prob_bg[x][y]);// (1- Probabilitat_Foreground);  //// logaritme foreground
//                      /*E1 = E1;*/  E1 = (-1)* log(Prob_fg[x][y]); // E1 = Probabilitat_Foreground;   //// logaritme background

                    /*E0 = E0;*/ E0 = (-1)* log(ptr_Prob_bg[Prob_bg.channels()*x]);// (1- Probabilitat_Foreground);  //// logaritme foreground
                    /*E1 = E1;*/ E1 = (-1)* log(ptr_Prob_fg[Prob_fg.channels()*x]); // E1 = Probabilitat_Foreground;   //// logaritme background

                    g->add_tweights(var,E0,E1);


                    if(_lambda != 0.0){
                            //              /*smoothness term*/
                            for (k=0; k<neighbor_num;>                                {
                                    //       q = p + NEIGHBORS[k];
                                    q[0] = p[0]+ NEIGHBORS[k][0];
                                    q[1] = p[1] + NEIGHBORS[k][1];



                                    if ((q[0] >= 0) && (q[1] >=0) && (q[0] < (int64)WIDTH)
                                                    && (q[1] < (int64)HEIGHT) )
                                    {
                                            qPixel = q[0] + (q[1]*WIDTH);
                                            qvar = pnodes[qPixel];

                                            // Calcular V(p,q)

                                            qR=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 2];
                                            qG=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 1];
                                            qB=in.data[in.step*(q[1]) + in.channels()* (q[0]) + 0];

//                                              qR=in(RED_CHANNEL)[q.x][q.y];
//                                              qG=in(GREEN_CHANNEL,q);
//                                              qB=in(BLUE_CHANNEL,q);
//                                              distance_pq=(*pR-qR)*(*pR-qR) + (*pG-qG)*(*pG-qG) + (*pB-qB)*(*pB-qB);
                                            distance_pq=(pR-qR)*(pR-qR) + (pG-qG)*(pG-qG) + (pB-qB)*(pB-qB);

                                            expon = ((-1)* distance_pq)/(2*var_intensity);

                                            V_pq= (1/(std::sqrt(distance_pq)))*exp(expon);

                                            g->add_edge(var,qvar,_lambda* V_pq,_lambda* V_pq);
                                    }
                            }
                    }
                    iPixel++;
                    pR++;
                    pG++;
                    pB++;
            }
    }

    /*minimize energy*/

    g->Graph::maxflow();

    /*set voxel labels*/
    //printf("labelling .....\n");
    int64 label;
    uint8 ucl;
    iPixel = 0;

    for(y=0;y<height;y++)>
    {
            for(x=0;x<width;x++)>
            {
                    label= g->Graph::what_segment(pnodes[iPixel]);

                    if(label == 0){
                            ucl = 255;
                    }
                    else if(label == 1){
                            ucl = 0;
                    }
                    else
                    {
                            ucl=0;
                    }

//                      out(0).data()[iPixel] = ucl;
                    out.data[iPixel] = ucl;


                    iPixel++;
            }
    }

//      cv::imshow( "Out mask", out );
//      cv::waitKey(0);

    delete g;
    delete[](pnodes);
}

推荐答案

如果您将问题输入搜索引擎(如Bing),您将获得很多(> 170万次点击。

Bing的第一个是:



从C / C ++或Fortran源代码构建MEX函数......

这个MATLAB函数编译并链接一个或多个C ,C ++,...通过将值-Wall附加到现有编译器标志来构建yprime.c MEX文件。



If you type your question into a search engine (like Bing) you'll get lots (>1.7 million) of hits.
The first one in Bing is:

Build MEX-function from C/C++ or Fortran source code ...
This MATLAB function compiles and links one or more C, C++, ... Build the yprime.c MEX-file by appending the value -Wall to the existing compiler flag.

.

请参考文档齐全的Matlab-API:



http://www.mathworks.de/de/help/matlab/write-cc-mex-files.html [ ^ ]
Please refer to the very well documented Matlab-API:

http://www.mathworks.de/de/help/matlab/write-cc-mex-files.html[^]


这篇关于如何为C函数构建mex-file matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆