将imshow与用户输入结合

1 投票
1 回答
1388 浏览
提问于 2025-04-18 05:35

我正在尝试使用trackpy(简称tp)来追踪粒子。我有一系列细胞样本的图片。自然,这些图片中会有一些噪声。追踪的第一步是从这系列的第一张图片中选择哪些聚集体是细胞,哪些不是。这主要是通过tp.locate来完成的。不过,这个过程并不是完美的。我希望能逐个查看tp.locate选出的“候选者”,并标记它们是否是细胞。

为此,我创建了一个叫做ID的函数。我的目标是遍历tp.locate生成的“候选者”列表。我想通过matplotlib的imshow函数显示每个“候选者”,同时提示用户输入,来指示这个“候选者”是否是细胞。

问题是,要求用户输入似乎会抑制imshow函数的输出。每次循环都会询问不同的候选者,但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]

1 个回答

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来写脚本,因为它的状态管理可能会给你带来很多麻烦。

撰写回答