Skip to content

Instantly share code, notes, and snippets.

@alexlib
Last active April 17, 2022 22:16
Show Gist options
  • Save alexlib/aa51427a2de37d52dce174e61c863813 to your computer and use it in GitHub Desktop.
Save alexlib/aa51427a2de37d52dce174e61c863813 to your computer and use it in GitHub Desktop.
Visualize a 3d cloud of points from a file and then click on a 2D image projection of those points with visualizing the clicks using text in Matplotlib and Jupyter
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"## Installation requirements:\n",
"\n",
"# !conda create -n myptv -y\n",
"# !conda activate myptv\n",
"# !pip install git+https://github.com/alexlib/myptv\n",
"# !pip install ipympl"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from pylab import * # recommended only for short interactive sessions\n",
"%matplotlib tk"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# plot the target file in 3D\n",
"# zoom in with the Right button rolling\n",
"# rotate with the left button\n",
"# pan with the middle button\n",
"\n",
"fig = figure(figsize=(10,10))\n",
"# ax = axes(projection='3d')\n",
"ax = fig.add_subplot(1, 1, 1, projection='3d')\n",
"targets = loadtxt('./Calibration/target_file.csv')\n",
"ax.plot(targets[:,0], targets[:,2], targets[:,1], 'ro')\n",
"for counter, row in enumerate(targets):\n",
" ax.text(row[0], row[2], row[1], str(counter) );\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# looking at the both plots, decide which points you want to pick\n",
"# type here the order of the points you want to click \n",
"\n",
"points = [436, 22, 0, 414, 207, 229]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.text as mpl_text\n",
"\n",
"im = imread('Calibration/cal1.tif')\n",
"fig,ax = subplots(figsize = (im.shape[1]/300, im.shape[0]/300))\n",
"ax.imshow(im, cmap='gray')\n",
"ax.axis('off')\n",
"clicks = []\n",
"\n",
"\n",
"def onclick(event):\n",
"\n",
" # tx = 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata)\n",
" # text.set_text(tx)\n",
" counter = len(clicks)\n",
" print('%s click: button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %\n",
" ('double' if event.dblclick else 'single', event.button,\n",
" event.x, event.y, event.xdata, event.ydata))\n",
" ax.text(event.xdata, event.ydata, str(points[counter]),fontsize=20,color='w')\n",
" plt.draw_if_interactive() \n",
" clicks.append([event.xdata, event.ydata, *targets[points[counter]-1,1:]])\n",
" print(f'counter = {counter}')\n",
"\n",
" \n",
"\n",
"cid = fig.canvas.mpl_connect('button_press_event', onclick)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# replace tmp.txt with the file name from the YAML\n",
"\n",
"savetxt('tmp.txt',array(clicks))\n"
]
}
],
"metadata": {
"interpreter": {
"hash": "fbc45e318997727c983b0f99c79f6104b0e8c67be24f9c29083ccdf8ef91b86e"
},
"kernelspec": {
"display_name": "Python 3.9.12 ('myptv')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
0.0 0.0 0.0
0.0 3.0 0.0
0.0 6.0 0.0
0.0 9.0 0.0
0.0 12.0 0.0
0.0 15.0 0.0
0.0 18.0 0.0
0.0 21.0 0.0
0.0 24.0 0.0
0.0 27.0 0.0
0.0 30.0 0.0
0.0 33.0 0.0
0.0 36.0 0.0
0.0 39.0 0.0
0.0 42.0 0.0
0.0 45.0 0.0
0.0 48.0 0.0
0.0 51.0 0.0
0.0 54.0 0.0
0.0 57.0 0.0
0.0 60.0 0.0
0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.0 0.0
12.0 45.0 0.0
12.0 48.0 0.0
12.0 51.0 0.0
12.0 54.0 0.0
12.0 57.0 0.0
12.0 60.0 0.0
12.0 63.0 0.0
12.0 66.0 0.0
15.0 0.0 0.0
15.0 3.0 0.0
15.0 6.0 0.0
15.0 9.0 0.0
15.0 12.0 0.0
15.0 15.0 0.0
15.0 18.0 0.0
15.0 21.0 0.0
15.0 24.0 0.0
15.0 27.0 0.0
15.0 30.0 0.0
15.0 33.0 0.0
15.0 36.0 0.0
15.0 39.0 0.0
15.0 42.0 0.0
15.0 45.0 0.0
15.0 48.0 0.0
15.0 51.0 0.0
15.0 54.0 0.0
15.0 57.0 0.0
15.0 60.0 0.0
15.0 63.0 0.0
15.0 66.0 0.0
20.0 0.0 -6.0
20.0 3.0 -6.0
20.0 6.0 -6.0
20.0 9.0 -6.0
20.0 12.0 -6.0
20.0 15.0 -6.0
20.0 18.0 -6.0
20.0 21.0 -6.0
20.0 24.0 -6.0
20.0 27.0 -6.0
20.0 30.0 -6.0
20.0 33.0 -6.0
20.0 36.0 -6.0
20.0 39.0 -6.0
20.0 42.0 -6.0
20.0 45.0 -6.0
20.0 48.0 -6.0
20.0 51.0 -6.0
20.0 54.0 -6.0
20.0 57.0 -6.0
20.0 60.0 -6.0
20.0 63.0 -6.0
20.0 66.0 -6.0
24.5 0.0 -12.0
24.5 3.0 -12.0
24.5 6.0 -12.0
24.5 9.0 -12.0
24.5 12.0 -12.0
24.5 15.0 -12.0
24.5 18.0 -12.0
24.5 21.0 -12.0
24.5 24.0 -12.0
24.5 27.0 -12.0
24.5 30.0 -12.0
24.5 33.0 -12.0
24.5 36.0 -12.0
24.5 39.0 -12.0
24.5 42.0 -12.0
24.5 45.0 -12.0
24.5 48.0 -12.0
24.5 51.0 -12.0
24.5 54.0 -12.0
24.5 57.0 -12.0
24.5 60.0 -12.0
24.5 63.0 -12.0
24.5 66.0 -12.0
29.0 0.0 -6.0
29.0 3.0 -6.0
29.0 6.0 -6.0
29.0 9.0 -6.0
29.0 12.0 -6.0
29.0 15.0 -6.0
29.0 18.0 -6.0
29.0 21.0 -6.0
29.0 24.0 -6.0
29.0 27.0 -6.0
29.0 30.0 -6.0
29.0 33.0 -6.0
29.0 36.0 -6.0
29.0 39.0 -6.0
29.0 42.0 -6.0
29.0 45.0 -6.0
29.0 48.0 -6.0
29.0 51.0 -6.0
29.0 54.0 -6.0
29.0 57.0 -6.0
29.0 60.0 -6.0
29.0 63.0 -6.0
29.0 66.0 -6.0
34.0 0.0 0.0
34.0 3.0 0.0
34.0 6.0 0.0
34.0 9.0 0.0
34.0 12.0 0.0
34.0 15.0 0.0
34.0 18.0 0.0
34.0 21.0 0.0
34.0 24.0 0.0
34.0 27.0 0.0
34.0 30.0 0.0
34.0 33.0 0.0
34.0 36.0 0.0
34.0 39.0 0.0
34.0 42.0 0.0
34.0 45.0 0.0
34.0 48.0 0.0
34.0 51.0 0.0
34.0 54.0 0.0
34.0 57.0 0.0
34.0 60.0 0.0
34.0 63.0 0.0
34.0 66.0 0.0
39.0 0.0 -6.0
39.0 3.0 -6.0
39.0 6.0 -6.0
39.0 9.0 -6.0
39.0 12.0 -6.0
39.0 15.0 -6.0
39.0 18.0 -6.0
39.0 21.0 -6.0
39.0 24.0 -6.0
39.0 27.0 -6.0
39.0 30.0 -6.0
39.0 33.0 -6.0
39.0 36.0 -6.0
39.0 39.0 -6.0
39.0 42.0 -6.0
39.0 45.0 -6.0
39.0 48.0 -6.0
39.0 51.0 -6.0
39.0 54.0 -6.0
39.0 57.0 -6.0
39.0 60.0 -6.0
39.0 63.0 -6.0
39.0 66.0 -6.0
43.5 0.0 -12.0
43.5 3.0 -12.0
43.5 6.0 -12.0
43.5 9.0 -12.0
43.5 12.0 -12.0
43.5 15.0 -12.0
43.5 18.0 -12.0
43.5 21.0 -12.0
43.5 24.0 -12.0
43.5 27.0 -12.0
43.5 30.0 -12.0
43.5 33.0 -12.0
43.5 36.0 -12.0
43.5 39.0 -12.0
43.5 42.0 -12.0
43.5 45.0 -12.0
43.5 48.0 -12.0
43.5 51.0 -12.0
43.5 54.0 -12.0
43.5 57.0 -12.0
43.5 60.0 -12.0
43.5 63.0 -12.0
43.5 66.0 -12.0
48.0 0.0 -6.0
48.0 3.0 -6.0
48.0 6.0 -6.0
48.0 9.0 -6.0
48.0 12.0 -6.0
48.0 15.0 -6.0
48.0 18.0 -6.0
48.0 21.0 -6.0
48.0 24.0 -6.0
48.0 27.0 -6.0
48.0 30.0 -6.0
48.0 33.0 -6.0
48.0 36.0 -6.0
48.0 39.0 -6.0
48.0 42.0 -6.0
48.0 45.0 -6.0
48.0 48.0 -6.0
48.0 51.0 -6.0
48.0 54.0 -6.0
48.0 57.0 -6.0
48.0 60.0 -6.0
48.0 63.0 -6.0
48.0 66.0 -6.0
53.0 0.0 0.0
53.0 3.0 0.0
53.0 6.0 0.0
53.0 9.0 0.0
53.0 12.0 0.0
53.0 15.0 0.0
53.0 18.0 0.0
53.0 21.0 0.0
53.0 24.0 0.0
53.0 27.0 0.0
53.0 30.0 0.0
53.0 33.0 0.0
53.0 36.0 0.0
53.0 39.0 0.0
53.0 42.0 0.0
53.0 45.0 0.0
53.0 48.0 0.0
53.0 51.0 0.0
53.0 54.0 0.0
53.0 57.0 0.0
53.0 60.0 0.0
53.0 63.0 0.0
53.0 66.0 0.0
56.0 0.0 0.0
56.0 3.0 0.0
56.0 6.0 0.0
56.0 9.0 0.0
56.0 12.0 0.0
56.0 15.0 0.0
56.0 18.0 0.0
56.0 21.0 0.0
56.0 24.0 0.0
56.0 27.0 0.0
56.0 30.0 0.0
56.0 33.0 0.0
56.0 36.0 0.0
56.0 39.0 0.0
56.0 42.0 0.0
56.0 45.0 0.0
56.0 48.0 0.0
56.0 51.0 0.0
56.0 54.0 0.0
56.0 57.0 0.0
56.0 60.0 0.0
56.0 63.0 0.0
56.0 66.0 0.0
59.0 0.0 0.0
59.0 3.0 0.0
59.0 6.0 0.0
59.0 9.0 0.0
59.0 12.0 0.0
59.0 15.0 0.0
59.0 18.0 0.0
59.0 21.0 0.0
59.0 24.0 0.0
59.0 27.0 0.0
59.0 30.0 0.0
59.0 33.0 0.0
59.0 36.0 0.0
59.0 39.0 0.0
59.0 42.0 0.0
59.0 45.0 0.0
59.0 48.0 0.0
59.0 51.0 0.0
59.0 54.0 0.0
59.0 57.0 0.0
59.0 60.0 0.0
59.0 63.0 0.0
59.0 66.0 0.0
62.0 0.0 0.0
62.0 3.0 0.0
62.0 6.0 0.0
62.0 9.0 0.0
62.0 12.0 0.0
62.0 15.0 0.0
62.0 18.0 0.0
62.0 21.0 0.0
62.0 24.0 0.0
62.0 27.0 0.0
62.0 30.0 0.0
62.0 33.0 0.0
62.0 36.0 0.0
62.0 39.0 0.0
62.0 42.0 0.0
62.0 45.0 0.0
62.0 48.0 0.0
62.0 51.0 0.0
62.0 54.0 0.0
62.0 57.0 0.0
62.0 60.0 0.0
62.0 63.0 0.0
62.0 66.0 0.0
65.0 0.0 0.0
65.0 3.0 0.0
65.0 6.0 0.0
65.0 9.0 0.0
65.0 12.0 0.0
65.0 15.0 0.0
65.0 18.0 0.0
65.0 21.0 0.0
65.0 24.0 0.0
65.0 27.0 0.0
65.0 30.0 0.0
65.0 33.0 0.0
65.0 36.0 0.0
65.0 39.0 0.0
65.0 42.0 0.0
65.0 45.0 0.0
65.0 48.0 0.0
65.0 51.0 0.0
65.0 54.0 0.0
65.0 57.0 0.0
65.0 60.0 0.0
65.0 63.0 0.0
65.0 66.0 0.0
68.0 0.0 0.0
68.0 3.0 0.0
68.0 6.0 0.0
68.0 9.0 0.0
68.0 12.0 0.0
68.0 15.0 0.0
68.0 18.0 0.0
68.0 21.0 0.0
68.0 24.0 0.0
68.0 27.0 0.0
68.0 30.0 0.0
68.0 33.0 0.0
68.0 36.0 0.0
68.0 39.0 0.0
68.0 42.0 0.0
68.0 45.0 0.0
68.0 48.0 0.0
68.0 51.0 0.0
68.0 54.0 0.0
68.0 57.0 0.0
68.0 60.0 0.0
68.0 63.0 0.0
68.0 66.0 0.0
@alexlib
Copy link
Author

alexlib commented Apr 17, 2022

cal1

@alexlib
Copy link
Author

alexlib commented Apr 17, 2022

Figure_2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment