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

时间:2015-09-01 19:41:25

标签: python opencv

我想绘制一个50边的开放多边形。现在我正在寻找一种方法,让每一面都有不同的颜色,具体取决于索引,例如:

polygon_coordinates = [ [5, 10], [7, 9], [8, 11], [11, 20] ]
color_index = [100, 75, 200]

我正在寻找像

这样的东西
import cv2
for i in range (0, len(polygon_coordinates)-1):
    cv2.polylines([polygon_coordinates[i],polygon_coordinates[i+1]], color=[color_index[i], color_index[i], color_index[i]])

有没有办法,理想情况下没有循环? 谢谢你的帮助

2 个答案:

答案 0 :(得分:3)

这可能不是您正在寻找的答案:)

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

答案 1 :(得分:1)

我尝试使用matplotlib模块根据您的输入结构创建一个简单的函数。我希望它能回答一下你的问题:

import matplotlib.pyplot as plt 

def color_segment(pc, col=[]):
    n_segment = len(pc)-1
    for i in range(0,n_segment):
        if col:
            c = col[i]    
            # I suppose the list col contains tuples for (r,g,b) color code for every segment.
        else:
            c = (float(i)/n_segment, 0.0, float(n_segment-i)/n_segment)

        plt.plot([pc[i][0], pc[i+1][0]], [pc[i][1], pc[i+1][1]], color=c)

    plt.show()

polygon_coordinates = [ [5, 10], [7, 9], [8, 11], [11, 20] ]
color_segment(polygon_coordinates)

图片https://cdn.getdatajoy.com/gq2rp3/output.png