| 115 | |
| 116 | |
| 117 | void maxflow_inference(unsigned char * label, const float* img, const float * prob, const unsigned char * seed, |
| 118 | int H, int W, int chns, int cls, float lambda, float sigma) |
| 119 | { |
| 120 | // currently, only cls == 2 is supported |
| 121 | typedef Graph<float, float, float> GraphType; |
| 122 | /*estimated # of nodes*/ /*estimated # of edges*/ |
| 123 | GraphType * g = new GraphType(H*W, 2*H*W); |
| 124 | g->add_node(H*W); |
| 125 | float max_weight = -100000; |
| 126 | for(int x=0; x<H;x++) |
| 127 | { |
| 128 | for(int y=0; y<W;y++) |
| 129 | { |
| 130 | std::vector<float> pValue = get_pixel_vector(img, H, W, chns, x, y); |
| 131 | std::vector<float> qValue; |
| 132 | float l2dis, n_weight; |
| 133 | int pIndex = x*W + y; |
| 134 | int qIndex; |
| 135 | int xn, yn; |
| 136 | int Xoff[2] = {-1, 0}; |
| 137 | int Yoff[2] = {0, -1}; |
| 138 | for(int i=0; i<2; i++){ |
| 139 | xn = x + Xoff[i]; |
| 140 | yn = y + Yoff[i]; |
| 141 | if(xn < 0 || yn < 0) continue; |
| 142 | qValue = get_pixel_vector(img, H, W, chns, xn, yn); |
| 143 | l2dis = get_l2_distance(pValue, qValue); |
| 144 | n_weight = lambda*exp(-l2dis * l2dis/(2*sigma*sigma)); |
| 145 | qIndex = xn*W + yn; |
| 146 | g->add_edge(qIndex,pIndex,n_weight,n_weight); |
| 147 | if(n_weight > max_weight) max_weight = n_weight; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | max_weight = 1000 * max_weight; |
| 153 | for(int x=0;x<H;x++) |
| 154 | { |
| 155 | for(int y=0;y<W;y++) |
| 156 | { |
| 157 | bool seed_exist = false; |
| 158 | float s_weight = 1e-3; |
| 159 | float t_weight = 1e-3; |
| 160 | if(seed != NULL) |
| 161 | { |
| 162 | std::vector<unsigned char> label = get_pixel_vector(seed, H, W, 2, x, y); |
| 163 | if(label[0] > 0){ |
| 164 | t_weight = max_weight; |
| 165 | seed_exist = true; |
| 166 | } |
| 167 | else if(label[1] > 0){ |
| 168 | s_weight = max_weight; |
| 169 | seed_exist = true; |
| 170 | } |
| 171 | } |
| 172 | if(!seed_exist){ |
| 173 | std::vector<float> probs = get_pixel_vector(prob, H, W, 2, x, y); |
| 174 | s_weight = -log(probs[0]); |
no test coverage detected