1 //M*//////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
45 #define INFINITY 10000
46 #define OCCLUSION_PENALTY 10000
47 #define OCCLUSION_PENALTY2 1000
48 #define DENOMINATOR 16
50 #define OCCLUDED CV_STEREO_GC_OCCLUDED
52 #define IS_BLOCKED(d1, d2) ((d1) > (d2))
74 typedef struct CvStereoGCState2
76 int Ithreshold, interactionRadius;
77 int lambda, lambda1, lambda2, K;
78 int dataCostFuncTab[CUTOFF+1];
79 int smoothnessR[CUTOFF*2+1];
80 int smoothnessGrayDiff[512];
86 // truncTab[x+255] = MAX(x-255,0)
87 static uchar icvTruncTab[512];
88 // cutoffSqrTab[x] = MIN(x*x, CUTOFF)
89 static int icvCutoffSqrTab[256];
91 static void icvInitStereoConstTabs()
93 static volatile int initialized = 0;
97 for( i = 0; i < 512; i++ )
98 icvTruncTab[i] = (uchar)MIN(MAX(i-255,0),255);
99 for( i = 0; i < 256; i++ )
100 icvCutoffSqrTab[i] = MIN(i*i, CUTOFF);
105 static void icvInitStereoTabs( CvStereoGCState2* state2 )
107 int i, K = state2->K;
109 for( i = 0; i <= CUTOFF; i++ )
110 state2->dataCostFuncTab[i] = MIN(i*DENOMINATOR - K, 0);
112 for( i = 0; i < CUTOFF*2 + 1; i++ )
113 state2->smoothnessR[i] = MIN(abs(i-CUTOFF), state2->interactionRadius);
115 for( i = 0; i < 512; i++ )
117 int diff = abs(i - 255);
118 state2->smoothnessGrayDiff[i] = diff < state2->Ithreshold ? state2->lambda1 : state2->lambda2;
123 static int icvGCResizeOrphansBuf( GCVtx**& orphans, int norphans )
125 int i, newNOrphans = MAX(norphans*3/2, 256);
126 GCVtx** newOrphans = (GCVtx**)cvAlloc( newNOrphans*sizeof(orphans[0]) );
127 for( i = 0; i < norphans; i++ )
128 newOrphans[i] = orphans[i];
130 orphans = newOrphans;
134 static int64 icvGCMaxFlow( GCVtx* vtx, int nvtx, GCEdge* edges, GCVtx**& _orphans, int& _maxOrphans )
136 const int TERMINAL = -1, ORPHAN = -2;
137 GCVtx stub, *nilNode = &stub, *first = nilNode, *last = nilNode;
141 int norphans = 0, maxOrphans = _maxOrphans;
142 GCVtx** orphans = _orphans;
145 // initialize the active queue and the graph vertices
146 for( i = 0; i < nvtx; i++ )
152 last = last->next = v;
154 v->parent = TERMINAL;
155 v->t = v->weight < 0;
162 last->next = nilNode;
165 // run the search-path -> augment-graph -> restore-trees loop
169 int e0 = -1, ei = 0, ej = 0, min_weight, weight;
172 // grow S & T search trees, find an edge connecting them
173 while( first != nilNode )
179 for( ei = v->first; ei != 0; ei = edges[ei].next )
181 if( edges[ei^vt].weight == 0 )
189 u->dist = v->dist + 1;
193 last = last->next = u;
204 if( u->dist > v->dist+1 && u->ts <= v->ts )
206 // reassign the parent
209 u->dist = v->dist + 1;
215 // exclude the vertex from the active list
223 // find the minimum edge weight along the path
224 min_weight = edges[e0].weight;
225 assert( min_weight > 0 );
226 // k = 1: source tree, k = 0: destination tree
227 for( k = 1; k >= 0; k-- )
229 for( v = edges[e0^k].dst;; v = edges[ei].dst )
231 if( (ei = v->parent) < 0 )
233 weight = edges[ei^k].weight;
234 min_weight = MIN(min_weight, weight);
235 assert( min_weight > 0 );
237 weight = abs(v->weight);
238 min_weight = MIN(min_weight, weight);
239 assert( min_weight > 0 );
242 // modify weights of the edges along the path and collect orphans
243 edges[e0].weight -= min_weight;
244 edges[e0^1].weight += min_weight;
247 // k = 1: source tree, k = 0: destination tree
248 for( k = 1; k >= 0; k-- )
250 for( v = edges[e0^k].dst;; v = edges[ei].dst )
252 if( (ei = v->parent) < 0 )
254 edges[ei^(k^1)].weight += min_weight;
255 if( (edges[ei^k].weight -= min_weight) == 0 )
257 if( norphans >= maxOrphans )
258 maxOrphans = icvGCResizeOrphansBuf( orphans, norphans );
259 orphans[norphans++] = v;
264 v->weight = (short)(v->weight + min_weight*(1-k*2));
267 if( norphans >= maxOrphans )
268 maxOrphans = icvGCResizeOrphansBuf( orphans, norphans );
269 orphans[norphans++] = v;
274 // restore the search trees by finding new parents for the orphans
276 while( norphans > 0 )
278 GCVtx* v = orphans[--norphans];
279 int d, min_dist = INT_MAX;
283 for( ei = v->first; ei != 0; ei = edges[ei].next )
285 if( edges[ei^(vt^1)].weight == 0 )
288 if( u->t != vt || u->parent == 0 )
290 // compute the distance to the tree root
293 if( u->ts == curr_ts )
314 // update the distance
322 for( u = edges[ei].dst; u->ts != curr_ts; u = edges[u->parent].dst )
330 if( (v->parent = e0) > 0 )
337 /* no parent is found */
339 for( ei = v->first; ei != 0; ei = edges[ei].next )
343 if( u->t != vt || !ej )
345 if( edges[ei^(vt^1)].weight && !u->next )
348 last = last->next = u;
350 if( ej > 0 && edges[ej].dst == v )
352 if( norphans >= maxOrphans )
353 maxOrphans = icvGCResizeOrphansBuf( orphans, norphans );
354 orphans[norphans++] = u;
362 _maxOrphans = maxOrphans;
368 CvStereoGCState* cvCreateStereoGCState( int numberOfDisparities, int maxIters )
370 CvStereoGCState* state = 0;
372 //CV_FUNCNAME("cvCreateStereoGCState");
376 state = (CvStereoGCState*)cvAlloc( sizeof(*state) );
377 memset( state, 0, sizeof(*state) );
378 state->minDisparity = 0;
379 state->numberOfDisparities = numberOfDisparities;
380 state->maxIters = maxIters <= 0 ? 3 : maxIters;
381 state->Ithreshold = 5;
382 state->interactionRadius = 1;
383 state->K = state->lambda = state->lambda1 = state->lambda2 = -1.f;
384 state->occlusionCost = OCCLUSION_PENALTY;
391 void cvReleaseStereoGCState( CvStereoGCState** _state )
393 CvStereoGCState* state;
395 if( !_state && !*_state )
399 cvReleaseMat( &state->left );
400 cvReleaseMat( &state->right );
401 cvReleaseMat( &state->ptrLeft );
402 cvReleaseMat( &state->ptrRight );
403 cvReleaseMat( &state->vtxBuf );
404 cvReleaseMat( &state->edgeBuf );
408 // ||I(x) - J(x')|| =
412 // max(minJ(x') - I(x), 0),
413 // max(I(x) - maxJ(x'), 0)),
415 // max(minI(x) - J(x'), 0),
416 // max(J(x') - maxI(x), 0)))**2) ==
419 // max(minJ(x') - I(x), 0) +
420 // max(I(x) - maxJ(x'), 0),
422 // max(minI(x) - J(x'), 0) +
423 // max(J(x') - maxI(x), 0)))**2)
424 // where (I, minI, maxI) and
425 // (J, minJ, maxJ) are stored as interleaved 3-channel images.
426 // minI, maxI are computed from I,
427 // minJ, maxJ are computed from J - see icvInitGraySubPix.
428 static inline int icvDataCostFuncGraySubpix( const uchar* a, const uchar* b )
430 int va = a[0], vb = b[0];
431 int da = icvTruncTab[b[1] - va + 255] + icvTruncTab[va - b[2] + 255];
432 int db = icvTruncTab[a[1] - vb + 255] + icvTruncTab[vb - a[2] + 255];
433 return icvCutoffSqrTab[MIN(da,db)];
436 static inline int icvSmoothnessCostFunc( int da, int db, int maxR, const int* stabR, int scale )
438 return da == db ? 0 : (da == OCCLUDED || db == OCCLUDED ? maxR : stabR[da - db])*scale;
441 static void icvInitGraySubpix( const CvMat* left, const CvMat* right,
442 CvMat* left3, CvMat* right3 )
444 int k, x, y, rows = left->rows, cols = left->cols;
446 for( k = 0; k < 2; k++ )
448 const CvMat* src = k == 0 ? left : right;
449 CvMat* dst = k == 0 ? left3 : right3;
450 int sstep = src->step;
452 for( y = 0; y < rows; y++ )
454 const uchar* sptr = src->data.ptr + sstep*y;
455 const uchar* sptr_prev = y > 0 ? sptr - sstep : sptr;
456 const uchar* sptr_next = y < rows-1 ? sptr + sstep : sptr;
457 uchar* dptr = dst->data.ptr + dst->step*y;
458 int v_prev = sptr[0];
460 for( x = 0; x < cols; x++, dptr += 3 )
462 int v = sptr[x], v1, minv = v, maxv = v;
465 minv = MIN(minv, v1); maxv = MAX(maxv, v1);
466 v1 = (v + sptr_prev[x])/2;
467 minv = MIN(minv, v1); maxv = MAX(maxv, v1);
468 v1 = (v + sptr_next[x])/2;
469 minv = MIN(minv, v1); maxv = MAX(maxv, v1);
472 v1 = (v + sptr[x+1])/2;
473 minv = MIN(minv, v1); maxv = MAX(maxv, v1);
477 dptr[1] = (uchar)minv;
478 dptr[2] = (uchar)maxv;
484 // Optimal K is computed as avg_x(k-th-smallest_d(||I(x)-J(x+d)||)),
485 // where k = number_of_disparities*0.25.
487 icvComputeK( CvStereoGCState* state )
489 int x, y, x1, d, i, j, rows = state->left->rows, cols = state->left->cols, n = 0;
490 int mind = state->minDisparity, nd = state->numberOfDisparities, maxd = mind + nd;
491 int k = MIN(MAX((nd + 2)/4, 3), nd);
492 int *arr = (int*)cvStackAlloc(k*sizeof(arr[0])), delta, t, sum = 0;
494 for( y = 0; y < rows; y++ )
496 const uchar* lptr = state->left->data.ptr + state->left->step*y;
497 const uchar* rptr = state->right->data.ptr + state->right->step*y;
499 for( x = 0; x < cols; x++ )
501 for( d = maxd-1, i = 0; d >= mind; d-- )
504 if( (unsigned)x1 >= (unsigned)cols )
506 delta = icvDataCostFuncGraySubpix( lptr + x*3, rptr + x1*3 );
510 for( i = 0; i < k; i++ )
512 CV_SWAP( arr[i], delta, t );
515 for( j = 1; j < i; j++ )
516 delta = MAX(delta, arr[j]);
525 static int64 icvComputeEnergy( const CvStereoGCState* state, const CvStereoGCState2* state2,
528 int x, y, rows = state->left->rows, cols = state->left->cols;
530 const int* dtab = state2->dataCostFuncTab;
531 int maxR = state2->interactionRadius;
532 const int* stabR = state2->smoothnessR + CUTOFF;
533 const int* stabI = state2->smoothnessGrayDiff + 255;
534 const uchar* left = state->left->data.ptr;
535 const uchar* right = state->right->data.ptr;
536 short* dleft = state->dispLeft->data.s;
537 short* dright = state->dispRight->data.s;
538 int step = state->left->step;
539 int dstep = (int)(state->dispLeft->step/sizeof(short));
541 assert( state->left->step == state->right->step &&
542 state->dispLeft->step == state->dispRight->step );
545 return (int64)OCCLUSION_PENALTY*rows*cols*2;
547 for( y = 0; y < rows; y++, left += step, right += step, dleft += dstep, dright += dstep )
549 for( x = 0; x < cols; x++ )
551 int d = dleft[x], x1, d1;
553 E += OCCLUSION_PENALTY;
557 if( (unsigned)x1 >= (unsigned)cols )
561 E += dtab[icvDataCostFuncGraySubpix( left + x*3, right + x1*3 )];
567 E += icvSmoothnessCostFunc(d, d1, maxR, stabR, stabI[left[x*3] - left[x*3+3]] );
572 E += icvSmoothnessCostFunc(d, d1, maxR, stabR, stabI[left[x*3] - left[x*3+step]] );
577 E += OCCLUSION_PENALTY;
582 E += icvSmoothnessCostFunc(d, d1, maxR, stabR, stabI[right[x*3] - right[x*3+3]] );
586 d1 = dright[x+dstep];
587 E += icvSmoothnessCostFunc(d, d1, maxR, stabR, stabI[right[x*3] - right[x*3+step]] );
596 static inline void icvAddEdge( GCVtx *x, GCVtx* y, GCEdge* edgeBuf, int nedges, int w, int rw )
598 GCEdge *xy = edgeBuf + nedges, *yx = xy + 1;
600 assert( x != 0 && y != 0 );
603 xy->weight = (short)w;
608 yx->weight = (short)rw;
612 static inline int icvAddTWeights( GCVtx* vtx, int sourceWeight, int sinkWeight )
619 vtx->weight = (short)(sourceWeight - sinkWeight);
620 return MIN(sourceWeight, sinkWeight);
623 static inline int icvAddTerm( GCVtx* x, GCVtx* y, int A, int B, int C, int D,
624 GCEdge* edgeBuf, int& nedges )
628 assert(B - A + C - D >= 0);
631 dE += icvAddTWeights(x, D, B);
632 dE += icvAddTWeights(y, 0, A - B);
633 if( (w = B - A + C - D) != 0 )
635 icvAddEdge( x, y, edgeBuf, nedges, 0, w );
641 dE += icvAddTWeights(x, D, A + D - C);
642 dE += icvAddTWeights(y, 0, C - D);
643 if( (w = B - A + C - D) != 0 )
645 icvAddEdge( x, y, edgeBuf, nedges, w, 0 );
651 dE += icvAddTWeights(x, D, A);
652 if( B != A || C != D )
654 icvAddEdge( x, y, edgeBuf, nedges, B - A, C - D );
661 static int64 icvAlphaExpand( int64 Eprev, int alpha, CvStereoGCState* state, CvStereoGCState2* state2 )
665 int delta, E00=0, E0a=0, Ea0=0, Eaa=0;
666 int k, a, d, d1, x, y, x1, y1, rows = state->left->rows, cols = state->left->cols;
667 int nvtx = 0, nedges = 2;
668 GCVtx* vbuf = (GCVtx*)state->vtxBuf->data.ptr;
669 GCEdge* ebuf = (GCEdge*)state->edgeBuf->data.ptr;
670 int maxR = state2->interactionRadius;
671 const int* dtab = state2->dataCostFuncTab;
672 const int* stabR = state2->smoothnessR + CUTOFF;
673 const int* stabI = state2->smoothnessGrayDiff + 255;
674 const uchar* left0 = state->left->data.ptr;
675 const uchar* right0 = state->right->data.ptr;
676 short* dleft0 = state->dispLeft->data.s;
677 short* dright0 = state->dispRight->data.s;
678 GCVtx** pleft0 = (GCVtx**)state->ptrLeft->data.ptr;
679 GCVtx** pright0 = (GCVtx**)state->ptrRight->data.ptr;
680 int step = state->left->step;
681 int dstep = (int)(state->dispLeft->step/sizeof(short));
682 int pstep = (int)(state->ptrLeft->step/sizeof(GCVtx*));
683 int aa[] = { alpha, -alpha };
685 //double t = (double)cvGetTickCount();
687 assert( state->left->step == state->right->step &&
688 state->dispLeft->step == state->dispRight->step &&
689 state->ptrLeft->step == state->ptrRight->step );
690 for( k = 0; k < 2; k++ )
697 for( y = 0; y < rows; y++ )
699 const uchar* left = left0 + step*y;
700 const uchar* right = right0 + step*y;
701 const short* dleft = dleft0 + dstep*y;
702 const short* dright = dright0 + dstep*y;
703 GCVtx** pleft = pleft0 + pstep*y;
704 GCVtx** pright = pright0 + pstep*y;
705 const uchar* lr[] = { left, right };
706 const short* dlr[] = { dleft, dright };
707 GCVtx** plr[] = { pleft, pright };
709 for( k = 0; k < 2; k++ )
712 for( y1 = y+(y>0); y1 <= y+(y<rows-1); y1++ )
714 const short* disp = (k == 0 ? dleft0 : dright0) + y1*dstep;
715 GCVtx** ptr = (k == 0 ? pleft0 : pright0) + y1*pstep;
716 for( x = 0; x < cols; x++ )
718 GCVtx* v = ptr[x] = &vbuf[nvtx++];
720 v->weight = disp[x] == (short)(OCCLUDED ? -OCCLUSION_PENALTY2 : 0);
725 for( x = 0; x < cols; x++ )
731 // (left + x, right + x + d)
732 if( d != alpha && d != OCCLUDED && (unsigned)x1 < (unsigned)cols )
739 delta = IS_BLOCKED(alpha, d) ? INFINITY : 0;
741 E += icvAddTerm( var, var1,
742 dtab[icvDataCostFuncGraySubpix( left + x*3, right + x1*3 )],
743 delta, delta, 0, ebuf, nedges );
745 else if( IS_BLOCKED(alpha, d) )
746 E += icvAddTerm( var, var1, 0, INFINITY, 0, 0, ebuf, nedges );
749 // (left + x, right + x + alpha)
751 if( (unsigned)x1 < (unsigned)cols )
756 E0a = IS_BLOCKED(d, alpha) ? INFINITY : 0;
757 Ea0 = IS_BLOCKED(-d1, alpha) ? INFINITY : 0;
758 Eaa = dtab[icvDataCostFuncGraySubpix( left + x*3, right + x1*3 )];
759 E += icvAddTerm( var, var1, 0, E0a, Ea0, Eaa, ebuf, nedges );
763 for( k = 0; k < 2; k++ )
766 const short* disp = dlr[k];
767 const uchar* img = lr[k] + x*3;
777 scale = stabI[img[0] - img[3]];
778 E0a = icvSmoothnessCostFunc( d, a, maxR, stabR, scale );
779 Ea0 = icvSmoothnessCostFunc( a, d1, maxR, stabR, scale );
780 E00 = icvSmoothnessCostFunc( d, d1, maxR, stabR, scale );
781 E += icvAddTerm( var, var1, E00, E0a, Ea0, 0, ebuf, nedges );
788 scale = stabI[img[0] - img[step]];
789 E0a = icvSmoothnessCostFunc( d, a, maxR, stabR, scale );
790 Ea0 = icvSmoothnessCostFunc( a, d1, maxR, stabR, scale );
791 E00 = icvSmoothnessCostFunc( d, d1, maxR, stabR, scale );
792 E += icvAddTerm( var, var1, E00, E0a, Ea0, 0, ebuf, nedges );
797 if( d != OCCLUDED && IS_BLOCKED(alpha, -d))
800 if( (unsigned)x1 < (unsigned)cols )
802 if( d != -dleft[x1] )
805 E += icvAddTerm( var, var1, 0, INFINITY, 0, 0, ebuf, nedges );
812 //t = (double)cvGetTickCount() - t;
813 ebuf[0].weight = ebuf[1].weight = 0;
814 E += icvGCMaxFlow( vbuf, nvtx, ebuf, state2->orphans, state2->maxOrphans );
818 for( y = 0; y < rows; y++ )
820 short* dleft = dleft0 + dstep*y;
821 short* dright = dright0 + dstep*y;
822 GCVtx** pleft = pleft0 + pstep*y;
823 GCVtx** pright = pright0 + pstep*y;
824 for( x = 0; x < cols; x++ )
826 GCVtx* var = pleft[x];
827 if( var && var->parent && var->t )
828 dleft[x] = (short)alpha;
831 if( var && var->parent && var->t )
832 dright[x] = (short)-alpha;
837 return MIN(E, Eprev);
841 CV_IMPL void cvFindStereoCorrespondenceGC( const CvArr* _left, const CvArr* _right,
842 CvArr* _dispLeft, CvArr* _dispRight, CvStereoGCState* state, int useDisparityGuess )
844 CvStereoGCState2 state2;
846 state2.maxOrphans = 0;
848 CV_FUNCNAME( "cvFindStereoCorrespondenceGC" );
852 CvMat lstub, *left = cvGetMat( _left, &lstub );
853 CvMat rstub, *right = cvGetMat( _right, &rstub );
854 CvMat dlstub, *dispLeft = cvGetMat( _dispLeft, &dlstub );
855 CvMat drstub, *dispRight = cvGetMat( _dispRight, &drstub );
857 int iter, i, nZeroExpansions = 0;
858 CvRNG rng = cvRNG(-1);
863 CV_ASSERT( state != 0 );
864 CV_ASSERT( CV_ARE_SIZES_EQ(left, right) && CV_ARE_TYPES_EQ(left, right) &&
865 CV_MAT_TYPE(left->type) == CV_8UC1 );
866 CV_ASSERT( !dispLeft ||
867 (CV_ARE_SIZES_EQ(dispLeft, left) && CV_MAT_CN(dispLeft->type) == 1) );
868 CV_ASSERT( !dispRight ||
869 (CV_ARE_SIZES_EQ(dispRight, left) && CV_MAT_CN(dispRight->type) == 1) );
871 size = cvGetSize(left);
872 if( !state->left || state->left->width != size.width || state->left->height != size.height )
874 int pcn = (int)(sizeof(GCVtx*)/sizeof(int));
875 int vcn = (int)(sizeof(GCVtx)/sizeof(int));
876 int ecn = (int)(sizeof(GCEdge)/sizeof(int));
877 cvReleaseMat( &state->left );
878 cvReleaseMat( &state->right );
879 cvReleaseMat( &state->ptrLeft );
880 cvReleaseMat( &state->ptrRight );
881 cvReleaseMat( &state->dispLeft );
882 cvReleaseMat( &state->dispRight );
884 state->left = cvCreateMat( size.height, size.width, CV_8UC3 );
885 state->right = cvCreateMat( size.height, size.width, CV_8UC3 );
886 state->dispLeft = cvCreateMat( size.height, size.width, CV_16SC1 );
887 state->dispRight = cvCreateMat( size.height, size.width, CV_16SC1 );
888 state->ptrLeft = cvCreateMat( size.height, size.width, CV_32SC(pcn) );
889 state->ptrRight = cvCreateMat( size.height, size.width, CV_32SC(pcn) );
890 state->vtxBuf = cvCreateMat( 1, size.height*size.width*2, CV_32SC(vcn) );
891 state->edgeBuf = cvCreateMat( 1, size.height*size.width*12 + 16, CV_32SC(ecn) );
894 if( !useDisparityGuess )
896 cvSet( state->dispLeft, cvScalarAll(OCCLUDED));
897 cvSet( state->dispRight, cvScalarAll(OCCLUDED));
901 CV_ASSERT( dispLeft && dispRight );
902 cvConvert( dispLeft, state->dispLeft );
903 cvConvert( dispRight, state->dispRight );
906 state2.Ithreshold = state->Ithreshold;
907 state2.interactionRadius = state->interactionRadius;
908 state2.lambda = cvRound(state->lambda*DENOMINATOR);
909 state2.lambda1 = cvRound(state->lambda1*DENOMINATOR);
910 state2.lambda2 = cvRound(state->lambda2*DENOMINATOR);
911 state2.K = cvRound(state->K*DENOMINATOR);
913 icvInitStereoConstTabs();
914 icvInitGraySubpix( left, right, state->left, state->right );
915 disp = (int*)cvStackAlloc( state->numberOfDisparities*sizeof(disp[0]) );
916 _disp = cvMat( 1, state->numberOfDisparities, CV_32S, disp );
917 cvRange( &_disp, state->minDisparity, state->minDisparity + state->numberOfDisparities );
918 cvRandShuffle( &_disp, &rng );
920 if( state2.lambda < 0 && (state2.K < 0 || state2.lambda1 < 0 || state2.lambda2 < 0) )
922 float L = icvComputeK(state)*0.2f;
923 state2.lambda = cvRound(L*DENOMINATOR);
927 state2.K = state2.lambda*5;
928 if( state2.lambda1 < 0 )
929 state2.lambda1 = state2.lambda*3;
930 if( state2.lambda2 < 0 )
931 state2.lambda2 = state2.lambda;
933 icvInitStereoTabs( &state2 );
935 E = icvComputeEnergy( state, &state2, !useDisparityGuess );
936 for( iter = 0; iter < state->maxIters; iter++ )
938 for( i = 0; i < state->numberOfDisparities; i++ )
941 int64 Enew = icvAlphaExpand( E, -alpha, state, &state2 );
947 else if( ++nZeroExpansions >= state->numberOfDisparities )
953 cvConvert( state->dispLeft, dispLeft );
955 cvConvert( state->dispRight, dispRight );
959 cvFree( &state2.orphans );