简体   繁体   English

如何从python中的轮廓线提取坐标并将其存储在新的x和y变量中?

[英]How to extract coordinates from contour lines in python and store them in new x and y variables?

I am facing a problem in extracting coordinates from a contour plot. 我在从等高线图中提取坐标时遇到问题。 I first plotted contour line in python. 我首先在python中绘制轮廓线。 Then I want to extract only the x,y coordinates along the contour line and store them in x and y variables to use them in the next operation. 然后,我只想沿轮廓线提取x,y坐标,并将它们存储在x和y变量中,以在下一个操作中使用它们。 I saw a code to extract the coordinates along the contour in the forum but that does not give me points to store in separate x and y variable so that I can use them in the next operation. 我在论坛中看到了一个代码,用于提取沿轮廓的坐标,但是并没有给我要存储在单独的x和y变量中的点,因此我可以在下一个操作中使用它们。

Please help me if there is an easy python code possible for this operation. 如果此操作有简单的python代码,请帮助我。 I am not expert in python. 我不是python专家。

I have tried a link from the forum but that does not give me coordinates like xy it comes with bracketed x and y. 我已经尝试了论坛中的链接,但没有像xy那样给我坐标,它带有带括号的x和y。 I cannot write the code here as it shows some error in this editing window. 我无法在此处编写代码,因为它在此编辑窗口中显示了一些错误。

def get_contour_verts(cp):
    contours=[]
    for cc in cp.collections:
        paths=[]
        for pp in cc.get_paths():
            xy=[]
            for vv in pp.iter_segments():
                xy.append(vv[0])
            paths.append(np.vstack(xy))
        contours.append(paths)
    return contours

contours=get_contour_verts(cp)
      for ip,path in enumerate(contours):
        for i,item in enumerate(path):

           print(path[i])

Using this code I get output path[i] containing x and y within brackets together. 使用此代码,我得到包含x和y的输出路径[i]放在方括号内。 I want to save the output x and y coordinates as an array. 我想将输出x和y坐标保存为数组。 The output data looks like, 输出数据看起来像

[[-0.0018 -0.02222131]] [[-0.0018 -0.02222131]]

But I want them as x=-0.0018 y= -0.02222131 Thanks, 但我希望它们为x = -0.0018 y = -0.02222131,谢谢,

I just tried with for ip,path in enumerate(contours): for i,item in enumerate(path): print(item[i][0],item[i][1]) 我刚刚尝试使用ip,enumerate(contours)中的路径:对于i,enumerate(path)中的项目:print(item [i] [0],item [i] [1])

But it is not giving me a list of x and y values but showing error "IndexError: index 1 is out of bounds for axis 0 with size 1" 但这并没有给我x和y值的列表,而是显示错误“ IndexError:索引1超出了轴1的大小1的范围”

If path[i] gives [[-0.0018 -0.02222131]] then 如果path[i]给出[[-0.0018 -0.02222131]]

 x = path[i][0][0]
 y = path[i][0][1] 

暂无
暂无

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

相关问题 在 Open CV Python 中从 cv2.contour 获取 x 和 y 坐标并将其存储到不同的变量中 - Getting the x and y coordinates from cv2.contour in Open CV Python and store it to different variables 如何在 python 中存储(x,y)坐标并能够更新它们 - How to store (x,y) Coordinates in python and be able to update them 如何从CSV文件中提取数据列并将其定义为x和y变量,然后使用pylab在python中绘制它们? - How can I extract columns of data from a CSV file and define them as x and y variables, then plot them in python using pylab? 如何从matplotlib中的x,y,z坐标绘制等高线图? (plt.contourf或plt.contour) - How to do a contour plot from x,y,z coordinates in matplotlib? (plt.contourf or plt.contour) 在 Python 中从图像中提取每个像素的 x,y 坐标 - Extract x,y coordinates of each pixel from an image in Python 使用python获取图像中形状的轮廓(x,y)坐标 - Get a contour (x,y) coordinates of a shape in an image with python 如何从特定轮廓提取坐标? - How to extract coordinates from a specific contour? 给定Python中具有X、Y、Z坐标的数据集,如何在NumPy中生成轮廓? - Given a dataset with X, Y, Z coordinates in Python, how to generate a contour in NumPy? 如何使用Python从地理数据中提取x,y和z坐标? - How can I extract x, y and z coordinates from geographical data by Python? 如何从图像中提取(x,y)坐标并写入 CSV 文件? - How to extract (x, y) coordinates from image and write to CSV file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM