<p>谢谢你给胡安一些有用的建议。在</p>
<p>我的目标是有滑块和一个交互式矩形选择器。最后我决定不使用<code>scikit-image</code>库,而是直接使用<code>matplotlib</code>。在</p>
<p>这是我的代码的一个简化版本,可供将来遇到类似问题时使用:</p>
<pre><code>from matplotlib.widgets import RectangleSelector
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
def onselect(eclick, erelease):
'Dummy function'
# Lists to store pixel values
intensity_dumps = []
def toggle_selector(event):
#print(' Key pressed.')
if event.key in ['Q', 'q'] and toggle_selector.RS.active:
print(' RectangleSelector deactivated.')
toggle_selector.RS.set_active(False)
if event.key in ['B', 'b']:
extents = toggle_selector.RS.extents
xmin = np.floor(extents[0]).astype('uint16')
xmax = np.ceil(extents[1]).astype('uint16')
ymin = np.floor(extents[2]).astype('uint16')
ymax = np.ceil(extents[3]).astype('uint16')
# print(xmin,xmax,ymin,ymax)
intensity_dumps.append(image_cropped[ymin:ymax,xmin:xmax])
ax = plt.subplot(111)
plt.subplots_adjust(left=0.2, bottom=0.11, top=0.99)
#plt.xlabel('x (pixels)')
#plt.ylabel('y (pixels)')
ax.set_title('Select region; type <b> to save to list')
img = ax.imshow(image, interpolation='nearest', cmap='terrain')
cb = plt.colorbar(img)
ax_cmin = plt.axes([0.2, 0.01, 0.65, 0.03])
ax_cmax = plt.axes([0.2, 0.06, 0.65, 0.03])
s_cmin = Slider(ax_cmin, 'min percentile', 0, 100, valinit=1)
s_cmax = Slider(ax_cmax, 'max percentile', 0, 100, valinit=99)
def update(val, s=None):
min_display = np.percentile(image, s_cmin.val)
max_display = np.percentile(image, s_cmax.val)
img.set_clim([min_display, max_display])
plt.draw()
s_cmin.on_changed(update)
s_cmax.on_changed(update)
toggle_selector.RS = RectangleSelector(ax,
onselect,
spancoords='pixels',
drawtype='box',
interactive=True)
plt.connect('key_press_event', toggle_selector)
plt.show()
</code></pre>