1
    from nptdms import TdmsFile as td  
    from matplotlib import pyplot as plt
    import numpy as np
    import skimage.color
    import skimage.filters
    import mplcursors
    from skimage.feature import corner_harris,corner_peaks
    
    file = 'sample.tdms' 
    with td.open(file) as tdms_file:
     img = tdms_file.as_dataframe()
    cropped_list = []
    sel=cropped_list.append(img.iloc[700:1250,450:1550:])
coords=corner_peaks(corner_harris(sel),min_distance=10,threshold_rel=0.02)
    fig, ax = plt.subplots()
    ax.imshow(sel, cmap='gray')
    plotted_points =ax.plot(coords[:, 1], coords[:, 0], color='cyan', marker='o',linestyle='None', markersize=2) 
    mplcursors.cursor(plotted_points, hover=True)
    plt.show(

我尝试使用 mplcursors。但是当鼠标悬停时,绘图上没有显示任何内容。箭头未显示任何坐标。我尝试只绘制没有图像的点,但它仍然没有显示任何坐标。

plot_withoutimage

[108 162] [ 65 397] [212 552] [ 62 645] [109 550] [256 240] [283 24] [185 552] [286 86] [186 242] [355 180] [141 550] [64 707] [283 704] [285 330] [257 398] [185 398] [285 398] [109 126] [356 89] [37 709] [64 331] [111 316] [35 334] [214 487] [328 241] [119 172] [212 88] [356 244] [253 332] [39 240] [328 27] [359 395] [77 266] [358 641] [211 642] [110 706] [181 151] [140 396] [110 332] [64 489] [182 486] [328 396] [254 485] [195 274] [256 549] [111 83] [328 639] [253 179] [339 270] [206 135] [325 488]] 这是坐标数组。那么如何根据图像的形状将坐标保存在变量中。只有第一行中的矩形。我的图像是一系列矩形传感器。这就是为什么我试图通过鼠标点击找到坐标。

4

1 回答 1

1

mplcursors自动创建带有 x 和 y 位置的注释。只有当您需要额外信息时,您才需要编写一个函数来根据所选点更改注释。

这是一些代码,使用随机图像来显示它是如何工作的:

import matplotlib.pyplot as plt
import numpy as np
import mplcursors

coords = np.array([[301,557],[301,378],[201,426],[77,58],[243,12],[466,87],[161,75],[302,489],[197,507],[157,400],[131,85],[379,253],[386,525],[215,500],[165,963],[210,381],[308,254],[285,378],[217,628],[77,500],[156,629],[527,67],[514,1010],[234,264],[523,717],[282,630],[523,799],[355,783],[379,474],[353,711],[351,473],[416,719],[232,249],[185,75],[451,409],[130,247],[384,509],[309,473],[202,937],[130,561],[340,549],[310,627],[348,256],[381,869],[258,555],[342,417],[204,393],[385,489],[523,483],[303,784],[448,476],[228,939],[229,559],[454,260],[158,939],[109,521],[158,248],[160,561],[425,319],[129,939],[526,263],[221,389],[236,754],[203,628],[335,492],[453,319],[160,313],[197,76],[420,410],[129,874],[279,250],[130,310],[206,472],[352,1017],[228,508],[117,555],[278,783],[306,937],[424,475],[157,874],[235,407],[78,552],[129,625],[128,781],[423,934],[349,316],[202,781],[378,935],[475,543],[204,547],[391,546],[424,253],[202,251],[170,549],[236,781],[132,471],[75,959],[128,407],[200,356],[309,716],[232,715],[422,629],[274,562],[201,563],[234,471],[149,89],[277,713],[290,12],[236,313],[161,720],[383,626],[350,867],[451,782],[379,783],[450,934],[166,467],[347,626],[450,559],[451,712],[418,875],[379,318],[156,1018],[306,871],[379,408],[278,471],[203,715],[161,782],[203,872],[277,936],[234,874],[348,933],[235,627],[420,561],[438,502],[204,314],[263,499],[279,317],[421,780],[453,627],[334,959],[174,496],[125,500],[131,542],[159,545],[352,499],[79,1012],[417,501],[501,128],[295,340],[215,543],[218,358],[168,1020],[112,504],[158,502]])

fig, ax = plt.subplots()

img = np.random.rand(550, 1050)
ax.imshow(img, cmap='gray')
plotted_points = ax.plot(coords[:, 1], coords[:, 0], color='cyan', marker='o', linestyle='None', markersize=2)
mplcursors.cursor(plotted_points, hover=True)
plt.show()

mplcursors 显示点坐标

于 2021-04-11T00:59:12.873 回答