这可能不是你要找的答案:)
简短的回答并不是真的.你不能在cv2中这样做.我还检查了大约5或6个其他库,它们也是相同的(我相信你也做过).
但并非所有人都失去了.我强烈感觉cv2中的折线是通过使用线函数实现的.从我生锈的cpp岁月开始,这是我在深入研究OpenCV for linux和mac(https://github.com/Itseez/opencv/archive/3.0.0.zip)的源代码时收集的内容:
在opencv-3.0.0 / modules / imgproc / src / drawing.cpp中
折线调用PolyLine在代码块的末尾进行绘图
void polylines( Mat& img, const Point* const* pts, const int* npts, int ncontours, bool isClosed,
const Scalar& color, int thickness, int line_type, int shift )
{
if( line_type == CV_AA && img.depth() != CV_8U )
line_type = 8;
CV_Assert( pts && npts && ncontours >= 0 &&
0 <= thickness && thickness <= MAX_THICKNESS &&
0 <= shift && shift <= XY_SHIFT );
double buf[4];
scalarToRawData( color, buf, img.type(), 0 );
for( int i = 0; i < ncontours; i++ )
PolyLine( img, pts[i], npts[i], isClosed, buf, thickness, line_type, shift );
}
PolyLine调用ThickLine通过循环绘制段.
PolyLine( Mat& img, const Point* v, int count, bool is_closed,
const void* color, int thickness,
int line_type, int shift )
{
if( !v || count <= 0 )
return;
int i = is_closed ? count - 1 : 0;
int flags = 2 + !is_closed;
Point p0;
CV_Assert( 0 <= shift && shift <= XY_SHIFT && thickness >= 0 );
p0 = v[i];
for( i = !is_closed; i < count; i++ )
{
Point p = v[i];
ThickLine( img, p0, p, color, thickness, line_type, flags, shift );
p0 = p;
flags = 2;
}
}
ThickLine反过来调用各种Line函数来执行它的实现,因为它是一个很长的函数,但只是看看它在绘制厚度为1或更小的行时的作用,它会调用Line函数
ThickLine( Mat& img, Point p0, Point p1, const void* color,
int thickness, int line_type, int flags, int shift )
{
static const double INV_XY_ONE = 1./XY_ONE;
p0.x <<= XY_SHIFT - shift;
p0.y <<= XY_SHIFT - shift;
p1.x <<= XY_SHIFT - shift;
p1.y <<= XY_SHIFT - shift;
if( thickness <= 1 )
{
if( line_type < CV_AA )
{
if( line_type == 1 || line_type == 4 || shift == 0 )
{
p0.x = (p0.x + (XY_ONE>>1)) >> XY_SHIFT;
p0.y = (p0.y + (XY_ONE>>1)) >> XY_SHIFT;
p1.x = (p1.x + (XY_ONE>>1)) >> XY_SHIFT;
p1.y = (p1.y + (XY_ONE>>1)) >> XY_SHIFT;
Line( img, p0, p1, color, line_type );
}
else
Line2( img, p0, p1, color );
}
else
LineAA( img, p0, p1, color );
}
...
最后Line(及其变体如Line2等)只是绘制点:
Line( Mat& img, Point pt1, Point pt2,
const void* _color, int connectivity = 8 )
{
if( connectivity == 0 )
connectivity = 8;
else if( connectivity == 1 )
connectivity = 4;
LineIterator iterator(img, pt1, pt2, connectivity, true);
int i, count = iterator.count;
int pix_size = (int)img.elemSize();
const uchar* color = (const uchar*)_color;
for( i = 0; i < count; i++, ++iterator )
{
uchar* ptr = *iterator;
if( pix_size == 1 )
ptr[0] = color[0];
else if( pix_size == 3 )
{
ptr[0] = color[0];
ptr[1] = color[1];
ptr[2] = color[2];
}
else
memcpy( *iterator, color, pix_size );
}
}
这意味着在折线上调用线条不应该有太大的性能损失,因为C代码或多或少地做同样的事情:迭代线条绘制函数.
如果你想测试这个,你可以通过调用折线和直线并计算它们,绘制一个单边的彩色多边形,其边数接近你需要在应用程序中使用的边.这是一个非常快速和肮脏的例子,说明如何从Learning Python 5th Edition第630页开始在Python中计算时间:
import time
def timer(func, *args):
start = time.clock()
for i in range(1000):
func(*args)
return time.clock() - start
我相信你可以找到比这更好的工具来测试:)
最后一个想法:如果我错了并且两种方法之间存在明显的性能差异,那么您可以随后优化代码以获得更快的速度.有很多工具可以加速Python工具.你可以从PyPy开始.