使用 matplotlib 显示存储在数组中的图像的坐标
show coordinates of an image stored in an array using mathplotlib
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。但是当鼠标悬停时,绘图上没有显示任何内容。箭头未显示任何坐标。
我尝试只绘制没有图像的点,仍然没有显示任何坐标。
[106 190] [205 167] 249 280] [ 80 301] [343 294] [207 151] [293 300] [ 30 298] [116 301] [290 261] [ 38 81] [295 316] [209 262] [210 285] [205 323] [282 244] [128 158] [255 26] [133 708] [213 243] [214 690] [62 241] [68 181] [254 692] [ 35 21] [ 74 318] [290 275] [284 178] [185 21] [207 107] [ 25 323] [331 88] [113 273] [157 293] [112 241] [164 323] [250 301] [137 20] [ 35 645] [284 484] [244 317] [ 27 26
9] [356 331] [356 25] [131 279] [206 296] [ 35 490] [139 85] [215 396] [116 150] [139 644] [338 305] [183 330] [140 488] [327 331] [357 550] [187 707] [260 705] [180 646] [282 552] [61 554] [113 485] [163 269] [114 642] [287 645] [281 287] [140 241] [327 181] [ 36 552] [242 263] [257 87] [ 52 18] [358 61] [110 397] [359 692] [139 176] [212 23] [140 332] [271 704] [254 359] [63 19] [37 396] [110 20] [65 82] [36 176] [211 706] [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]] 这是坐标数组。
那么如何根据图像的形状将坐标保存在变量中。第一行只有矩形。
我的图像是一系列矩形传感器。这就是为什么我尝试通过鼠标单击来查找坐标的原因。
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()
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。但是当鼠标悬停时,绘图上没有显示任何内容。箭头未显示任何坐标。 我尝试只绘制没有图像的点,仍然没有显示任何坐标。
[106 190] [205 167] 249 280] [ 80 301] [343 294] [207 151] [293 300] [ 30 298] [116 301] [290 261] [ 38 81] [295 316] [209 262] [210 285] [205 323] [282 244] [128 158] [255 26] [133 708] [213 243] [214 690] [62 241] [68 181] [254 692] [ 35 21] [ 74 318] [290 275] [284 178] [185 21] [207 107] [ 25 323] [331 88] [113 273] [157 293] [112 241] [164 323] [250 301] [137 20] [ 35 645] [284 484] [244 317] [ 27 26 9] [356 331] [356 25] [131 279] [206 296] [ 35 490] [139 85] [215 396] [116 150] [139 644] [338 305] [183 330] [140 488] [327 331] [357 550] [187 707] [260 705] [180 646] [282 552] [61 554] [113 485] [163 269] [114 642] [287 645] [281 287] [140 241] [327 181] [ 36 552] [242 263] [257 87] [ 52 18] [358 61] [110 397] [359 692] [139 176] [212 23] [140 332] [271 704] [254 359] [63 19] [37 396] [110 20] [65 82] [36 176] [211 706] [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]] 这是坐标数组。 那么如何根据图像的形状将坐标保存在变量中。第一行只有矩形。 我的图像是一系列矩形传感器。这就是为什么我尝试通过鼠标单击来查找坐标的原因。
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()