python - 将 imshow 与用户输入相结合

标签 python matplotlib user-input imshow trackpy

我正在尝试使用 trackpy(以下称为 tp)进行粒子跟踪。我有一系列细胞样本的图像。当然,图像中存在一些噪声。跟踪的第一步是从该系列的第一张图像中选择哪些簇是细胞,哪些簇不是。这很大程度上是由 tp.locate 完成的。这并不完美。我希望能够浏览 tp.locate 选择的“候选者”,并指示每个候选者是否是一个单元格。

我创建了函数 ID 来执行此操作。目标是浏览 tp.locate 生成的“候选者”列表。我想通过显示(通过 matplotlib 的 imshow 函数)每个“候选者”来做到这一点,同时提示用户输入以指示“候选者”是否是一个单元格。

问题在于,要求用户输入似乎会抑制 imshow 函数的输出。每次通过 for 循环都会询问不同的候选者,但 imshow 窗口实际上从未显示该候选者。我不知道如何解决这个问题,我觉得我非常接近我的最终目标,所以我非常感谢您的意见。

我不需要 GUI,但是有什么方法可以使用 tkinter 处理这个问题吗?我不熟悉 tkinter,但我读过一些东西,这让我觉得我可以用它解决这个问题。

import numpy as np
import matplotlib.pyplot as plt

def framer(f,image,windowsize=(60,100)):
    arr = image[:,:] #This makes a copy of image, so that when the buffers are 
                     #added for the following process, the input image (image) 
                     #is not modified.
    h = windowsize[0]
    w = windowsize[1]
    hbuffer = np.zeros((h/2,arr.shape[1]))
    arr = np.concatenate((hbuffer,arr,hbuffer),axis=0) #Buffer takes care of situations
                                                       #where the crop window extends
                                                       #beyond input image dimensions         
    wbuffer = np.zeros((arr.shape[0],w/2))
    arr = np.concatenate((wbuffer,arr,wbuffer),axis=1)
    narr = np.zeros((f.shape[0],h,w)) #Initialize array of crop windows
    for i in range(f.shape[0]):
        crop_arr = arr[f.get_value(i,'y'):f.get_value(i,'y') + h,f.get_value(i,'x'):f.get_value(i,'x') + w]   #THIS MIGHT BE BACKWARDS
        narr[i] = crop_arr
    return narr

def ID(f,image,windowsize=(60,100)):
    arr = framer(f,image,windowsize=(60,100))
    f_cop = f[:]
    reslist = np.zeros((arr.shape[0]))
    for i in range(arr.shape[0]):
        plt.imshow(arr[i],cmap='gray')
        plt.annotate('particle '+repr(i),xy=(f.get_value(i,'x'),\
        f.get_value(i,'y')),xytext=(f.get_value(i,'x')+20,f.get_value(i,'y')+20),\
        arrowprops=dict(facecolor='red', shrink=0.05),fontsize=12,color='r')

        res = input('Is this a cell? 1 for yes, 0 for no, 5 to exit')
        if res == 1 or res == 0:
            reslist[i] = res
        if res == 5:
            break
        else:
            print('Must give a valid input! (0,1 or 5)')
    f_cop['res'] = reslist
    return f_cop[f_cop.res == 1]

最佳答案

尝试如下:

fig, ax = plt.subplots(1, 1)


for i in range(arr.shape[0]):
    ax.cla()
    im = ax.imshow(arr[i], cmap='gray', interpolation='nearest')
    plt.annotate('particle ' + repr(i), xy=(f.get_value(i, 'x'), f.get_value(i,'y')),
                  xytext=(f.get_value(i,'x')+20, f.get_value(i,'y')+20),
                  arrowprops=dict(facecolor='red', shrink=0.05),
                  fontsize=12,color='r')
    fig.canvas.draw()
    res = None
    while res not in (0, 1, 5):
        res = input('Is this a cell? 1 for yes, 0 for no, 5 to exit')
    if res == 1 or res == 0:
        reslist[i] = res
    elif res == 5:
        break
    else:
        print "should never get here"

你应该尽可能避免使用 pyplot 脚本,状态机会给你带来很大的麻烦。

关于python - 将 imshow 与用户输入相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501357/

相关文章:

c++ - 根据用户输入打开文件 C++

python - 如何在 Anaconda x64 上安装 Yandex CatBoost?

python - matplotlib - 允许条形图超出图表限制吗?

c - gets() 函数抛出异常?

unity3d - 使用新输入系统损坏的按钮

python - 在 Python 中绘制 4 维连续线

python - 从 S3 下载透明背景文件

python - Keras/Tensorflow CNN 输入形状

javascript - 将数据结构从 Python 移植到 JS

python - 在 Matplotlib 中,有没有办法知道可用输出格式的列表