我想绘制一个50边的开放多边形.现在我正在寻找一种方法,让每一面都有不同的颜色,具体取决于索引,例如:
polygon_coordinates = [ [5, 10], [7, 9], [8, 11], [11, 20] ]
color_index = [100, 75, 200]
Run Code Online (Sandbox Code Playgroud)
我正在寻找类似的东西
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]])
Run Code Online (Sandbox Code Playgroud)
有没有办法,理想情况下没有循环?谢谢您的帮助
小智 3
这可能不是您正在寻找的答案:)
简短的回答不是真的。你不能在 cv2 中这样做。我还检查了大约 5 或 6 个其他库,它们也是一样的(我相信你也这样做过)。
但并非一切都丢失了。我有一种强烈的感觉,cv2中的折线是使用line函数实现的。在我生锈的 cpp 岁月里,以下是我深入研究适用于 Linux 和 Mac 的 OpenCV 源代码时收集到的内容(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 );
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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 );
}
...
Run Code Online (Sandbox Code Playgroud)
最后,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 );
}
}
Run Code Online (Sandbox Code Playgroud)
这意味着通过折线调用线条不会对性能造成太大影响,因为 C++ 代码或多或少执行相同的操作:迭代线条绘制函数。
如果您想对此进行测试,您可以通过调用折线和直线并对它们计时,绘制一个单色多边形,其边数接近您在应用程序中需要使用的边数。下面是一个非常快速且肮脏的示例,说明了如何在 Python 中对内容进行计时(来自《学习 Python 第五版》第 630 页):
import time
def timer(func, *args):
start = time.clock()
for i in range(1000):
func(*args)
return time.clock() - start
Run Code Online (Sandbox Code Playgroud)
我相信您可以找到比这更好的工具来测试:)
最后一个想法:如果我错了并且两种方法之间存在明显的性能差异,您可以随时优化代码以提高速度。有大量工具可以加速 Python 工具。您可以从查看 PyPy 开始。
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |