Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this function's input calculate by matlab code . how can I join this function with matlab code


C++
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);
}
Posted
Updated 9-Sep-14 0:18am
v2
Comments
George Jonsson 9-Sep-14 6:20am    
I tried to format your code, but there are so many commented lines so it makes it hard.
If you want people to help you, you should format the code so it is readable for others.
mohamed sabri 9-Sep-14 9:08am    
what's the problem exactly?
is it to erase there commented lines
George Jonsson 9-Sep-14 9:19am    
Yep, and also indent the code properly for easy reading.
mohamed sabri 9-Sep-14 9:33am    
OK I reply this code without commented lines
George Jonsson 9-Sep-14 9:37am    
Don't use reply. Use Improve Question

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.

.
 
Share this answer
 
Please refer to the very well documented Matlab-API:

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900