python不同颜色的边_python – 如何为多边形的每一边使用不同的颜色?

这可能不是你要找的答案:)

简短的回答并不是真的.你不能在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开始.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值