Skip to content

Commit c1e5c16

Browse files
committed
Backport C-API cleanup (imgproc) from 5.x
1 parent c63d79c commit c1e5c16

File tree

34 files changed

+206
-397
lines changed

34 files changed

+206
-397
lines changed

doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.markdown

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,9 @@ receives three arguments:
204204

205205
We can choose any of three shapes for our kernel:
206206

207-
- Rectangular box: CV_SHAPE_RECT
208-
- Cross: CV_SHAPE_CROSS
209-
- Ellipse: CV_SHAPE_ELLIPSE
207+
- Rectangular box: Imgproc.SHAPE_RECT
208+
- Cross: Imgproc.SHAPE_CROSS
209+
- Ellipse: Imgproc.SHAPE_ELLIPSE
210210

211211
Together with the shape we specify the size of our kernel and the *anchor point*. If the anchor point is not
212212
specified, it is assumed to be in the center.

doc/tutorials/imgproc/histograms/histogram_comparison/histogram_comparison.markdown

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ Theory
2727
(\f$d(H_{1}, H_{2})\f$) to express how well both histograms match.
2828
- OpenCV implements the function @ref cv::compareHist to perform a comparison. It also offers 4
2929
different metrics to compute the matching:
30-
-# **Correlation ( CV_COMP_CORREL )**
30+
-# **Correlation ( cv::HISTCMP_CORREL )**
3131
\f[d(H_1,H_2) = \frac{\sum_I (H_1(I) - \bar{H_1}) (H_2(I) - \bar{H_2})}{\sqrt{\sum_I(H_1(I) - \bar{H_1})^2 \sum_I(H_2(I) - \bar{H_2})^2}}\f]
3232
where
3333
\f[\bar{H_k} = \frac{1}{N} \sum _J H_k(J)\f]
3434
and \f$N\f$ is the total number of histogram bins.
3535

36-
-# **Chi-Square ( CV_COMP_CHISQR )**
36+
-# **Chi-Square ( cv::HISTCMP_CHISQR )**
3737
\f[d(H_1,H_2) = \sum _I \frac{\left(H_1(I)-H_2(I)\right)^2}{H_1(I)}\f]
3838

39-
-# **Intersection ( method=CV_COMP_INTERSECT )**
39+
-# **Intersection ( method=cv::HISTCMP_INTERSECT )**
4040
\f[d(H_1,H_2) = \sum _I \min (H_1(I), H_2(I))\f]
4141

42-
-# **Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )**
42+
-# **Bhattacharyya distance ( cv::HISTCMP_BHATTACHARYYA )**
4343
\f[d(H_1,H_2) = \sqrt{1 - \frac{1}{\sqrt{\bar{H_1} \bar{H_2} N^2}} \sum_I \sqrt{H_1(I) \cdot H_2(I)}}\f]
4444

4545
Code

modules/calib3d/src/calibinit.cpp

+2-149
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@ namespace cv {
101101

102102
#define MAX_CONTOUR_APPROX 7
103103

104-
#define USE_CV_FINDCONTOURS // switch between cv::findContours() and legacy C API
105-
#ifdef USE_CV_FINDCONTOURS
106104
struct QuadCountour {
107105
Point pt[4];
108106
int parent_contour;
@@ -113,18 +111,6 @@ struct QuadCountour {
113111
pt[0] = pt_[0]; pt[1] = pt_[1]; pt[2] = pt_[2]; pt[3] = pt_[3];
114112
}
115113
};
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
128114

129115

130116
/** This structure stores information about the chessboard corner.*/
@@ -552,13 +538,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size,
552538
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);
553539

554540
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);
562542
DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1));
563543
SHOW_QUADS("New quads", thresh_img_new, &detector.all_quads[0], detector.all_quads_count);
564544
if (detector.processQuads(out_corners, prev_sqr_size))
@@ -623,13 +603,7 @@ bool findChessboardCorners(InputArray image_, Size pattern_size,
623603
rectangle( thresh_img, Point(0,0), Point(thresh_img.cols-1, thresh_img.rows-1), Scalar(255,255,255), 3, LINE_8);
624604

625605
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);
633607
DPRINTF("Quad count: %d/%d", detector.all_quads_count, (pattern_size.width/2+1)*(pattern_size.height/2+1));
634608
SHOW_QUADS("Old quads", thresh_img, &detector.all_quads[0], detector.all_quads_count);
635609
if (detector.processQuads(out_corners, prev_sqr_size))
@@ -1376,7 +1350,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
13761350

