简体   繁体   English

如何有效地在python中找到图形的顶部边界线

[英]How to efficienctly find the top border line of a graph in python

I have a set of graphs from which I want to find an outline graph (Black line in this figure.) 我有一组图形,希望从中找到轮廓图( 图中的黑线。)

Finding the maximum of each graph at all points on the x-axis is not possible because the x-values are not same for all the graphs. 不可能在x轴上的所有点上找到每个图的最大值,因为所有图的x值都不相同。 The points are accurate to a couple of decimal places. 这些点精确到小数点后两位。 this figure might be able to help understand better. 这个数字可能有助于更好地理解。

I tried converting each graph to a polygon and using shapely cascaded_union and then cropping off the bottom. 我尝试将每个图形都转换为多边形,然后使用形状层叠的union_union,然后裁剪底部。 It works for a small number of graphs, but when the number of graphs becomes large. 它适用于少数图,但是当图数变大时。 It takes a lot of time. 这需要很多时间。

Is there some other efficient way to do this? 还有其他有效的方法吗?

Sort all your points by their x coordinate. 通过x坐标对所有点进行排序。

Your final output will have a finite number of pixels. 您的最终输出将具有有限数量的像素。 You can compute the range of x values that fall within each pixel(small range but not 0). 您可以计算落在每个像素内的x值的范围(较小范围,但不能为0)。 So split your points into buckets. 因此,将您的观点分成多个部分。 Since they are already sorted, you just need to advance through the list until the values belongs to the next range. 由于已经对它们进行了排序,因此您只需要遍历列表,直到这些值属于下一个范围。

For each pixel column compute the maximum y value you find. 为每个像素列计算找到的最大y值。 Add a point at the (x, y) for the black line. 在(x,y)处为黑线添加一个点。

The complexity of this will be o(N logN). 其复杂度将为o(N logN)。

If you have gaps on the x axis you can choose to either skip it and have a gap in the black line or simply interpolate between the neighbouring values. 如果x轴上有间隙,则可以选择跳过它,或者在黑线中有间隙,或者只是在相邻值之间进行插值。 If you plot the blackline as a collection of line segments you can just skip generating a point for that column and let the renderer do the interpolation for you. 如果将黑线绘制为线段的集合,则可以跳过为该列生成点的操作,然后让渲染器为您进行插值。

If your original points are too rare (they skip pixels) your line may look jagged (jumping up and down). 如果原始点太稀少(它们会跳过像素),则线条可能看起来参差不齐(上下跳跃)。 You can avoid this by adding interpolated values for the functions that don't have a point in that range. 您可以通过为没有该范围内的点的函数添加插值来避免这种情况。 Linear interpolation should work just fine. 线性插值应该可以正常工作。 Make sure you generate try to generate a point at the beginning and the end of the interval and take the larger y-value. 确保生成尝试在间隔的开始和结束处生成一个点并采用较大的y值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM