|
|
|
@ -2477,36 +2477,9 @@ void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours, |
|
|
|
|
CV_Assert(ncontours <= (size_t)std::numeric_limits<int>::max()); |
|
|
|
|
if (lineType == cv::LINE_AA && _image.depth() != CV_8U) |
|
|
|
|
lineType = 8; |
|
|
|
|
Mat image = _image.getMat(), hierarchy = _hierarchy.getMat(); |
|
|
|
|
Mat image = _image.getMat(); |
|
|
|
|
Mat_<Vec4i> hierarchy = _hierarchy.getMat(); |
|
|
|
|
|
|
|
|
|
if (thickness >= 0) // contour lines
|
|
|
|
|
{ |
|
|
|
|
double color_buf[4] {}; |
|
|
|
|
scalarToRawData(color, color_buf, _image.type(), 0 ); |
|
|
|
|
int i = 0, end = (int)ncontours; |
|
|
|
|
if (contourIdx >= 0) |
|
|
|
|
{ |
|
|
|
|
i = contourIdx; |
|
|
|
|
end = i + 1; |
|
|
|
|
} |
|
|
|
|
for (; i < end; ++i) |
|
|
|
|
{ |
|
|
|
|
Mat cnt = _contours.getMat(i); |
|
|
|
|
if (cnt.empty()) |
|
|
|
|
continue; |
|
|
|
|
const int npoints = cnt.checkVector(2, CV_32S); |
|
|
|
|
CV_Assert(npoints > 0); |
|
|
|
|
for (int j = 0; j < npoints; ++j) |
|
|
|
|
{ |
|
|
|
|
const bool isLastIter = j == npoints - 1; |
|
|
|
|
const Point pt1 = cnt.at<Point>(j); |
|
|
|
|
const Point pt2 = cnt.at<Point>(isLastIter ? 0 : j + 1); |
|
|
|
|
cv::ThickLine(image, pt1 + offset, pt2 + offset, color_buf, thickness, lineType, 2, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else // filled polygons
|
|
|
|
|
{ |
|
|
|
|
int i = 0, end = (int)ncontours; |
|
|
|
|
if (contourIdx >= 0) |
|
|
|
|
{ |
|
|
|
@ -2516,8 +2489,8 @@ void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours, |
|
|
|
|
std::vector<int> indexesToFill; |
|
|
|
|
if (hierarchy.empty() || maxLevel == 0) |
|
|
|
|
{ |
|
|
|
|
for (; i != end; ++i) |
|
|
|
|
indexesToFill.push_back(i); |
|
|
|
|
indexesToFill.resize(end - i); |
|
|
|
|
std::iota(indexesToFill.begin(), indexesToFill.end(), i); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
@ -2525,7 +2498,7 @@ void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours, |
|
|
|
|
for (; i != end; ++i) |
|
|
|
|
{ |
|
|
|
|
// either all from the top level or a single contour
|
|
|
|
|
if (hierarchy.at<Vec4i>(i)[3] < 0 || contourIdx >= 0) |
|
|
|
|
if (hierarchy(i)[3] < 0 || contourIdx >= 0) |
|
|
|
|
indexes.push(i); |
|
|
|
|
} |
|
|
|
|
while (!indexes.empty()) |
|
|
|
@ -2539,7 +2512,7 @@ void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours, |
|
|
|
|
int par = cur; |
|
|
|
|
while (par >= 0) |
|
|
|
|
{ |
|
|
|
|
par = hierarchy.at<Vec4i>(par)[3]; // parent
|
|
|
|
|
par = hierarchy(par)[3]; // parent
|
|
|
|
|
++curLevel; |
|
|
|
|
} |
|
|
|
|
if (curLevel <= maxLevel) |
|
|
|
@ -2547,18 +2520,39 @@ void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours, |
|
|
|
|
indexesToFill.push_back(cur); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int next = hierarchy.at<Vec4i>(cur)[2]; // first child
|
|
|
|
|
int next = hierarchy(cur)[2]; // first child
|
|
|
|
|
while (next > 0) |
|
|
|
|
{ |
|
|
|
|
indexes.push(next); |
|
|
|
|
next = hierarchy.at<Vec4i>(next)[0]; // next sibling
|
|
|
|
|
next = hierarchy(next)[0]; // next sibling
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
std::vector<Mat> contoursToFill; |
|
|
|
|
contoursToFill.reserve(indexesToFill.size()); |
|
|
|
|
for (const int& idx : indexesToFill) |
|
|
|
|
contoursToFill.push_back(_contours.getMat(idx)); |
|
|
|
|
contoursToFill.emplace_back(_contours.getMat(idx)); |
|
|
|
|
|
|
|
|
|
if (thickness < 0) |
|
|
|
|
fillPoly(image, contoursToFill, color, lineType, 0, offset); |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
double color_buf[4]{}; |
|
|
|
|
scalarToRawData(color, color_buf, _image.type(), 0); |
|
|
|
|
for (const Mat& cnt : contoursToFill) |
|
|
|
|
{ |
|
|
|
|
if (cnt.empty()) |
|
|
|
|
continue; |
|
|
|
|
const int npoints = cnt.checkVector(2, CV_32S); |
|
|
|
|
CV_Assert(npoints > 0); |
|
|
|
|
for (int j = 0; j < npoints; ++j) |
|
|
|
|
{ |
|
|
|
|
const bool isLastIter = j == npoints - 1; |
|
|
|
|
const Point pt1 = cnt.at<Point>(j); |
|
|
|
|
const Point pt2 = cnt.at<Point>(isLastIter ? 0 : j + 1); |
|
|
|
|
cv::ThickLine(image, pt1 + offset, pt2 + offset, color_buf, thickness, lineType, 2, 0); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|