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); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)