gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:14:06 25 4
gpt4 key购买 nike

我想画一个有 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]])

有没有办法做到这一点,最好是没有循环?感谢您的帮助

最佳答案

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

简短的回答不是真的。你不能在 cv2 中这样做。我还检查了大约 5 或 6 个其他库,它们也一样(我相信您也这样做了)。

但并非所有都丢失了。我有一种强烈的感觉,cv2中的折线是使用line函数实现的。在我生锈的 cpp 岁月里,这是我在深入研究 OpenCV 的 Linux 和 Mac 源代码时收集到的信息(https://github.com/Itseez/opencv/archive/3.0.0.zip):

在opencv-3.0.0/modules/imgproc/src/drawing.cpp

polylines在代码块末尾调用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++ 代码或多或少地做着相同的事情:迭代线条绘制函数。

如果您想对此进行测试,您可以通过调用折线和线并对其计时来绘制一个单色多边形,其边数接近您在应用程序中需要使用的边数。这是一个非常快速和肮脏的示例,说明如何在 Python 中对来自 Learning Python 5th Edition 的内容进行计时。第 630 页:

import time
def timer(func, *args):
start = time.clock()
for i in range(1000):
func(*args)
return time.clock() - start

我相信您可以找到比这更好的工具来进行测试:)

最后一个想法:如果我错了并且两种方法之间存在明显的性能差异,您可以随时优化代码以提高速度。有很多工具可以加速 Python 工具。您可以从查看 PyPy 开始。

关于python - 如何为多边形的每一侧使用不同的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32339675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com