opencv drawContours的工作原理:
- 基于扫描线的多边形填充算法
- CollectPolyEdges 收集多边形边缘
- FillEdgeCollection填充多边形边缘
Structural Analysis and Shape Descriptors — OpenCV 2.4.13.7 documentation opencv文档
- Optimize FillEdgeCollection function · Issue #20986 · opencv/opencv · GitHub Optimize FillEdgeCollection function 优化这个函数的方法
有关更多信息,您可以分析OpenCV的cvDrawContours的github源代码:(文末也粘贴了)
- cv::CollectPolyEdges
- cv::FillEdgeCollection
原理
13图形光栅化——区域填充(种子填充)+多边形扫描转换(扫描线算法)_在区域填充中,多边形扫描转换算法和区域填充算法有何异同_浪在冰城的博客-CSDN博客
- 区域填充
- 多边形的扫描转换
- ①逐点判断:逐个像素判断是否位于多边形内部。方法为:射线法。
- 射线法:从当前的像素发射一条不经过顶点的射线,如果与多边形的交点为奇数个点,则说明该点在多边形内部,如果与多边形的交点为偶数个点,则说明该点在多边形的外部。射线法的虽然实现比较简单,但是速度慢
- ②扫描线算法:
- 代码参考:
- ①计算机图形学——扫描线填充算法(有序边表)代码简单易懂可运行_扫描线填充算法代码_小yuan不搜题的博客-CSDN博客
- ②Scan-line Polygon filling using OPENGL in C - GeeksforGeeks
- ③多边形扫描转换算法(C语言实现)_计算机图形学et表怎么写_苏宇117的博客-CSDN博客 这个只用了ET表,没有用AET表。
- ④
opencv - 填充轮廓时,drawContours函数在OpenCV中如何工作? - IT工具网
opencv源码code
void cv::drawContours( InputOutputArray _image, InputArrayOfArrays _contours,
int contourIdx, const Scalar& color, int thickness,
int lineType, InputArray _hierarchy,
int maxLevel, Point offset )
{
CV_INSTRUMENT_REGION();
Mat image = _image.getMat(), hierarchy = _hierarchy.getMat();
CvMat _cimage = cvMat(image);
size_t ncontours = _contours.total();
size_t i = 0, first = 0, last = ncontours;
std::vector<CvSeq> seq;
std::vector<CvSeqBlock> block;
if( !last )
return;
seq.resize(last);
block.resize(last);
for( i = first; i < last; i++ )
seq[i].first = 0;
if( contourIdx >= 0 )
{
CV_Assert( 0 <= contourIdx && contourIdx < (int)last );
first = contourIdx;
last = contourIdx + 1;
}
for( i = first; i < last; i++ )
{
Mat ci = _contours.getMat((int)i);
if( ci.empty() )
continue;
int npoints = ci.checkVector(2, CV_32S);
CV_Assert( npoints > 0 );
cvMakeSeqHeaderForArray( CV_SEQ_POLYGON, sizeof(CvSeq), sizeof(Point),
ci.ptr(), npoints, &seq[i], &block[i] );
}
if( hierarchy.empty() || maxLevel == 0 )
for( i = first; i < last; i++ )
{
seq[i].h_next = i < last-1 ? &seq[i+1] : 0;
seq[i].h_prev = i > first ? &seq[i-1] : 0;
}
else
{
size_t count = last - first;
CV_Assert(hierarchy.total() == ncontours && hierarchy.type() == CV_32SC4 );
const Vec4i* h = hierarchy.ptr<Vec4i>();
if( count == ncontours )
{
for( i = first; i < last; i++ )
{
int h_next = h[i][0], h_prev = h[i][1],
v_next = h[i][2], v_prev = h[i][3];
seq[i].h_next = (size_t)h_next < count ? &seq[h_next] : 0;
seq[i].h_prev = (size_t)h_prev < count ? &seq[h_prev] : 0;
seq[i].v_next = (size_t)v_next < count ? &seq[v_next] : 0;
seq[i].v_prev = (size_t)v_prev < count ? &seq[v_prev] : 0;
}
}
else
{
int child = h[first][2];
if( child >= 0 )
{
addChildContour(_contours, ncontours, h, child, seq, block);
seq[first].v_next = &seq[child];
}
}
}
cvDrawContours( &_cimage, &seq[first], cvScalar(color), cvScalar(color), contourIdx >= 0 ?
-maxLevel : maxLevel, thickness, lineType, cvPoint(offset) );
}
CV_IMPL void
cvDrawContours( void* _img, CvSeq* contour,
CvScalar _externalColor, CvScalar _holeColor,
int maxLevel, int thickness,
int line_type, CvPoint _offset )
{
CvSeq *contour0 = contour, *h_next = 0;
CvTreeNodeIterator iterator;
std::vector<cv::PolyEdge> edges;
std::vector<cv::Point2l> pts;
cv::Scalar externalColor = _externalColor, holeColor = _holeColor;
cv::Mat img = cv::cvarrToMat(_img);
cv::Point offset = _offset;
double ext_buf[4], hole_buf[4];
if( line_type == CV_AA && img.depth() != CV_8U )
line_type = 8;
if( !contour )
return;
CV_Assert( thickness <= MAX_THICKNESS );
scalarToRawData( externalColor, ext_buf, img.type(), 0 );
scalarToRawData( holeColor, hole_buf, img.type(), 0 );
maxLevel = MAX(maxLevel, INT_MIN+2);
maxLevel = MIN(maxLevel, INT_MAX-1);
if( maxLevel < 0 )
{
h_next = contour->h_next;
contour->h_next = 0;
maxLevel = -maxLevel+1;
}
cvInitTreeNodeIterator( &iterator, contour, maxLevel );
while( (contour = (CvSeq*)cvNextTreeNode( &iterator )) != 0 )
{
CvSeqReader reader;
int i, count = contour->total;
int elem_type = CV_MAT_TYPE(contour->flags);
void* clr = (contour->flags & CV_SEQ_FLAG_HOLE) == 0 ? ext_buf : hole_buf;
cvStartReadSeq( contour, &reader, 0 );
CV_Assert(reader.ptr != NULL);
if( thickness < 0 )
pts.resize(0);
if( CV_IS_SEQ_CHAIN_CONTOUR( contour ))
{
cv::Point pt = ((CvChain*)contour)->origin;
cv::Point prev_pt = pt;
char prev_code = reader.ptr ? reader.ptr[0] : '\0';
prev_pt += offset;
for( i = 0; i < count; i++ )
{
char code;
CV_READ_SEQ_ELEM( code, reader );
CV_Assert( (code & ~7) == 0 );
if( code != prev_code )
{
prev_code = code;
if( thickness >= 0 )
cv::ThickLine( img, prev_pt, pt, clr, thickness, line_type, 2, 0 );
else
pts.push_back(pt);
prev_pt = pt;
}
pt.x += CodeDeltas[(int)code][0];
pt.y += CodeDeltas[(int)code][1];
}
if( thickness >= 0 )
cv::ThickLine( img, prev_pt,
cv::Point(((CvChain*)contour)->origin) + offset,
clr, thickness, line_type, 2, 0 );
else
cv::CollectPolyEdges(img, &pts[0], (int)pts.size(),
edges, ext_buf, line_type, 0, offset);
}
else if( CV_IS_SEQ_POLYLINE( contour ))
{
CV_Assert( elem_type == CV_32SC2 );
cv::Point pt1, pt2;
int shift = 0;
count -= !CV_IS_SEQ_CLOSED(contour);
{ CvPoint pt_ = CV_STRUCT_INITIALIZER; CV_READ_SEQ_ELEM(pt_, reader); pt1 = pt_; }
pt1 += offset;
if( thickness < 0 )
pts.push_back(pt1);
for( i = 0; i < count; i++ )
{
{ CvPoint pt_ = CV_STRUCT_INITIALIZER; CV_READ_SEQ_ELEM(pt_, reader); pt2 = pt_; }
pt2 += offset;
if( thickness >= 0 )
cv::ThickLine( img, pt1, pt2, clr, thickness, line_type, 2, shift );
else
pts.push_back(pt2);
pt1 = pt2;
}
if( thickness < 0 )
cv::CollectPolyEdges( img, &pts[0], (int)pts.size(),
edges, ext_buf, line_type, 0, cv::Point() );
}
}
if( thickness < 0 )
cv::FillEdgeCollection( img, edges, ext_buf );
if( h_next && contour0 )
contour0->h_next = h_next;
}
static void
CollectPolyEdges( Mat& img, const Point2l* v, int count, std::vector<PolyEdge>& edges,
const void* color, int line_type, int shift, Point offset )
{
int i, delta = offset.y + ((1 << shift) >> 1);
Point2l pt0 = v[count-1], pt1;
pt0.x = (pt0.x + offset.x) << (XY_SHIFT - shift);
pt0.y = (pt0.y + delta) >> shift;
edges.reserve( edges.size() + count );
for( i = 0; i < count; i++, pt0 = pt1 )
{
Point2l t0, t1;
PolyEdge edge;
pt1 = v[i];
pt1.x = (pt1.x + offset.x) << (XY_SHIFT - shift);
pt1.y = (pt1.y + delta) >> shift;
if( line_type < CV_AA )
{
t0.y = pt0.y; t1.y = pt1.y;
t0.x = (pt0.x + (XY_ONE >> 1)) >> XY_SHIFT;
t1.x = (pt1.x + (XY_ONE >> 1)) >> XY_SHIFT;
Line( img, t0, t1, color, line_type );
}
else
{
t0.x = pt0.x; t1.x = pt1.x;
t0.y = pt0.y << XY_SHIFT;
t1.y = pt1.y << XY_SHIFT;
LineAA( img, t0, t1, color );
}
if( pt0.y == pt1.y )
continue;
if( pt0.y < pt1.y )
{
edge.y0 = (int)(pt0.y);
edge.y1 = (int)(pt1.y);
edge.x = pt0.x;
}
else
{
edge.y0 = (int)(pt1.y);
edge.y1 = (int)(pt0.y);
edge.x = pt1.x;
}
edge.dx = (pt1.x - pt0.x) / (pt1.y - pt0.y);
edges.push_back(edge);
}
}
static void
FillEdgeCollection( Mat& img, std::vector<PolyEdge>& edges, const void* color )
{
PolyEdge tmp;
int i, y, total = (int)edges.size();
Size size = img.size();
PolyEdge* e;
int y_max = INT_MIN, y_min = INT_MAX;
int64 x_max = 0xFFFFFFFFFFFFFFFF, x_min = 0x7FFFFFFFFFFFFFFF;
int pix_size = (int)img.elemSize();
if( total < 2 )
return;
for( i = 0; i < total; i++ )
{
PolyEdge& e1 = edges[i];
CV_Assert( e1.y0 < e1.y1 );
// Determine x-coordinate of the end of the edge.
// (This is not necessary x-coordinate of any vertex in the array.)
int64 x1 = e1.x + (e1.y1 - e1.y0) * e1.dx;
y_min = std::min( y_min, e1.y0 );
y_max = std::max( y_max, e1.y1 );
x_min = std::min( x_min, e1.x );
x_max = std::max( x_max, e1.x );
x_min = std::min( x_min, x1 );
x_max = std::max( x_max, x1 );
}
if( y_max < 0 || y_min >= size.height || x_max < 0 || x_min >= ((int64)size.width<<XY_SHIFT) )
return;
std::sort( edges.begin(), edges.end(), CmpEdges() );
// start drawing
tmp.y0 = INT_MAX;
edges.push_back(tmp); // after this point we do not add
// any elements to edges, thus we can use pointers
i = 0;
tmp.next = 0;
e = &edges[i];
y_max = MIN( y_max, size.height );
for( y = e->y0; y < y_max; y++ )
{
PolyEdge *last, *prelast, *keep_prelast;
int draw = 0;
int clipline = y < 0;
prelast = &tmp;
last = tmp.next;
while( last || e->y0 == y )
{
if( last && last->y1 == y )
{
// exclude edge if y reaches its lower point
prelast->next = last->next;
last = last->next;
continue;
}
keep_prelast = prelast;
if( last && (e->y0 > y || last->x < e->x) )
{
// go to the next edge in active list
prelast = last;
last = last->next;
}
else if( i < total )
{
// insert new edge into active list if y reaches its upper point
prelast->next = e;
e->next = last;
prelast = e;
e = &edges[++i];
}
else
break;
if( draw )
{
if( !clipline )
{
// convert x's from fixed-point to image coordinates
uchar *timg = img.ptr(y);
int x1, x2;
if (keep_prelast->x > prelast->x)
{
x1 = (int)((prelast->x + XY_ONE - 1) >> XY_SHIFT);
x2 = (int)(keep_prelast->x >> XY_SHIFT);
}
else
{
x1 = (int)((keep_prelast->x + XY_ONE - 1) >> XY_SHIFT);
x2 = (int)(prelast->x >> XY_SHIFT);
}
// clip and draw the line
if( x1 < size.width && x2 >= 0 )
{
if( x1 < 0 )
x1 = 0;
if( x2 >= size.width )
x2 = size.width - 1;
ICV_HLINE( timg, x1, x2, color, pix_size );
}
}
keep_prelast->x += keep_prelast->dx;
prelast->x += prelast->dx;
}
draw ^= 1;
}
// sort edges (using bubble sort)
keep_prelast = 0;
do
{
prelast = &tmp;
last = tmp.next;
PolyEdge *last_exchange = 0;
while( last != keep_prelast && last->next != 0 )
{
PolyEdge *te = last->next;
// swap edges
if( last->x > te->x )
{
prelast->next = te;
last->next = te->next;
te->next = last;
prelast = te;
last_exchange = prelast;
}
else
{
prelast = last;
last = te;
}
}
if (last_exchange == NULL)
break;
keep_prelast = last_exchange;
} while( keep_prelast != tmp.next && keep_prelast != &tmp );
}
}