我正在将一个jpeg图像(大型:1300x2000)加载到matplotlib中,在其上绘制一个50x50的方格,并单击每个方格进行着色编码。然而,我注意到程序在我的点击之后远远落后,并且如果我以合理的速度快速点击50个方格,则需要长达30秒才能赶上。我想知道是否有人可以加速。以下是我的脚本,如果您复制/粘贴它并具有scipy,numpy,matplotlib,pillow和tkinter,则可以立即使用。
欢迎任何建议。我是一名医学科学家,所以请原谅我如果代码没有很好地解释:
import matplotlib
import matplotlib.pyplot as plt
import tkinter
import tkinter.filedialog
from matplotlib.figure import Figure
import math, sys
import numpy as np
import scipy.io as sio
from PIL import Image
from numpy import arange, sin, pi
#from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
#matplotlib.matplotlib_fname()
import os, re
global stridesize, classnumber,x, im,fn, plt, fig, mask
classnumber = 1
def onmove(eve):
global x,im, plt
print(eve.ydata)
print(eve.button)
if (eve.ydata !=None) and (eve.xdata !=None):
if eve.button==1:
print(eve.button)
xcoord = int(eve.xdata)
ycoord = int(eve.ydata)
startX = math.floor(xcoord/stridesize)*stridesize
startY = math.floor(ycoord/stridesize)*stridesize
# print(eve.xdata, int(eve.ydata), stridesize)
if(classnumber==1):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,0,0])
if(classnumber==2):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,255,0])
if(classnumber==3):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,0,255])
if(classnumber==4):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,0,255])
if(classnumber==5):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,255,0])
if(classnumber==6):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,255,255])
if(classnumber==7):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([100,255,50])
if eve.button==3:
xcoord = int(eve.xdata)
ycoord = int(eve.ydata)
startX = math.floor(xcoord/stridesize)*stridesize
startY = math.floor(ycoord/stridesize)*stridesize
print(eve.xdata, int(eve.ydata), stridesize)
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,0,0])
im.set_data(mask)
fig.canvas.draw()
def onclick(event):
if (event.ydata !=None) and (event.xdata !=None):
global x, im, fig
if event.button==1:
xcoord = int(event.xdata)
ycoord = int(event.ydata)
startX = math.floor(xcoord/stridesize)*stridesize
startY = math.floor(ycoord/stridesize)*stridesize
print(event.xdata, int(event.ydata), stridesize)
if(classnumber==1):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,0,0])
if(classnumber==2):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,255,0])
if(classnumber==3):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,0,255])
if(classnumber==4):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,0,255])
if(classnumber==5):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([255,255,0])
if(classnumber==6):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,255,255])
if(classnumber==7):
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([100,255,50])
im.set_data(mask)
if event.button==3:
xcoord = int(event.xdata)
ycoord = int(event.ydata)
startX = math.floor(xcoord/stridesize)*stridesize
startY = math.floor(ycoord/stridesize)*stridesize
print(event.xdata, int(event.ydata), stridesize)
mask[startY:startY+stridesize,startX:startX+stridesize,:]=np.array([0,0,0])
im.set_data(mask)
fig.canvas.draw()
def onpress(event):
global classnumber, mask
if (event.key == 'e'):
print("YO")
mask[:,:,:]=0;
im.set_data(mask)
fig.canvas.draw()
if (event.key=='s'):
savemask(fn)
if (event.key=='r'):
plt.figure();
plt.imshow(mask);
plt.show();
if int(event.key) > 0 and int(event.key) <9 :
classnumber = int(event.key)
print(classnumber)
def onrelease(event):
print(event.button)
# im.set_data(mask)
def savemask(fn):
# matrixname =os.path.basename(filename)
# matrixname = re.sub(r'\.jpg','',matrixname)
pre, ext = os.path.splitext(fn)
savename_default = os.path.basename(pre)
options = {}
options['defaultextension'] = ''
options['filetypes'] = [('mat files', '.mat')]
options['initialdir'] = ''
options['initialfile'] = savename_default
options['title'] = 'Save file'
f = tkinter.filedialog.asksaveasfile(**options)
if f is None: # asksaveasfile return `None` if aadialog closed with "cancel".
return
name = f.name
sio.savemat(name,{'mask':mask},do_compression=True)
f.close()
root = tkinter.Tk()
root.withdraw()
options = {}
options['defaultextension'] = '.jpg'
options['filetypes'] = [('Jpeg', '.jpg')]
options['initialdir'] = 'C:\\'
options['initialfile']= ''
options['parent'] = root
options['title'] = 'This is a title'
fn= tkinter.filedialog.askopenfilename(**options)
img = Image.open(fn)
x = np.asarray(img)
x.setflags(write=1)
#masksize= (x.shape[0],x.shape[1],4)
mask= np.zeros(x.shape,'uint8')
#mask[:,:,3]=0.2
fig = plt.figure()
fig.suptitle(r'Key codes: 1 = Tumour, 2 = stroma-hypocellular, 3=stroma cellular (inflammatory)' '\n4 = proteinaceous, 5= red cells, 6,7: anyother,''\nRight click: clear square''\n r: review mask, e: erase mask, o : open mask image, s : save mask image;')
im=plt.imshow(x)
im=plt.imshow(mask,alpha=.25)
ax = plt.gca();
stridesize = 50;
plt.rcParams['keymap.save']=''
ax.set_yticks(np.arange(0, x.shape[0], stridesize));
ax.set_xticks(np.arange(0, x.shape[1], stridesize));
cid = fig.canvas.mpl_connect('button_press_event', onclick)
cod = fig.canvas.mpl_connect('key_press_event', onpress)
#cdd = fig.canvas.mpl_connect('motion_notify_event', onmove)
cdr = fig.canvas.mpl_connect('button_release_event', onrelease)
plt.grid(b=True, which='both', color='black',linestyle='-')
#
plt.show()
plt.ion()