13771351
for (int j = 0; j < 4; ++j)
13781352
{
1379-
//cvLine( debug_img, cvPointFrom32f(q->corners[j]->pt), cvPointFrom32f(q->corners[(j+1)&3]->pt), color, 1, CV_AA, 0 );
13801353
if (q->neighbors[j])
13811354
{
13821355
int next_j = (j + 1) & 3;
@@ -1465,7 +1438,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
14651438
goto finalize;
14661439

14671440
cur->row = 0;
1468-
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,255,0), -1, 8, 0 );
14691441

14701442
first = below; // remember the first corner in the next row
14711443

@@ -1474,7 +1446,6 @@ int ChessBoardDetector::checkQuadGroup(std::vector<ChessBoardQuad*>& quad_group,
14741446
{
14751447
right->row = 0;
14761448
out_corners.push_back(right);
1477-
//cvCircle( debug_img, cvPointFrom32f(right->pt), 3, cvScalar(0,255-j*10,0), -1, 8, 0 );
14781449
if( right->count == 2 )
14791450
break;
14801451
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,
15191490
{
15201491
cur->row = i;
15211492
out_corners.push_back(cur);
1522-
//cvCircle( debug_img, cvPointFrom32f(cur->pt), 3, cvScalar(0,0,255-j*10), -1, 8, 0 );
15231493
if (cur->count == 2 + (i < height-1) && j > 0)
15241494
break;
15251495

@@ -1764,7 +1734,6 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags)
17641734
int min_size = 25; //cvRound( image->cols * image->rows * .03 * 0.01 * 0.92 );
17651735

17661736
bool filterQuads = (flags & CALIB_CB_FILTER_QUADS) != 0;
1767-
#ifdef USE_CV_FINDCONTOURS // use cv::findContours
17681737

17691738
std::vector<std::vector<Point> > contours;
17701739
std::vector<Vec4i> hierarchy;
@@ -1879,122 +1848,6 @@ void ChessBoardDetector::generateQuads(const cv::Mat& image_, int flags)
18791848
}
18801849
}
18811850

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-
19981851
all_quads_count = quad_count;
19991852

20001853
CV_LOG_VERBOSE(NULL, 3, "Total quad contours: " << total);

modules/calib3d/test/test_undistort.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ TEST(DISABLED_Calib3d_InitInverseRectificationMap, accuracy) { CV_InitInverseRec
998998
////////////////////////////// undistort /////////////////////////////////
999999

10001000
static void test_remap( const Mat& src, Mat& dst, const Mat& mapx, const Mat& mapy,
1001-
Mat* mask=0, int interpolation=CV_INTER_LINEAR )
1001+
Mat* mask=0, int interpolation=cv::INTER_LINEAR )
10021002
{
10031003
int x, y, k;
10041004
int drows = dst.rows, dcols = dst.cols;
@@ -1009,7 +1009,7 @@ static void test_remap( const Mat& src, Mat& dst, const Mat& mapx, const Mat& ma
10091009
int step = (int)(src.step / CV_ELEM_SIZE(depth));
10101010
int delta;
10111011

1012-
if( interpolation != CV_INTER_CUBIC )
1012+
if( interpolation != cv::INTER_CUBIC )
10131013
{
10141014
delta = 0;
10151015
scols -= 1; srows -= 1;
@@ -1318,7 +1318,7 @@ void CV_UndistortTest::get_test_array_types_and_sizes( int test_case_idx, vector
13181318
sizes[INPUT][2] = cvtest::randInt(rng)%2 ? cvSize(4,1) : cvSize(1,4);
13191319
types[INPUT][3] = types[INPUT][1];
13201320
sizes[INPUT][3] = sizes[INPUT][1];
1321-
interpolation = CV_INTER_LINEAR;
1321+
interpolation = cv::INTER_LINEAR;
13221322
}
13231323

13241324

modules/core/include/opencv2/core/cuda.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ Below is an example that utilizes BufferPool with StackAllocator:
609609
GpuMat d_src2 = pool2.getBuffer(1024, 1024, CV_8UC1); // 1MB
610610
GpuMat d_dst2 = pool2.getBuffer(1024, 1024, CV_8UC3); // 3MB
611611
612-
cvtColor(d_src1, d_dst1, CV_GRAY2BGR, 0, stream1);
613-
cvtColor(d_src2, d_dst2, CV_GRAY2BGR, 0, stream2);
612+
cvtColor(d_src1, d_dst1, cv::COLOR_GRAY2BGR, 0, stream1);
613+
cvtColor(d_src2, d_dst2, cv::COLOR_GRAY2BGR, 0, stream2);
614614
}
615615
@endcode
616616
@@ -675,8 +675,8 @@ and the corresponding memory is automatically returned to the pool for later usa
675675
d_src1.setTo(Scalar(i), stream1);
676676
d_src2.setTo(Scalar(i), stream2);
677677
678-
cvtColor(d_src1, d_dst1, CV_GRAY2BGR, 0, stream1);
679-
cvtColor(d_src2, d_dst2, CV_GRAY2BGR, 0, stream2);
678+
cvtColor(d_src1, d_dst1, cv::COLOR_GRAY2BGR, 0, stream1);
679+
cvtColor(d_src2, d_dst2, cv::COLOR_GRAY2BGR, 0, stream2);
680680
// The order of destruction of the local variables is:
681681
// d_dst2 => d_src2 => d_dst1 => d_src1
682682
// LIFO rule is satisfied, this code runs without error

modules/gapi/include/opencv2/gapi/core.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ types.
14181418
*/
14191419
GAPI_EXPORTS_W GMat threshold(const GMat& src, const GScalar& thresh, const GScalar& maxval, int type);
14201420
/** @overload
1421-
This function applicable for all threshold types except CV_THRESH_OTSU and CV_THRESH_TRIANGLE
1421+
This function applicable for all threshold types except cv::THRESH_OTSU and cv::THRESH_TRIANGLE
14221422
@note Function textual ID is "org.opencv.core.matrixop.thresholdOT"
14231423
*/
14241424
GAPI_EXPORTS_W std::tuple<GMat, GScalar> threshold(const GMat& src, const GScalar& maxval, int type);

modules/imgproc/doc/pics/polar_remap_doc.svg

+3-3
Loading

modules/imgproc/misc/java/test/ImgprocTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public void testCompareHist() {
357357
H1.put(0, 0, 1, 2, 3);
358358
H2.put(0, 0, 4, 5, 6);
359359

360-
double distance = Imgproc.compareHist(H1, H2, Imgproc.CV_COMP_CORREL);
360+
double distance = Imgproc.compareHist(H1, H2, Imgproc.HISTCMP_CORREL);
361361

362362
assertEquals(1., distance, EPS);
363363
}
@@ -636,7 +636,7 @@ public void testDistanceTransformWithLabels() {
636636
Mat dstLables = getMat(CvType.CV_32SC1, 0);
637637
Mat labels = new Mat();
638638

639-
Imgproc.distanceTransformWithLabels(gray128, dst, labels, Imgproc.CV_DIST_L2, 3);
639+
Imgproc.distanceTransformWithLabels(gray128, dst, labels, Imgproc.DIST_L2, 3);
640640

641641
assertMatEqual(dstLables, labels);
642642
assertMatEqual(getMat(CvType.CV_32FC1, 8192), dst, EPS);
@@ -813,7 +813,7 @@ public void testFitLine() {
813813
Mat linePoints = new Mat(4, 1, CvType.CV_32FC1);
814814
linePoints.put(0, 0, 0.53198653, 0.84675282, 2.5, 3.75);
815815

816-
Imgproc.fitLine(points, dst, Imgproc.CV_DIST_L12, 0, 0.01, 0.01);
816+
Imgproc.fitLine(points, dst, Imgproc.DIST_L12, 0, 0.01, 0.01);
817817

818818
assertMatEqual(linePoints, dst, EPS);
819819
}
@@ -1040,7 +1040,7 @@ public void testHoughCirclesMatMatIntDoubleDouble() {
10401040
Mat img = new Mat(sz, sz, CvType.CV_8U, new Scalar(128));
10411041
Mat circles = new Mat();
10421042

1043-
Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
1043+
Imgproc.HoughCircles(img, circles, Imgproc.HOUGH_GRADIENT, 2, img.rows() / 4);
10441044

10451045
assertEquals(0, circles.cols());
10461046
}
@@ -1054,7 +1054,7 @@ public void testHoughCirclesMatMatIntDoubleDouble1() {
10541054
int radius = Math.min(img.cols() / 4, img.rows() / 4);
10551055
Imgproc.circle(img, center, radius, colorBlack, 3);
10561056

1057-
Imgproc.HoughCircles(img, circles, Imgproc.CV_HOUGH_GRADIENT, 2, img.rows() / 4);
1057+
Imgproc.HoughCircles(img, circles, Imgproc.HOUGH_GRADIENT, 2, img.rows() / 4);
10581058

10591059
assertEquals(1, circles.cols());
10601060
}
@@ -1308,7 +1308,7 @@ public void testMatchShapes() {
13081308
contour1.put(0, 0, 1, 1, 5, 1, 4, 3, 6, 2);
13091309
contour2.put(0, 0, 1, 1, 6, 1, 4, 1, 2, 5);
13101310

1311-
double distance = Imgproc.matchShapes(contour1, contour2, Imgproc.CV_CONTOURS_MATCH_I1, 1);
1311+
double distance = Imgproc.matchShapes(contour1, contour2, Imgproc.CONTOURS_MATCH_I1, 1);
13121312

13131313
assertEquals(2.81109697365334, distance, EPS);
13141314
}

0 commit comments

Comments
 (0)