@@ -101,8 +101,6 @@ namespace cv {
101
101
102
102
#define MAX_CONTOUR_APPROX 7
103
103
104
- #define USE_CV_FINDCONTOURS // switch between cv::findContours() and legacy C API
105
- #ifdef USE_CV_FINDCONTOURS
106
104
struct QuadCountour {
107
105
Point pt[4 ];
108
106
int parent_contour;
@@ -113,18 +111,6 @@ struct QuadCountour {
113
111
pt[0 ] = pt_[0 ]; pt[1 ] = pt_[1 ]; pt[2 ] = pt_[2 ]; pt[3 ] = pt_[3 ];
114
112
}
115
113
};
116
- #else
117
-
118
- } // namespace
119
- #include " opencv2/imgproc/imgproc_c.h"
120
- namespace cv {
121
-
122
- struct CvContourEx
123
- {
124
- CV_CONTOUR_FIELDS ()
125
- int counter;
126
- };
127
- #endif
128
114
129
115
130
116
/* * This structure stores information about the chessboard corner.*/
@@ -552,13 +538,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size,
552
538
rectangle ( thresh_img_new, Point (0 ,0 ), Point (thresh_img_new.cols -1 , thresh_img_new.rows -1 ), Scalar (255 ,255 ,255 ), 3 , LINE_8);
553
539
554
540
detector.reset ();
555
-
556
- #ifdef USE_CV_FINDCONTOURS
557
- Mat binarized_img = thresh_img_new;
558
- #else
559
- Mat binarized_img = thresh_img_new.clone (); // make clone because cvFindContours modifies the source image
560
- #endif
561
- detector.generateQuads (binarized_img, flags);
541
+ detector.generateQuads (thresh_img_new, flags);
562
542
DPRINTF (" Quad count: %d/%d" , detector.all_quads_count , (pattern_size.width /2 +1 )*(pattern_size.height /2 +1 ));
563
543
SHOW_QUADS (" New quads" , thresh_img_new, &detector.all_quads [0 ], detector.all_quads_count );
564
544
if (detector.processQuads (out_corners, prev_sqr_size))
@@ -623,13 +603,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size,
623
603
rectangle ( thresh_img, Point (0 ,0 ), Point (thresh_img.cols -1 , thresh_img.rows -1 ), Scalar (255 ,255 ,255 ), 3 , LINE_8);
624
604
625
605
detector.reset ();
626
-
627
- #ifdef USE_CV_FINDCONTOURS
628
- Mat binarized_img = thresh_img;
629
- #else
630
- Mat binarized_img = (useAdaptive) ? thresh_img : thresh_img.clone (); // make clone because cvFindContours modifies the source image
631
- #endif
632
- detector.generateQuads (binarized_img, flags);
606
+ detector.generateQuads (thresh_img, flags);
633
607
DPRINTF (" Quad count: %d/%d" , detector.all_quads_count , (pattern_size.width /2 +1 )*(pattern_size.height /2 +1 ));
634
608
SHOW_QUADS (" Old quads" , thresh_img, &detector.all_quads [0 ], detector.all_quads_count );
635
609
if (detector.processQuads (out_corners, prev_sqr_size))
@@ -1376,7 +1350,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
1376
1350
1377
1351
for (int j = 0 ; j < 4 ; ++j)
1378
1352
{
1379
- // cvLine( debug_img, cvPointFrom32f(q->corners[j]->pt), cvPointFrom32f(q->corners[(j+1)&3]->pt), color, 1, CV_AA, 0 );
1380
1353
if (q->neighbors [j])
1381
1354
{
1382
1355
int next_j = (j + 1 ) & 3 ;
@@ -1465,7 +1438,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
1465
1438
goto finalize;
1466
1439
1467
1440
cur->row = 0 ;
1468
- // cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,255,0), -1, 8, 0 );
1469
1441
1470
1442
first = below; // remember the first corner in the next row
1471
1443
@@ -1474,7 +1446,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
1474
1446
{
1475
1447
right->row = 0 ;
1476
1448
out_corners.push_back (right);
1477
- // cvCircle( debug_img, cvPointFrom32f(right->pt), 3, cvScalar(0,255-j*10,0), -1, 8, 0 );
1478
1449
if ( right->count == 2 )
1479
1450
break ;
1480
1451
if ( right->count != 3 || (int )out_corners.size () >= std::max (pattern_size.width ,pattern_size.height ) )
@@ -1519,7 +1490,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
1519
1490
{
1520
1491
cur->row = i;
1521
1492
out_corners.push_back (cur);
1522
- // cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,0,255-j*10), -1, 8, 0 );
1523
1493
if (cur->count == 2 + (i < height-1 ) && j > 0 )
1524
1494
break ;
1525
1495
@@ -1764,7 +1734,6 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags)
1764
1734
int min_size = 25 ; // cvRound( image->cols * image->rows * .03 * 0.01 * 0.92 );
1765
1735
1766
1736
bool filterQuads = (flags & CALIB_CB_FILTER_QUADS) != 0 ;
1767
- #ifdef USE_CV_FINDCONTOURS // use cv::findContours
1768
1737
1769
1738
std::vector<std::vector<Point > > contours;
1770
1739
std::vector<Vec4i> hierarchy;
@@ -1879,122 +1848,6 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags)
1879
1848
}
1880
1849
}
1881
1850
1882
- #else // use legacy API: cvStartFindContours / cvFindNextContour / cvEndFindContours
1883
-
1884
- CvMat image_old = cvMat (image_), *image = &image_old;
1885
-
1886
- CvContourEx* board = 0 ;
1887
-
1888
- // create temporary storage for contours and the sequence of pointers to found quadrangles
1889
- cv::Ptr <CvMemStorage> temp_storage (cvCreateMemStorage (0 ));
1890
- CvSeq *root = cvCreateSeq (0 , sizeof (CvSeq), sizeof (CvSeq*), temp_storage);
1891
-
1892
- // initialize contour retrieving routine
1893
- CvContourScanner scanner = cvStartFindContours (image, temp_storage, sizeof (CvContourEx),
1894
- CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
1895
-
1896
- // get all the contours one by one
1897
- CvSeq* src_contour = NULL ;
1898
- while ((src_contour = cvFindNextContour (scanner)) != NULL )
1899
- {
1900
- CvSeq *dst_contour = 0 ;
1901
- CvRect rect = ((CvContour*)src_contour)->rect ;
1902
-
1903
- // reject contours with too small perimeter
1904
- if ( CV_IS_SEQ_HOLE (src_contour) && rect.width *rect.height >= min_size )
1905
- {
1906
- const int min_approx_level = 1 , max_approx_level = MAX_CONTOUR_APPROX;
1907
- for (int approx_level = min_approx_level; approx_level <= max_approx_level; approx_level++ )
1908
- {
1909
- dst_contour = cvApproxPoly ( src_contour, sizeof (CvContour), temp_storage,
1910
- CV_POLY_APPROX_DP, (float )approx_level );
1911
- if ( dst_contour->total == 4 )
1912
- break ;
1913
-
1914
- // we call this again on its own output, because sometimes
1915
- // cvApproxPoly() does not simplify as much as it should.
1916
- dst_contour = cvApproxPoly ( dst_contour, sizeof (CvContour), temp_storage,
1917
- CV_POLY_APPROX_DP, (float )approx_level );
1918
-
1919
- if ( dst_contour->total == 4 )
1920
- break ;
1921
- }
1922
-
1923
- // reject non-quadrangles
1924
- if ( dst_contour->total == 4 && cvCheckContourConvexity (dst_contour) )
1925
- {
1926
- cv::Point2i pt[4 ];
1927
- double p = cvContourPerimeter (dst_contour);
1928
- double area = fabs (cvContourArea (dst_contour, CV_WHOLE_SEQ));
1929
-
1930
- for (int i = 0 ; i < 4 ; ++i)
1931
- pt[i] = *(CvPoint*)cvGetSeqElem (dst_contour, i);
1932
- CV_LOG_VERBOSE (NULL , 9 , " ... contours(" << root->total << " added):" << pt[0 ] << " " << pt[1 ] << " " << pt[2 ] << " " << pt[3 ]);
1933
-
1934
- double d1 = sqrt (normL2Sqr<double >(pt[0 ] - pt[2 ]));
1935
- double d2 = sqrt (normL2Sqr<double >(pt[1 ] - pt[3 ]));
1936
-
1937
- // philipg. Only accept those quadrangles which are more square
1938
- // than rectangular and which are big enough
1939
- double d3 = sqrt (normL2Sqr<double >(pt[0 ] - pt[1 ]));
1940
- double d4 = sqrt (normL2Sqr<double >(pt[1 ] - pt[2 ]));
1941
- if (!filterQuads ||
1942
- (d3*4 > d4 && d4*4 > d3 && d3*d4 < area*1.5 && area > min_size &&
1943
- d1 >= 0.15 * p && d2 >= 0.15 * p))
1944
- {
1945
- CvContourEx* parent = (CvContourEx*)(src_contour->v_prev );
1946
- parent->counter ++;
1947
- if ( !board || board->counter < parent->counter )
1948
- board = parent;
1949
- dst_contour->v_prev = (CvSeq*)parent;
1950
- // for( i = 0; i < 4; i++ ) cvLine( debug_img, pt[i], pt[(i+1)&3], cvScalar(200,255,255), 1, CV_AA, 0 );
1951
- cvSeqPush ( root, &dst_contour );
1952
- }
1953
- }
1954
- }
1955
- }
1956
-
1957
- // finish contour retrieving
1958
- cvEndFindContours ( &scanner );
1959
-
1960
- // allocate quad & corner buffers
1961
- int total = root->total ;
1962
- size_t max_quad_buf_size = std::max ((size_t )2 , (size_t )total * 3 );
1963
- all_quads.allocate (max_quad_buf_size);
1964
- all_corners.allocate (max_quad_buf_size * 4 );
1965
-
1966
- // Create array of quads structures
1967
- for (int idx = 0 ; idx < total; ++idx)
1968
- {
1969
- /* CvSeq* */ src_contour = *(CvSeq**)cvGetSeqElem (root, idx);
1970
- if (filterQuads && src_contour->v_prev != (CvSeq*)board)
1971
- continue ;
1972
-
1973
- int quad_idx = quad_count++;
1974
- ChessBoardQuad& q = all_quads[quad_idx];
1975
-
1976
- // reset group ID
1977
- q = ChessBoardQuad ();
1978
- CV_Assert (src_contour->total == 4 );
1979
- for (int i = 0 ; i < 4 ; i++)
1980
- {
1981
- Point * onePoint = (Point *)cvGetSeqElem (src_contour, i);
1982
- CV_Assert (onePoint != NULL );
1983
- Point2f pt (*onePoint);
1984
- ChessBoardCorner& corner = all_corners[quad_idx*4 + i];
1985
-
1986
- corner = ChessBoardCorner (pt);
1987
- q.corners [i] = &corner;
1988
- }
1989
- q.edge_len = FLT_MAX;
1990
- for (int i = 0 ; i < 4 ; ++i)
1991
- {
1992
- float d = normL2Sqr<float >(q.corners [i]->pt - q.corners [(i+1 )&3 ]->pt );
1993
- q.edge_len = std::min (q.edge_len , d);
1994
- }
1995
- }
1996
- #endif
1997
-
1998
1851
all_quads_count = quad_count;
1999
1852
2000
1853
CV_LOG_VERBOSE (NULL , 3 , " Total quad contours: " << total);
0 commit comments