//------------------------------------------------------------------------------------------------ // multi-grid belie propagation //------------------------------------------------------------------------------------------------ void BPFlow::generateCoarserLevel(BPFlow &bp) { //------------------------------------------------------------------------------------------------ // set the dimensions and parameters //------------------------------------------------------------------------------------------------ bp.Width=Width/2; if(Width%2==1) bp.Width++; bp.Height=Height/2; if(Height%2==1) bp.Height++; bp.Area=bp.Width*bp.Height; bp.s=s; bp.d=d; DImage foo; Im_s.smoothing(foo); foo.imresize(bp.Im_s,bp.Width,bp.Height); Im_d.smoothing(foo); foo.imresize(bp.Im_d,bp.Width,bp.Height); bp.IsDisplay=IsDisplay; bp.nNeighbors=nNeighbors; //------------------------------------------------------------------------------------------------ // allocate buffers //------------------------------------------------------------------------------------------------ for(int i=0;i<2;i++) { bp.pOffset[i]=new int[bp.Area]; bp.pWinSize[i]=new int[bp.Area]; ReduceImage(bp.pOffset[i],Width,Height,pOffset[i]); ReduceImage(bp.pWinSize[i],Width,Height,pWinSize[i]); for(int j = 0;j<bp.Area;j++) bp.pWinSize[i][j] = __max(bp.pWinSize[i][j],1); } //------------------------------------------------------------------------------------------------ // generate data term //------------------------------------------------------------------------------------------------ bp.nTotalMatches=bp.AllocateBuffer(bp.pDataTerm,bp.ptrDataTerm,bp.pWinSize[0],bp.pWinSize[1]); for(int i=0;i<bp.Height;i++) for(int j=0;j<bp.Width;j++) { int offset=i*bp.Width+j; for(int ii=0;ii<2;ii++) for(int jj=0;jj<2;jj++) { int y=i*2+ii; int x=j*2+jj; if(y<Height && x<Width) { int nStates=(bp.pWinSize[0][offset]*2+1)*(bp.pWinSize[1][offset]*2+1); for(int k=0;k<nStates;k++) bp.pDataTerm[offset].data()[k]+=pDataTerm[y*Width+x].data()[k]; } } } //------------------------------------------------------------------------------------------------ // generate range term //------------------------------------------------------------------------------------------------ bp.ComputeRangeTerm(gamma/2); }
//------------------------------------------------------------------------------------------------ // multi-grid belief propagation //------------------------------------------------------------------------------------------------ void BPFlow::generateCoarserLevel(BPFlow &bp) { //------------------------------------------------------------------------------------------------ // set the dimensions and parameters //------------------------------------------------------------------------------------------------ bp.Width = Width / 2; if(Width % 2 == 1) bp.Width++; bp.Height = Height / 2; if(Height % 2 == 1) bp.Height++; bp.WinSize = WinSize; bp.Area = bp.Width * bp.Height; bp.s = s; bp.d = d; // DImage foo; // Im_s.smoothing(foo); // foo.imresize(bp.Im_s,bp.Width,bp.Height); // Im_d.smoothing(foo); // foo.imresize(bp.Im_d,bp.Width,bp.Height); bp.IsDisplay = IsDisplay; bp.nNeighbors = nNeighbors; bp.setPenalty(4 * alphaD, alphaV); //------------------------------------------------------------------------------------------------ // allocate buffers //------------------------------------------------------------------------------------------------ for(int i = 0; i < 2; i++) { bp.pOffset[i] = new int[bp.Area]; ReduceImage(bp.pOffset[i], Width, Height, pOffset[i]); } // generate mask bp.pMask1 = new bool[bp.Area]; bp.pMask2 = new bool[bp.Area]; memset(bp.pMask1, 0, sizeof(bool) * bp.Area); memset(bp.pMask2, 0, sizeof(bool) * bp.Area); int sum1 = 0; int sum2 = 0; for (int i = 0; i < bp.Width; i++) { for (int j = 0; j < bp.Height; j++) { int offset = i + j * bp.Width; sum1 = 0; sum2 = 0; for(int ii = 0; ii < 2; ii++) for(int jj = 0; jj < 2; jj++) { int x = j * 2 + jj; int y = i * 2 + ii; if(y < Height && x < Width) { int index = x + y * Width; if (pMask1[index]) sum1++; if (pMask2[index]) sum2++; } } if (sum1 > 0) bp.pMask1[offset] = true; if (sum2 > 0) bp.pMask2[offset] = true; } } // allocate buffer for(int i=0;i<2;i++) { bp.pOffset[i] = new int[bp.Area]; ReduceImage(bp.pOffset[i], Width, Height, pOffset[i]); } //------------------------------------------------------------------------------------------------ // generate data term //------------------------------------------------------------------------------------------------ bp.nTotalMatches = bp.AllocateBuffer(bp.pDataTerm, bp.ptrDataTerm, bp.WinSize); int nStates = (bp.WinSize * 2 + 1) * (bp.WinSize * 2 + 1); for(int i = 0; i < bp.Height; i++) for(int j = 0; j < bp.Width; j++) { int offset = i * bp.Width + j; for(int ii = 0; ii < 2; ii++) { for(int jj = 0; jj < 2; jj++) { int y = i * 2 + ii; int x = j * 2 + jj; if(y < Height && x < Width) { for(int k = 0; k < nStates; k++) bp.pDataTerm[offset].data()[k] += pDataTerm[y * Width + x].data()[k]; } } } } //------------------------------------------------------------------------------------------------ // generate range term //------------------------------------------------------------------------------------------------ bp.ComputeRangeTerm(gamma / 2); }