mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 09:25:45 +08:00
Update drawing.cpp and test_contours.cpp
This commit is contained in:
parent
7be5181bff
commit
b385767c1c
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,6 +62,7 @@
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
#include <stack>
|
||||
#include <numeric>
|
||||
|
||||
#define GET_OPTIMIZED(func) (func)
|
||||
|
||||
|
@ -446,9 +446,9 @@ static void d2xy(int n, int d, int *x, int *y)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Imgproc_FindContours, hilbert)
|
||||
static Mat draw_hilbert(int n = 64, int scale = 10)
|
||||
{
|
||||
int n = 64, n2 = n*n, scale = 10, w = (n + 2)*scale;
|
||||
int n2 = n*n, w = (n + 2)*scale;
|
||||
Point ofs(scale, scale);
|
||||
Mat img(w, w, CV_8U);
|
||||
img.setTo(Scalar::all(0));
|
||||
@ -462,12 +462,19 @@ TEST(Imgproc_FindContours, hilbert)
|
||||
p = q;
|
||||
}
|
||||
dilate(img, img, Mat());
|
||||
return img;
|
||||
}
|
||||
|
||||
TEST(Imgproc_FindContours, hilbert)
|
||||
{
|
||||
Mat img = draw_hilbert();
|
||||
vector<vector<Point> > contours;
|
||||
|
||||
findContours(img, contours, noArray(), RETR_LIST, CHAIN_APPROX_NONE);
|
||||
ASSERT_EQ(1, (int)contours.size());
|
||||
ASSERT_EQ(78632, (int)contours[0].size());
|
||||
|
||||
findContours(img, contours, noArray(), RETR_LIST, CHAIN_APPROX_SIMPLE);
|
||||
img.setTo(Scalar::all(0));
|
||||
|
||||
drawContours(img, contours, 0, Scalar::all(255), 1);
|
||||
|
||||
ASSERT_EQ(1, (int)contours.size());
|
||||
ASSERT_EQ(9832, (int)contours[0].size());
|
||||
}
|
||||
@ -539,6 +546,38 @@ TEST(Imgproc_FindContours, regression_4363_shared_nbd)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Imgproc_DrawContours, regression_26264)
|
||||
{
|
||||
Mat img = draw_hilbert(32);
|
||||
img.push_back(~img);
|
||||
|
||||
for (int i = 50; i < 200; i += 17)
|
||||
{
|
||||
rectangle(img, Rect(i, i, img.cols - (i*2), img.rows - (i*2)), Scalar(0), 7);
|
||||
rectangle(img, Rect(i, i, img.cols - (i*2), img.rows - (i*2)), Scalar(255), 1);
|
||||
}
|
||||
|
||||
vector<vector<Point> > contours;
|
||||
vector<Vec4i> hierarchy;
|
||||
findContours(img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
|
||||
img.setTo(Scalar::all(0));
|
||||
Mat img1 = img.clone();
|
||||
Mat img2 = img.clone();
|
||||
Mat img3 = img.clone();
|
||||
|
||||
int idx = 0;
|
||||
while (idx >= 0)
|
||||
{
|
||||
drawContours(img, contours, idx, Scalar::all(255), FILLED, LINE_8, hierarchy);
|
||||
drawContours(img2, contours, idx, Scalar::all(255), 1, LINE_8, hierarchy);
|
||||
idx = hierarchy[idx][0];
|
||||
}
|
||||
|
||||
drawContours(img1, contours, -1, Scalar::all(255), FILLED, LINE_8, hierarchy);
|
||||
drawContours(img3, contours, -1, Scalar::all(255), 1, LINE_8, hierarchy);
|
||||
ASSERT_EQ(0, cvtest::norm(img, img1, NORM_INF));
|
||||
ASSERT_EQ(0, cvtest::norm(img2, img3, NORM_INF));
|
||||
}
|
||||
|
||||
TEST(Imgproc_PointPolygonTest, regression_10222)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user