def find_ROI(p, filenames): """Uses template matching to find the region of interest (terminal bulb) in images.""" # define bulb template templ = define_template(filenames[:20], p) # find template location in all following images filenames = filenames[p.START:p.END:p.INTRVL] # initialize data arrays time = np.arange(p.START,p.END,p.INTRVL) spacing = np.array([p.INTRVL]*(len(time)-1)+[p.END - (len(time)-1)*p.INTRVL - p.START]) locs = np.zeros((len(filenames),2)) confs = np.zeros(len(filenames)) for cnt,fn in enumerate(filenames): img=mpimg.imread(os.path.join(p.DIRC,fn)) if p.ROT: img = np.transpose(img) if p.CROP is not None: img = img[:,p.CROP[0]:p.CROP[1]] # find bulb by template matching yr,xr, conf = find_bulb(img, templ) confs[cnt] = conf locs[cnt] = (yr,xr) time, yroi, spacing = clean_auto_coords(p, time, locs, confs, spacing, n = 3) xroi = np.ones(len(yroi))*img.shape[1]/2. #write data to file write_data(p.OUTDIR, "roi_"+p.BASENAME, zip(time,xroi,yroi, spacing), ncol=4)
def write_ROI(p, filenames): """writes all region of interests to file""" plt.ion() fig = plt.figure(figsize=(12,8), dpi=100,edgecolor='k',facecolor='w') clicks=clickSaver() clicks.reset_data() fig.canvas.mpl_connect('button_press_event', clicks.onclick) ax = fig.add_subplot(111) plt.title("Click on the bulb, if worm is out of frame click outside of image.") filenames = filenames[p.START:p.END:p.INTRVL] time = np.arange(p.START,p.END,p.INTRVL) spacing = [p.INTRVL]*(len(time)-1)+[p.END - (len(time)-1)*p.INTRVL - p.START] try: for cnt,fn in enumerate(filenames): img=mpimg.imread(os.path.join(p.DIRC,fn)) if p.ROT: img = np.transpose(img) if p.CROP !=None: img = img[:,p.CROP[0]:p.CROP[1]] if cnt==0: im=ax.imshow(img,cmap='gray', origin = 'lower') text = plt.text(-1.5,0.9,"%s"%fn, transform = ax.transAxes) text2 = plt.text(-1.5,0.8,"%i\%i"%(float(cnt),(len(filenames)-1)), transform = ax.transAxes) plt.draw() plt.tight_layout() plt.waitforbuttonpress() else: im.set_data(img) text.set_text("%s"%fn) text2.set_text("%i\%i"%(float(cnt),(len(filenames)-1))) plt.draw() #plt.tight_layout() plt.waitforbuttonpress() except IOError: print "Problem with image?" pass finally: xroi, yroi, time = clean_roi(time,clicks.xroi, clicks.yroi) #write data to file write_data(p.OUTDIR, "roi_"+p.BASENAME, zip(time,xroi,yroi, spacing), ncol=4)